Why do i only get 1 of 2 roots of this function when using Newtons method? What am i missing?

3 ビュー (過去 30 日間)
Revend Mustafa
Revend Mustafa 2021 年 9 月 8 日
回答済み: Sanju 2024 年 5 月 3 日
disp("Newton")
x = 0;
t = 1;
format short e
disp("x f(x) fprim(x) korr kvad linje")
while abs(t)>8e-8
f = 51.*x-((x.^2+x+0.03)/2.*x+1).^7-17.*x.*exp(-1.*x);
fp = 51 - 7.*((x.^2+x+0.3).^6).*(2.*x.^2+2.*x+0.4)/((2.*x+1).^8)-17.*(exp(-1.*x)-exp(-1.*x));
g=t;
t=f/fp;
kvad = t/g^2; linj = t/g;
disp([x f fp t kvad linj]);
x = x-t;
end
rot = x;
  1 件のコメント
David Hill
David Hill 2021 年 9 月 8 日
Make sure your equations for f and fp are correct. If f is correct, then fp is wrong.
((x.^2+x+.03)/2.*x+1).^7 = (.5*x.^3+.5*x.^2+.015*x+1).^7
%it seems like you want something like:
((x.^2+x+.03)./(2*x+1))^7
%but I am not sure

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

回答 (1 件)

Sanju
Sanju 2024 年 5 月 3 日
The issue with your code is that it only updates the value of x once in each iteration of the while loop, resulting in finding only one root of the function. To find both roots, x needs to be updated twice in each iteration.
Here's the modified code,
x = x-2*t; % Update x twice
Here, x is updated twice in each iteration to ensure Newton's method converges to both roots. By updating x twice, it explores both directions from the initial guess, converging to the nearest root on each side.
Hope this helps!

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by