include stdio include math include float double EPS double MAX_IT 1000

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <math.h>
#include <float.h>
double EPS;
double MAX_IT=1000;
double K=10000000;
typedef struct inter{
double a;
double a_val;
double b;
double b_val;
double len;
} interval;
double f_02(double x) {
return (cos(x)-exp(-((x*x)/2))+x-1);
}
double f_02_it(double x) {
return (-cos(x)+exp(-((x*x)/2))+1);
}
double f_09(double x){
return x*x-log(1+x)-3;
}
double f_09_it(double x){
return sqrt(log(1+x)+3);
}
double f_10(double x){
return 2*x*sin(x)-cos(x);
}
double f_10_it(double x){
return asin(sqrt(1/(4*x*x+1)));
}
interval divide(interval i,double (*func)(double)){
double c=(i.a+i.b)/2;
double c_val=(*func)(c);
if (c_val==0){
i.a=i.b=c;
i.a_val=i.b_val=c_val;
i.len=0;
return i;
}
if(c_val*i.a_val>0){
i.a=c;
i.a_val=c_val;
i.len=fabs(i.b-i.a);
}
else if(c_val*i.b_val>0){
i.b=c;
i.b_val=c_val;
i.len=fabs(i.b-i.a);
}
return i;
}
double dichotomy(double a, double b, double (*func)(double),int &it){
double x;
it=0;
interval i,i_old;
i.a=a;
i.a_val=(*func)(a);
i.b=b;
i.b_val=(*func)(b);
i.len=fabs(i.b-i.a);
do{
i_old=i;
i= divide(i,func);
it++;
}while(i.len>EPS && fabs(i.len-i_old.len)>EPS && it<MAX_IT);
if(i.len==0){
return i.a;
}
else{
x=(i.a+i.b)/2;
}
return x;
}
double iteration(double a, double b, double (*func_it)(double), double (*func)(double), int &it){
it=0;
double x=(a+b)/2;
double x0=x;
do{
x0=x;
x=(func_it)(x0);
it++;
}while(fabs(x-x0)>EPS && (func)(x)!=0 && it<MAX_IT);
return x;
}
double derivation(double (*func)(double), double x){
return (func(x+EPS)-func(x))/EPS;
}
double newton(double a, double b, double (*func)(double), int &it){
double x=(a+b)/2;
double x0=x;
it=0;
do{
x0=x;
x=x0 - (func)(x0)/derivation(func,x0);
it++;
}while(fabs(x-x0)>EPS && it<MAX_IT && func(x)!=0);
return x;
}
void print_methods(double a, double b, double (*func)(double),double (*func_it)(double),
char *str, char *str_func, char *str_func_it){
int it=0;
double x=0;
printf ("=====================================================================\n %s\n",str);
printf (" Function 1: %s\n",str_func);
printf (" Function 2: %s\n",str_func_it);
printf (" Interval: [%f,%f]\n\n",a,b);
printf ("--------------+-----------------+----------------+-------------------\n");
printf (" Method | Root | Value | Iterations\n");
printf ("--------------+-----------------+----------------+-------------------\n");
x=dichotomy(a,b,func,it);
printf (" Dichotomy | % 1.12f | % e | %4i\n",x,(func)(x),it);
printf ("--------------+-----------------+----------------+-------------------\n");
x=iteration(a,b,func_it, func, it);
printf (" Iteration | % 1.12f | % e | %4i\n",x,(func)(x),it);
printf ("--------------+-----------------+----------------+-------------------\n");
x=newton(a,b,func,it);
printf (" Newton | % 1.12f | % e | %4i\n",x,(func)(x),it);
printf ("--------------+-----------------+----------------+-------------------\n");
printf ("=====================================================================\n\n");
}
int main(){
EPS=DBL_EPSILON*K;
printf (" DBL_EPSILON: %e\n",DBL_EPSILON);
printf (" Epsilon: %2.3f*DBL_EPSILON\n",K);
print_methods(1,2,f_02,f_02_it,"Variant 2","","");
print_methods(2,3,f_09,f_09_it,"Variant 9","x*x-log(1+x)-3","sqrt(log(1+x)+3)");
print_methods(0.4,1,f_10,f_10_it,"Variant 10","2*x*sin(x)-cos(x)","asin(sqrt(1/(4*x*x+1)))");
scanf(" ");
}