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

2 vues (au cours des 30 derniers jours)
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

Réponses (1)

Roger Stafford
Roger Stafford le 18 Fév 2018
Modifié(e) : Roger Stafford le 18 Fév 2018
....
for i = 1:maxiter
if abs(h) <= tol
break
end
....
  2 commentaires
Raiven Balderas
Raiven Balderas le 18 Fév 2018
that doesnt really help me change it to a for loop but thankyou
Roger Stafford
Roger Stafford le 18 Fév 2018
Sorry. I have corrected it to be a for-loop.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by