tried bisection method opinions ?

1 次查看(过去 30 天)
Opariuc Andrei
Opariuc Andrei 2020-11-12
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 个评论
John D'Errico
John D'Errico 2020-11-12
编辑:John D'Errico 2020-11-12
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by