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
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
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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!