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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by