tried bisection method opinions ?

1 view (last 30 days)
Opariuc Andrei
Opariuc Andrei on 12 Nov 2020
Edited: John D'Errico on 12 Nov 2020
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
  1 Comment
John D'Errico
John D'Errico on 12 Nov 2020
Edited: John D'Errico on 12 Nov 2020
What is the problem? Did it find a solution? if so, then why does anything need to be changed? Do we really need to run your code, assuming that you already ran it?
To be honest, it does not look to have been written properly, since the text you ue for deciding whoch way to asplit the interval is wrong. But this is why you need to learn to use the debugger.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!