tried bisection method opinions ?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!