How to do iteration until certain tolerance is achieve?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Abba Alhaji Bala
el 30 de Jul. de 2021
Comentada: Abba Alhaji Bala
el 31 de Jul. de 2021
Hello! i wanted to iterate a function with an initial value to get a new value and then keep subtituting the new value back to the function, this process will continue until when the tolerance condition is achieved. i tried using "while loop" but it shows no iteration. No any new value obtained. please help me to check the code.
This is the equation: (psic/psit) + 1/(2*sigma) = 1
clear, clc;
psit=0.1; % initial
tol=1.0e-6;
sigma=0.8;
psic=-0.2079;
k=0; % iteration
while abs((psic/psit)+(1/(2*sigma)))<tol
psit=((psic/psit)+(1/(2*sigma)))-1
k=k+1;
end
disp(psit)
0 comentarios
Respuesta aceptada
Walter Roberson
el 30 de Jul. de 2021
while abs((psic/psit)+(1/(2*sigma)))<tol
That tells us that you are looking for (psic/psit)+(1/(2*sigma)) to become as close to 0 as possible, which is the same as solving
psic/psit + 1/(2*sigma) == 0
psic + psit/(2*sigma) == 0*psit
psit/(2*sigma) == -psic
psit == -psic*2*sigma %solution
but...
psit=((psic/psit)+(1/(2*sigma)))-1
notice the -1 . That -1 tells us that you want to solve for psic/psit + 1/(2*sigma) == 1 instead of psic/psit + 1/(2*sigma) == 0
The contradiction between those leads the equation to loop endlessly (once you change the < tol to > tol )
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!