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

3 views (last 30 days)
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 Comment
David Hill
David Hill on 8 Sep 2021
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

Sign in to comment.

Answers (1)

Sanju
Sanju on 3 May 2024
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!

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!