finding the number of interations of newtons method

조회 수: 3 (최근 30일)
Zachary Pride
Zachary Pride 2019년 11월 21일
답변: Harsha Priya Daggubati 2019년 12월 26일
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

답변 (1개)

Harsha Priya Daggubati
Harsha Priya Daggubati 2019년 12월 26일
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!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by