blob: 4951ec9c5f34c9574ed8cbbf822d4d9d82642bb5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//example 2.1
//bisection method
//page 24
clc;clear;close;
deff('y=f(x)','y=x^3-x-1');
x1=1,x2=2;//f(1) is negative and f(2) is positive
d=0.0001;//for accuracy of root
c=1;
printf('Succesive approximations \t x1\t \tx2\t \tm\t \tf(m)\n');
while abs(x1-x2)>d
m=(x1+x2)/2;
printf(' \t%f\t%f\t%f\t%f\n',x1,x2,m,f(m));
if f(m)*f(x1)>0
x1=m;
else
x2=m;
end
c=c+1;// to count number of iterations
end
printf('the solution of equation after %i iteration is %g',c,m)
|