Newton Raphson Method!!!

1 view (last 30 days)
alexp10
alexp10 on 5 May 2020
Commented: alexp10 on 5 May 2020
Hi,
I'm trying to run the Newton Raphson method for 3 different initial values. 5 iterations for each value. I'm trying to get results stored as r1,r2,r3. So far only r1 looks ok but the other 2 are 0 which is wrong. Any help would be appreciated.
[r1,r2,r3] = newton();
function [r1,r2,r3] = newton()
r1= 0;
r2=0;
r3=0;
r = [r1 , r2 , r3];
x0= [0.5,1.5,2.5];
x= [ 0 , 0 , 0 ];
i=0;
j=1:3;
for j= 1:3;
f = x0(j)^4 - 9*x0(j)^3+29*x0(j)^2-39*x0(j)+18;
df = 4*x0(j)^3 - 27*x0(j)^2 +58*x0(j) -39;
while i < 5
i= i+1;
x(j) = x(j) - f/df;
x0(j) = x(j);
disp (r1);
if i==5
r(j)=x0 (j);
disp('root is');
disp(r);
break
end
end
end
end

Accepted Answer

David Hill
David Hill on 5 May 2020
Edited: David Hill on 5 May 2020
function x = newton()
x=[0.5,1.5,2.5];
f=[1,-9,29,-39,18];
df=polyder(f);
for j= 1:5
x = x - polyval(f,x)./polyval(df,x);
end
  4 Comments
David Hill
David Hill on 5 May 2020
The loop is performing all the calculations at once doing element-wise calculations of the arrays.
alexp10
alexp10 on 5 May 2020
Thankyou

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!