我有以下matlab代码,它使用false位置的方法来查找任何给定多项式的根。在这种情况下,多项式由Horner方法评估。
% Find a roots using the method of false position.
% The initial interval is [a,b]
% and the iteration stops when b-a<tol.
function c=falseposition(a,b,tolerance,cons,n)
format long
fa = horner(cons,n,a);
fb = horner(cons,n,b);
if fa==0
c=a; return
end
if fb==0
c=b; return
end
c=0.0;
newc=1.0;
while (newc-c)>tolerance
fa = horner(cons,n,a);
fb = horner(cons,n,b);
c = (fa.*b - fb.*a)/(fa - fb);
fc = horner(cons,n,c);
if fc==0
return;
end
if fa*fc<0
b=c;
else
a=c;
end
newc = (b.*fa - a.*fb)/(fa-fb);
end
哪里
% Horner's Method Implementation.
% The given polynomial is evaluated by the function horner.
% For example to evaluate 1+ 3*x^2 -8*x^4 we write the array to
% passed as cons=[1 0 3 0 -8].
function y=horner(constants,sizee,xx)
format long
y=constants(sizee);
for i=(sizee-1):-1:1
y=(y.*xx) + constants(i);
end;
我打电话给
a=[8 -5 -2 1]
falseposition(-3, 2, 1.e-6, a, 4)
但是它给了我一个解决方案。
答案 0 :(得分:1)
fa*fb<0
。newc
。而你的while循环应该以{{1}}