How do i change this " while loop " to a for loop while still arriving to the same end product?

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

답변 (1개)

Roger Stafford
Roger Stafford 2018년 2월 18일
편집: Roger Stafford 2018년 2월 18일
....
for i = 1:maxiter
if abs(h) <= tol
break
end
....

댓글 수: 2

that doesnt really help me change it to a for loop but thankyou
Sorry. I have corrected it to be a for-loop.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 2월 18일

편집:

2018년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by