So i wrote a homework using the bisection method ,code functions, some opinions on if it's correct ? if not what should be improved/changed ?
Create a script file for calculating the necesary iterations necesary for finding the solution of the equation below by the half-life /bisection method interval with an error of 1e-10. Consider [-10,10] as initial solution search interval.
x3 – x2 – sin(x)-1=0
a = -10
b = 10
er ≤ 1e-10
%% Input Data
% Ecuation x^3-x^2-sin(x)-1
% Interval [-10 , 10]
% Error 1e^-10
%% Bloc de calcul
x1=input('value for x1: ');
x2=input('value for x2: ');
y=@(x) x^3-x^2-sin(x)-1;
if y(x1)*y(x2)>0
fprintf('nu roots exist for the given interval \n');
return
end
if y(x1)==0
fprintf('x1 is one of the roots \n')
return
elseif y(x2)==0
fprintf('x2 is one of the roots \n')
return
end
for i=1:100
f=(x1+x2)/2 % bisection
if y(x1)*y(x2)< 0
x2=f;
else
x1=f;
end
if abs(y(x1))< 10^-10
break
end
end