-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum_lib.cpp
107 lines (100 loc) · 1.86 KB
/
num_lib.cpp
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
#include<bits/stdc++.h>
using namespace std;
namespace num_lib {
double f(double x) {
double ret;
ret=(x-4)*(x-4)*(x+2);
return ret;
}
double df(double x) {
double ret;
ret=3*(x-4)*x;
return ret;
}
double froot(double mtol,double xl,double xh) {
int flag=0;
double prev;
while(true) {
double up=xh*f(xl)-xl*f(xh);
double down=f(xl)-f(xh);
double xm=up/down;
double tol=0;
if(f(xm)==0) {
return xm;
}
if(flag) {
tol=fabs(xm-prev)/fabs(xm);
if(mtol>=tol) {
printf("%d %lf %lf %lf %lf\n",flag+1,xl,xh,xm,tol*100.00);
return xm;
}
}
flag++;
printf("%d %lf %lf %lf %lf\n",flag,xl,xh,xm,tol*100.00);
if(f(xl)*f(xm)<0.000) {
xh=xm;
}
else {
xl=xm;
}
prev=xm;
}
}
double newton(double mtol,double x)
{
int it=1;double px;
while(true)
{
if(it<=1)
{
//cout<<it<<" "<<x<<" "<<f(x)<<" "<<df(x)<<" "<<0.0000<<endl;
printf("%d %lf %lf %lf %lf\n",it,x,f(x),df(x),0.0000);
if(f(x)==0.00) return x;
px=x;
}
else
{
x=px-(f(px)/df(px));
double tol=fabs(px-x)/fabs(x);
tol*=100;
printf("%d %lf %lf %lf %lf\n",it,x,f(x),df(x),tol);
if(f(x)==0.00) return x;
if(tol<=mtol) return x;
if(it>=8) return x;
px=x;
}
it++;
}
}
double secant(double mtol,double x0,double x1)
{
double x2;
int it=1;
while(true)
{
x2=x1-((f(x1)*(x1-x0))/(f(x1)-f(x0)));
if(it<=1)
{
printf("%d %lf lf %lf %lf %lf\n",it,x0,x1,x2,f(x2),0.0000);
}
else
{
double tol=fabs(x2-x1)/fabs(x2);
tol*=100;
printf("%d %lf lf %lf %lf %lf\n",it,x0,x1,x2,f(x2),tol);
if(mtol>=tol) return x2;
}
if(f(x2)==0.00) return x2;
x0=x1;
x1=x2;
it++;
}
}
}
using namespace num_lib;
int main()
{
cout<<froot(0.0003,-2.5,-1.0)<<endl;
cout<<newton(0.0002,-2.3)<<endl;
cout<<secant(0.1,2.5,-1.0)<<endl;
}