Undefined function or variable 'fr' and 'xm'

1 次查看(过去 30 天)
Hello, I am trying to do this exercise of finding a root by using the bisection method and the false position method. But, when compiling, it keeps saying this error. Can someone help me?
the code:
clear all clc
f =@(x)(1.67*cos(40.0))-(2.5*cos(x))+(1.83-cos((40.0-x)));
a0=input('Entre com a primeira aproximação a0:');
b0=input('Entre com a segunda aproximação b0:');
tol=input('Entre com o valor da tolerancia:');
while (abs(b0-a0)>tol)
xm =(a0+b0)/2;
if((f(a0)*f(xm)<0))
b0=xm
else
a0=xm
end
end
fprintf("A Raiz da equação pelo método da bissecção é= %f\n", xm);
%Regula Falsi
f1 = f(a0)
f2= f(b0)
while (abs(b0-a0)>tol)
fr = (((a0)*f1)-((b0)*f2))/(f2-f1);
y=f(fr);
if((f1*y>0))
a0=fr;
f1=y;
else
b0=fr;
f2=y;
end
end
fprintf("%f",fr);

回答(1 个)

James Tursa
James Tursa 2017-9-22
编辑:James Tursa 2017-9-22
Your bisection method changes a0 and b0, which are then fed into your false position method as the starting point. You need to remember the starting point and make sure you feed that same starting point into both methods.
Also, it a0 and b0 are really good guesses and pass the tol test immediately, you never set xm or fr.

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by