Untitled
unknown
plain_text
a year ago
1.5 kB
17
Indexable
#include <iostream>
#include <math.h>
double sin_func(double x, double error) {
double summ = x;
double term = x;
int k = 1;
while (fabs(term) > error) {
term *= (-1) * x * x / ((2 * k) * (2 * k + 1));
summ += term;
k++;
}
return summ;
}
double ch_func(double x, double error) {
double summ = 1;
double term = 1;
int k = 1;
while (fabs(term) > error) {
term *= (x*x) / (2*k*(2*k-1));
summ += term;
k++;
}
return summ;
}
double sqrt_func(double a, double error) {
double prev_x = 0;
double x = 2;
while (1) {
prev_x = x;
x = (1 / 2.) * (x + a / x);
if (fabs(x - prev_x) < error) return x;
}
}
double func(double x) {
return cosh(sqrt(x * x + 0.3) / (1 + x)) * sin((1 + x) / (0.6 * x));
}
int main() {
std::cout << "x my_func std_func error" << std::endl;
float c1 = 0.5;
float c2 = 1.2;
float c3 = 1;
double epsilon1 = pow(10, -6) / (3*c1);
double epsilon2 = pow(10, -6) / (3*c2);
double epsilon3 = pow(10, -6) / (3*c3);
float x = 0.2;
for (int i = 0; i < 11; i++) {
double my_func = ch_func(sqrt_func((x * x + 0.3)/pow(1+x, 2), epsilon1), epsilon3) * sin_func((1 + x) / (0.6 * x), epsilon2);
double std_func = func(x);
printf("%.2f ", x);
printf("%.20f ", my_func);
printf("%.20f ", std_func);
printf("%.20f\n", fabs(my_func - std_func));
x += 0.01;
}
}Editor is loading...
Leave a Comment