Write code to approximate3 Squareroot 25 by applying the a b
Solution
On the work space
%Bisection method
clear;
f=input(\'enter a function of x\')
x0=input(\'enter x0\')
x1=input(\'enter x1\')
x=0;
if f(x0)*f(x1)>0
error (\'choose another nterval\')
else
while abs(f(x))>0.00001
x=(x0+x1)/2
if f(x0)*f(x)>0
x0=x;
else x1=x;
end
end
end
On command window
>> krishna
 enter a function of x@(x)x.^3-25
f =
@(x)x.^3-25
enter x02
x0 =
2
enter x13
x1 =
3
 x =
2.5000
 x =
2.7500
 x =
2.8750
 x =
2.9375
 x =
2.9063
 x =
2.9219
 x =
2.9297
 x =
2.9258
 x =
2.9238
 x =
2.9248
 x =
2.9243
 x =
2.9241
 x =
2.9240
 x =
2.9240
 x =
2.9240
 x =
2.9240
 x =
2.9240
 x =
2.9240
 x =
2.9240
 x =
2.9240
>>
Falsi method:
On work space
%Regula falsi method
clear;
f=input(\'enter a function of x\')
x0=input(\'enter x0\')
x1=input(\'enter x1\')
x=0;
if f(x0)*f(x1)>0
error (\'choose another nterval\')
else
while abs(f(x))>0.00001
x=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0))
if f(x0)*f(x)>0
x0=x;
else x1=x;
end
end
end
On command window
>> jj
 enter a function of x@(x)x.^3-25
f =
@(x)x.^3-25
enter x02
x0 =
2
enter x13
x1 =
3
 x =
2.8947
 x =
2.9233
 x =
2.9240
 x =
2.9240
 x =
2.9240
>>




