finding the number of interations of newtons method

3 Ansichten (letzte 30 Tage)
Zachary Pride
Zachary Pride am 21 Nov. 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

Antworten (1)

Harsha Priya Daggubati
Harsha Priya Daggubati am 26 Dez. 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!

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by