How to end function when condition is met
Show older comments
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 Comments
Walter Roberson
on 9 Nov 2020
Edited: Walter Roberson
on 9 Nov 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
on 9 Nov 2020
David Hill
on 9 Nov 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
on 9 Nov 2020
Edited: Marcel Langner
on 9 Nov 2020
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!