How do i change this " while loop " to a for loop while still arriving to the same end product?
1 回表示 (過去 30 日間)
古いコメントを表示
function [r,resarray] = newton(f,df,x0,tol,maxiter)
%f = @(x) x^3-3
%df= @(x) 3*x.^2
%x0= 1
%tol=10^-6
%maxiter= 100
resarray = [];
x=x0;
h = (f(x))/ (df(x));
i = 1;
while(abs(h) > tol & i<=maxiter)
h = f(x)/ df(x);
x = x - h;
resarray = [resarray, x];
i=i+1;
end
r = x;
end
0 件のコメント
回答 (1 件)
Roger Stafford
2018 年 2 月 18 日
編集済み: Roger Stafford
2018 年 2 月 18 日
....
for i = 1:maxiter
if abs(h) <= tol
break
end
....
2 件のコメント
参考
カテゴリ
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!