# Calling max value of variable from another function

9 views (last 30 days)
jack carter on 13 Aug 2017
Commented: jack carter on 18 Aug 2017
I have made a function named "example.m" in which I have created two variables "x" and "y" both have certain range.
function [x,y,T] = example(v,sigma,G,N,K)
x=0:0.01:2;
y=(G*N^2*x/sigma)-(K+v);
end
Now in another function, named "loadvalues.m" I need to call the maximum value of "y" along with the corresponding value of "x" both of them are loaded in another variable named "loady" and "loadx" respectively. Can someone please tell me how to do this?
jack carter on 13 Aug 2017
Edited: jack carter on 13 Aug 2017
In addition, I would like to ask that, suppose in example.m I have used "if else condition" as given below;
u=K/N;
w=2.5;
if u<w
T=v*sqrt(sigma+G^2/(N-K);
else
T=v*sqrt(sigma+G^2/(N+K);
end
Now in loadvalues.m function, how can I call the value of "T" when (u>w) only. Not both (u<w) and (u>w).

dpb on 13 Aug 2017
The y in the function example has nothing to do with the y from the load y command so the question is unclear as to why you're referring to the latter...unless there's code not shown which SAVE d the x and y after you ran the function and you used the intermediary from the disk to get the output of the function later on instead of just saving the results of running the function itself???
If that's the case, IA's Answer is spot, but why not save the intermediary step and write
[x,y,T] = example(v,sigma,G,N,K);
[maxy,iy]=max(y);
maxx=x(iy);
?
As to the second question, there is only one value of T returned and it will correspond to whichever condition is T in the IF block; the other branch will never be evaluated. If it's important as to which condition is so, you could add another output which is a flag variable that has the value of true/false or 0/1 or -1/+1, whatever set of values you wish to indicate that condition.
jack carter on 18 Aug 2017
I will be assigning the resultant values of T to two different variables, say P1 and P2. In P1 I will be needing all of the outputs of T with u ranging from (0:0.01:5) by solving both equations i.e. when (u<2.5) as well as when (u>2.5).
For P2, I only need those values of T which are after the else statement i.e. only when u>2.5

Image Analyst on 13 Aug 2017
Try this