How to end function when condition is met
Mostrar comentarios más antiguos
I havent used Matlab for a while and was never really good at it. I need your help
How to terminate the function, as soon as NL reaches the number 1?
I think i have to put the 'if' and 'return' above the function but then Matlab tells me, that it doesnt know NL yet.
What am I doing wrong?
function NL = NLDestillationsblase(x, x0, Omega1)
NL = (x/x0).^(1./(Omega1-1)).*((1-x0)./(1-x)).^(Omega1./(Omega1-1))
if NL >= 1
return
end
end
4 comentarios
Walter Roberson
el 9 de Nov. de 2020
Editada: Walter Roberson
el 9 de Nov. de 2020
return is fine for this purpose. There is no obvious error in your code... though clearly you would be wanting to loop.
Generally speaking if you are looping, it is cleaner to break the loop and just let the code reach the end of the function instead of returning.
Marcel Langner
el 9 de Nov. de 2020
David Hill
el 9 de Nov. de 2020
That is because x is an array.
function NL = NLDestillationsblase(x, x0, Omega1)
NL = (x/x0).^(1./(Omega1-1)).*((1-x0)./(1-x)).^(Omega1./(Omega1-1))
NL=NL(NL<1);
end
Marcel Langner
el 9 de Nov. de 2020
Editada: Marcel Langner
el 9 de Nov. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!