include iostream include cmath include locale include time using names

 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
#include <iostream>
#include <cmath>
#include <locale.h>
#include <time.h>
using namespace std;
double MethSteffensen(double, double, double, int&);
double MethChord(double, double, double, int&);
double F(double x){
return pow(5,x)-6*x-3;
}
int main(){
setlocale(0,"");
double a=1,b=2,Fb,Fa,Fc,eps=0.000000000001,root, eps_tmp=eps;
std::streamsize ss = std::cout.precision();
int i, iter;
cout<<"\t\t\t\tМетод хорд\t\t\t\t"<<endl;
cout<<"----------------------------------------";
cout<<"----------------------------------------";
cout<<" № | Итерации |\t Эпсилон \t|\t\tКорень"<<endl;
cout<<"----------------------------------------";
cout<<"----------------------------------------";
cout<< fixed;
cout.precision(15);
for(i=1;eps_tmp<1;i++){
iter=0;
root = MethChord(a,b,eps_tmp,iter);
cout.width(4);
//cout.fill(' ');
cout<<i<<"|";
cout.width(10);
cout<<iter<<"|";
cout.width(24);
cout<<eps_tmp<<"|";
cout<<"\t"<<root<<endl;
eps_tmp*=10;
}
cout<<"----------------------------------------";
cout<<"----------------------------------------";
eps_tmp=eps;
cout.precision(15);
cout<<"\n\t\t\t\tМетод Стеффенсена\t\t"<<endl;
cout<<"----------------------------------------";
cout<<"----------------------------------------";
cout<<" № | Итерации |\t Эпсилон \t|\t\tКорень"<<endl;
cout<<"----------------------------------------";
cout<<"----------------------------------------";
for(i=1;eps_tmp<1;i++){
iter=0;
root = MethSteffensen(a,b,eps_tmp,iter);
cout.width(4);
cout<<i<<"|";
cout.width(10);
cout<<iter<<"|";
cout.width(24);
cout<<eps_tmp<<"|";
cout<<"\t"<<root<<endl;
eps_tmp*=10;
}
// cout<<"I="<<i<<"\nB="<<MethSteffensen(a,b,eps,i);
}
double MethChord(double a, double b, double eps, int &i){
double Fa, Fb, FaF;
for( ;fabs(b - a) > eps; i++)
{
Fa = F(a);
Fb = F(b);
a = b - (b - a) * Fb/(Fb - Fa);
b = a - (a - b) * Fa/(Fa - Fb);
}
return b;
}
double MethSteffensen(double a, double b, double eps, int &i){
double Fa, Fb, FaF;
a = MethChord(a,b,0.1,i);
//i-=3;
Fa = F(a);
FaF = F(a+Fa);
for( ;FaF!=Fa;i++){
b = a-((Fa*Fa)/(FaF-Fa));
Fb = F(b);
if(fabs(b-a) <= eps)
break;
a = b;
Fa = Fb;
FaF = F(a+Fa);
}
return b;
}