finding the number of interations of newtons method
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!