finding the number of interations of newtons method

1 visualización (últimos 30 días)
Zachary Pride
Zachary Pride el 21 de Nov. de 2019
Respondida: Harsha Priya Daggubati el 26 de Dic. de 2019
I'm having a problem trying to find how many interations the following function has to go through to get the root
function [root,count]=NewtonMethod(c0,c,x)
count=1;
fx1=poly_val(c0,c,x)
fx2=poly_val(c0,c,x +0.001)
m=(fx2-fx1)/(0.001)
newGuess= x-((fx1/m))
if fx1 < (1*10^-8)
fx1=poly_val(c0,c,newGuess)
fx2=poly_val(c0,c,newGuess+0.001)
m=(fx2-fx1)/(0.001)
newGuess=newGuess-((fx1)/(m))
fx1=poly_val(c0,c,newGuess)
end
root=newGuess
count=count+1
fprintf('number of times is = %i \n' , count)
the input is NewtonMethod(0,[5 1 -6 0 1],2)
and the output is 2.0943 and the count is 2 but should be 6

Respuestas (1)

Harsha Priya Daggubati
Harsha Priya Daggubati el 26 de Dic. de 2019
Hi,
I guess the count should be incremented in the place where fx1 is being compared and it should be conditioned in a loop to get the number of iterations.
function [root,count]=NewtonMethod(c0,c,x)
count=0;
fx1 = poly_val(c0,c,x);
fx2 = poly_val(c0,c,x +0.001);
m =(fx2-fx1)/(0.001);
newGuess = x-((fx1/m));
while fx1 < (1*10^-8)
fx1 = poly_val(c0,c,newGuess);
fx2 = poly_val(c0,c,newGuess+0.001);
m = (fx2-fx1)/(0.001);
newGuess = newGuess-((fx1)/(m));
fx1 = poly_val(c0,c,newGuess);
count = count+1;
end
root = newGuess;
fprintf('number of times is = %i \n' , count);
end
Hope this works!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by