I cannot the error in the bisection method...

조회 수: 3 (최근 30일)
llucia
llucia 2023년 4월 6일
댓글: llucia 2023년 4월 6일
Hi! I am trying to detect why my code doesn´t work properly.
This is the code.
f = @(x) x^3 + 2*x^2 + 10*x - 20;
a = 0;
b = 1;
eps_f = 10e-10;
eps_x = 10e-10;
maxits = 100;
[x, n, error] = biseccion(f,a,b,eps_x, eps_f, maxits)
function [x,n,error] = biseccion(f,a,b,eps_x, eps_f, maxits);
x0 = a;
x1 = b;
n = 0;
error (1) = abs(x1-x0);
while n < maxits && (error(n+1) > eps_x || abs(f(x0)) > eps_f)
x = 0.5 * (x0 + x1);
if f(x0)*f(x) < 0
x1 = x;
else
x0 = x;
end
n = n+1;
error(n+1) = abs(x1-x0);
end
end

채택된 답변

John D'Errico
John D'Errico 2023년 4월 6일
편집: John D'Errico 2023년 4월 6일
Get used to using the dotted operators for some things. That allows MATLAB to use vectorization in tools like fplot.
f = @(x) x.^3 + 2*x.^2 + 10*x - 20;
a = 0;
b = 1;
But the big problem is in f, and your limits.
fplot(f,[a,b])
grid on
Do you see a zero crossing in that interval? I don't.
Bisection CANNOT find a root in an interval where no root exists.
If you expand the interval, you might find it works better.
fplot(f,[0,2])
grid on
yline(0,'r')
A basic rule of using MATLAB is to plot EVERYTHING. Then plot something else. Then think about what you see. Do all of this before you just run code blindly.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by