روش bisection :
که نهایتا F(c)=0.00001
برنامه مطلب
%hale moadele cos(2*x)-x^2+1=0 ba raveshe nesf kardan
clc;
clear;
epsilon=input('enter eror:');
a=input('enter first value :');
b=input('enter next value :');
f1=cos(2*a)-a^2+1;
f2=cos(2*b)-b^2+1;
if f1*f2>0
disp('this equation is not root in this interval.');
end
fprintf(' i a b c f1n');
c=1;
fprintf('%4f %5.10f %5.10f %5.10f %5.10fn', 1, a,b, c ,f1);
i=2;
fc=1;
while abs(fc)>epsilon
c=(a+b)/2;
fc=cos(2*c)-c^2+1;
fa=cos(2*a)-a^2+1;
fb=cos(2*b)-b^2+1;
if fa*fc<0
a=a;
b=c;
end
if fa*fc>0
a=c;
b=b;
end
fprintf('%3f %5.4f %5.5f %5.5f %5.4f n', i, a , b , c, fc);
i=i+1;
end
fprintf('tekrar rishe fc n' );
fprintf('%4f %10.10f %10.10f n', i, c , fc);
enter eror:0.00001
enter first value :-1
enter next value :1
this equation is not root in this interval.
i a b c f1
1.000000 -1.0000000000 1.0000000000 1.0000000000 -0.4161468365
2.000000 -1.0000 0.00000 0.00000 2.0000
3.000000 -1.0000 -0.50000 -0.50000 1.2903
4.000000 -1.0000 -0.75000 -0.75000 0.5082
5.000000 -1.0000 -0.87500 -0.87500 0.0561
6.000000 -0.9375 -0.87500 -0.93750 -0.1784
7.000000 -0.9063 -0.87500 -0.90625 -0.0606
8.000000 -0.8906 -0.87500 -0.89063 -0.0021
9.000000 -0.8906 -0.88281 -0.88281 0.0270
10.000000 -0.8906 -0.88672 -0.88672 0.0125
11.000000 -0.8906 -0.88867 -0.88867 0.0052
12.000000 -0.8906 -0.88965 -0.88965 0.0015
13.000000 -0.8901 -0.88965 -0.89014 -0.0003
14.000000 -0.8901 -0.88989 -0.88989 0.0006
15.000000 -0.8901 -0.89001 -0.89001 0.0002
16.000000 -0.8901 -0.89001 -0.89008 -0.0001
17.000000 -0.8901 -0.89005 -0.89005 0.0001
18.000000 -0.8901 -0.89005 -0.89006 -0.0000
tekrar rishe fc
19.0 -0.8900604248 -0.0000067746
enter eror:0.001
enter first value :-1
enter next value :2
this equation is not root in this interval.
i a b c f1
1.000000 -1.0000000000 2.0000000000 1.0000000000 -0.4161468365
2.000000 -1.0000 0.50000 0.50000 1.2903
3.000000 -1.0000 -0.25000 -0.25000 1.8151
4.000000 -1.0000 -0.62500 -0.62500 0.9247
5.000000 -1.0000 -0.81250 -0.81250 0.2857
6.000000 -0.9063 -0.81250 -0.90625 -0.0606
7.000000 -0.9063 -0.85938 -0.85938 0.1141
8.000000 -0.9063 -0.88281 -0.88281 0.0270
9.000000 -0.8945 -0.88281 -0.89453 -0.0167
10.000000 -0.8945 -0.88867 -0.88867 0.0052
11.000000 -0.8916 -0.88867 -0.89160 -0.0058
12.000000 -0.8901 -0.88867 -0.89014 -0.0003
tekrar rishe fc
13.0 -0.8901367188 -0.0002918475
enter eror:0.0000000001
enter first value :-1
enter next value :2
this equation is not root in this interval.
i a b c f1
1.000000 -1.0000000000 2.0000000000 1.0000000000 -0.4161468365
2.000000 -1.0000 0.50000 0.50000 1.2903
3.000000 -1.0000 -0.25000 -0.25000 1.8151
4.000000 -1.0000 -0.62500 -0.62500 0.9247
5.000000 -1.0000 -0.81250 -0.81250 0.2857
6.000000 -0.9063 -0.81250 -0.90625 -0.0606
7.000000 -0.9063 -0.85938 -0.85938 0.1141
8.000000 -0.9063 -0.88281 -0.88281 0.0270
9.000000 -0.8945 -0.88281 -0.89453 -0.0167
10.000000 -0.8945 -0.88867 -0.88867 0.0052
11.000000 -0.8916 -0.88867 -0.89160 -0.0058
12.000000 -0.8901 -0.88867 -0.89014 -0.0003
13.000000 -0.8901 -0.88940 -0.88940 0.0024
14.000000 -0.8901 -0.88977 -0.88977 0.0011
15.000000 -0.8901 -0.88995 -0.88995 0.0004
16.000000 -0.8901 -0.89005 -0.89005 0.0001
17.000000 -0.8901 -0.89005 -0.89009 -0.0001
18.000000 -0.8901 -0.89005 -0.89007 -0.0000
19.000000 -0.8901 -0.89006 -0.89006 0.0000
20.000000 -0.8901 -0.89006 -0.89006 -0.0000
21.000000 -0.8901 -0.89006 -0.89006 -0.0000
22.000000 -0.8901 -0.89006 -0.89006 0.0000
23.000000 -0.8901 -0.89006 -0.89006 -0.0000
24.000000 -0.8901 -0.89006 -0.89006 0.0000
25.000000 -0.8901 -0.89006 -0.89006 0.0000
26.000000 -0.8901 -0.89006 -0.89006 -0.0000
27.000000 -0.8901 -0.89006 -0.89006 -0.0000
28.000000 -0.8901 -0.89006 -0.89006 0.0000
29.000000 -0.8901 -0.89006 -0.89006 0.0000
30.000000 -0.8901 -0.89006 -0.89006 -0.0000
31.000000 -0.8901 -0.89006 -0.89006 -0.0000
32.000000 -0.8901 -0.89006 -0.89006 -0.0000
33.000000 -0.8901 -0.89006 -0.89006 0.0000
34.000000 -0.8901 -0.89006 -0.89006 0.0000
35.000000 -0.8901 -0.89006 -0.89006 -0.0000
36.000000 -0.8901 -0.89006 -0.89006 -0.0000
tekrar rishe fc
37.0 -0.8900586117 -0.0000000001
ezplot('cos(2*x)')
hold on
ezplot('x^2-1')
hold off
enter eror:0.00001
enter first value :0
enter next value :2
i a b c f1
1.000000 0.0000000000 2.0000000000 1.0000000000 2.0000000000
2.000000 0.0000 1.00000 1.00000 -0.4161
3.000000 0.5000 1.00000 0.50000 1.2903
4.000000 0.7500 1.00000 0.75000 0.5082
5.000000 0.8750 1.00000 0.87500 0.0561
6.000000 0.8750 0.93750 0.93750 -0.1784
7.000000 0.8750 0.90625 0.90625 -0.0606
8.000000 0.8750 0.89063 0.89063 -0.0021
9.000000 0.8828 0.89063 0.88281 0.0270
10.000000 0.8867 0.89063 0.88672 0.0125
11.000000 0.8887 0.89063 0.88867 0.0052
12.000000 0.8896 0.89063 0.88965 0.0015
13.000000 0.8896 0.89014 0.89014 -0.0003
14.000000 0.8899 0.89014 0.88989 0.0006
15.000000 0.8900 0.89014 0.89001 0.0002
16.000000 0.8900 0.89008 0.89008 -0.0001
17.000000 0.8900 0.89008 0.89005 0.0001
18.000000 0.8900 0.89006 0.89006 -0.0000
tekrar rishe fc
19.000000 0.8900604248 -0.0000067746