How to end function when condition is met

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
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
Marcel Langner el 9 de Nov. de 2020
Thanks!
Hmm, then I dont know what is happening with my code.
So what Im doing is Im giving 'x' into the function that is:
x = 0:0.01:1;
The function should stop once NL reaches 1 but for that code its not the case. The function spills out as much values as I put in from my variable x (= 100). But I dont want all 100 values.
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
Marcel Langner el 9 de Nov. de 2020
Editada: Marcel Langner el 9 de Nov. de 2020
Thanks, could you explain what this is about?
NL=NL(NL<1);

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2020
x = 0:0.01:1;
NL = (x/x0).^(1./(Omega1-1)).*((1-x0)./(1-x)).^(Omega1./(Omega1-1))
Because x is a vector, and you are doing element-by-element operations, unless one of the other variables is not a scalar or row vector, then NL would be a row vector. NL is not going to be a scalar, because x is not a scalar and you are not doing any reduction operations such as sum()
if NL >= 1
In MATLAB, when you test a condition using if or while, the condition is considered true if all of the values being tested are non-zero. NL >= 1 with NL non-scalar is going to evaluate to a logical matrix the same sizes as NL. The test would then be considered to be true if all of the NL values are greater than or equal to 1. It is as-if you had written
if all(NL >= 1)
if you want the condition to be true as soon as at least one of the tests is satisfied, then write in any(), as in
if any(NL >= 1)
But you should be considering whether you need to loop over the x values, like
NL = zeros(size(x));
for xidx = 1 : numel(x)
NL(xidx) = (x(xidx)/x0).^(1./(Omega1-1)).*((1-x0)./(1-x(xidx))).^(Omega1./(Omega1-1));
if NL(xidx) >= 1
NL = NL(1:xidx);
return
end

3 comentarios

Marcel Langner
Marcel Langner el 9 de Nov. de 2020
Thanks a lot. I remember this again, now that you tell me. I dont want to test all of my NL values. I just wanted to test them one by one and stop once i reached NL >= 1.
So I guess I need to create a zero matrix first for NL according to the number of values I put in from the array x. Did I understand that correct?
Walter Roberson
Walter Roberson el 9 de Nov. de 2020
You do not need to pre-allocate, and if you do not do so then you do not need the NL(1:xidx) to trim off extra values.
However, pre-allocating is noticably more efficient to store into.
Marcel Langner
Marcel Langner el 9 de Nov. de 2020
ok, thanks a lot :-)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Nov. de 2020

Comentada:

el 9 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by