Script zero_with_while.m
Download Script zero_with_while.m
%Part a
x=1:0.1:3;
f=x.^3-2.*x-5;
plot(x,f),grid on,title('f ( x ) = x^3 - 2 x - 5');
xlabel('x'); ylabel ('f ( x )')

%Part b
check=[10^-4 10^-6 10^-8 10^-10];
for k=1:4

x1 = 2.0;
x2 = 2.2;
j=0;

while (x2-x1) > check(k)
j=j+1;
x0 = (x1+x2)/2;
fx = x0^3-2*x0-5;
if sign(fx) < 0
x1 = x0;
else
x2 = x0;
end

end
n_loops(1,k)=j;
solution(1,k)=x0;

end
n_loops
solution
Back