Calling max value of variable from another function

1 view (last 30 days)
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?
  1 Comment
jack carter
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).

Sign in to comment.

Accepted Answer

dpb
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.
  4 Comments
dpb
dpb on 15 Aug 2017
...
u=0:0.01:5;
w=2.5;
if u<w
...
The if statement above in Matlab does NOT do what you think it does (or what you want); in fact it will never execute the true clause.
In
if == expression
the condition is true iff ALL (real) elements of the expression are each true; the IF block is not a looping construct; it is a simple logic test only.
Also, T will be a vector of length(u) as written.
It's not clear exactly what you do want; do you want a vector T of all values with the evaluation of the expression for each element based on the ratio or only those in the T block or a single value that is the value that's the first location that U<W or what, precisely?
jack carter
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

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Aug 2017
Try this
MaxY = max(loady);

Community Treasure Hunt

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

Start Hunting!