Newton-Rahpson Method
Show older comments
Design a function that calculates the roots of a polynomial using the NewtonRaphson method with input parameters: f(the function),df(derivative of [f]),x0(initial point),maxIter(maximum number of iterations) & rTol(Error) and output:(flag,x,numIter), where flag=1 if the error in the iteration [while loop] is as follows and 0 otherwise:
I'm not getting a correct approximation, and the flag is set to 1 which is also incorrect
function [flag,x,numIter] = Newton1D(f,df,x0,maxIter,rTol)
k=0; %Variable para salir del while loop
iter=1; %Variable para almacenar el numero de iteraciones en los arreglos
x=x0 %Pasos para evaluar una funcion en un valor para una funcion simbolica
numIter=1;
f=subs(f,x0); %Evaluar la funcion f simbolica original en el valor x0
df=subs(df,x0); %Evaluar la derivada de la funcion f en el punto x0
iter= iter+1;
for j=1:maxIter
a(1,1)=x;
j=j+1;
w=j-1;
a(j,1)= a(w,1)- (f)/(df);
while(k<=maxIter && w<=maxIter)
if (abs(a(j,1)-a(w,1)))<(abs(a(w,1))*rTol)
flag=1;
x=a(j,1);
numIter= iter-1;
k= maxIter+1;
j=maxIter;
else
x=a(j,1);
f=subs(f,x);
df=subs(df,x);
iter= iter+1;
numIter= iter-1;
k= k+1;
j=j-1;
flag=0;
end
end
end
flag
x
numIter
end
Answers (0)
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!