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

조회 수: 2 (최근 30일)
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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by