Why is my Muller Methods program fail?

1 回表示 (過去 30 日間)
Fauzanul Ardhi
Fauzanul Ardhi 2022 年 2 月 10 日
コメント済み: Fauzanul Ardhi 2022 年 2 月 13 日
i tried to cunstruct muller method program for polinomial x^3-x^2+2*x-2 using initial guest x0=1.3 x1=1.5 and x2=2. When i try using excell, i get approriate result, x=1. Unfortunately, when i use my mathlab program, the result is different nor the expected roots of the polinomial. i hope someone can help me and explain whats wrong with my program.
From the deepest part of my heart, i'm kindly express my highest gratitude for your cooperation.
x=[1.3 1.5 2];
p=[1 -1 2 -2];
y=polyval(p,x);
es=0.0001;
ea=10;
while ea>es
h0=x(2)-x(1);
h1=x(3)-x(2);
delta0=(y(2)-y(1))/h0;
delta1=(y(3)-y(1))/h1;
a=(delta1-delta0)/h1+h0;
b=a*h1+delta1;
c=y(3);
rad=sqrt(b^2-4*a*c);
if abs(b+rad)>abs(b-rad)
den=b+rad;
else
den=b-rad;
end
x3=x(3)+(-2*c)/den;
ea=abs((x3-x(3))/x3);
x(1)=x(2);
x(2)=x(3);
x(3)=x3;
end
x3

採用された回答

David Hill
David Hill 2022 年 2 月 10 日
Not sure if you are doing divided differences correctly.
x=[1.3 1.5 2];%input x values
p=[1 -1 2 -2];%input polynomial
y=polyval(p,x);
es=1e-10;
ea=10;
c=3;
while ea>es
w=div_diff(p,x(c-2:c-1))+div_diff(p,x([c-2,c]))-div_diff(p,x(c-1:c));
Rad=sqrt(w^2-4*y(c)*div_diff(p,x(c-2:c)));
Div=[w+Rad,w-Rad];
[~,idx]=max(abs(Div));
Div=Div(idx);
x(c+1)=x(c)-2*y(c)/Div;
y(c+1)=polyval(p,x(c+1));
ea=abs((x(c+1)-x(c))/x(c));
c=c+1;
end
Anser=x(end);
function y=div_diff(p,x)
if length(x)==2
y=diff(polyval(p,x))/diff(x);
else
y=div_diff(p,x(2:end))-div_diff(p,x(1:2))/(x(2)-x(1));
end
end
  1 件のコメント
Fauzanul Ardhi
Fauzanul Ardhi 2022 年 2 月 13 日
ooh, i see. i think its also because i evaluate y before while loop, so its only give value for the initial guest. thanks a lot, sir.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by