Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Not able to stop value of iterations
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
This is my code. I get my x value correctly, and stops there. The if condition is not iterating and I'm not able to find the root or the number of iterations.
Can anybody please clear me about this?
function [x,i]=newton(R,f,Re,x0,maxiter,tol)
for i=1:maxiter
x=x0-(colebrook(R,f,Re)/colebrook_deriv(R,f,Re))
if (x-x0)<tol
break
disp(['root= ',num2str(x)])
end
fprintf('The number of iterations is: %d\n',i)
end
0 comentarios
Respuestas (1)
Alan Stevens
el 27 de Nov. de 2020
Editada: Alan Stevens
el 27 de Nov. de 2020
Better as something like
tol = 10^-8; % or whatever you desire
maxiter = 100; % ditto
err = 1;
its = 0;
while err>tol && its<maxiter
x=x0-(colebrook(R,f,Re)/colebrook_deriv(R,f,Re));
err = abs(x-x0);
x0 = x;
its = its+1;
end
disp(['root= ',num2str(x)])
fprintf('The number of iterations is: %d\n',its)
and, come to think of it, shouldn't colebrook and colebrook_deriv be functions of x, or, alternatively, shouldn't x and x0 be f and f0?
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!