Output from a function as an argument to another function

1 view (last 30 days)
Hello, I am trying to use the output from a function as the input to another function. As shown below, X0,Xb,Tf,X3,X4,X5 are all scalar that has been defined. u is the result pulled out from the solver. I wrote function ICN and function WF in independent .m files, and I need to solve for c.
function X1 = ICN(u,X0,Xb,Tf)
if u <= Tf
X1 = (X0-Xb)*(1-Tf/u);
else
X1 = 0;
end
function X2 = WF(u,X0,Xb,Tf)
if u <= Tf
X2 = Xb +(X0-Xb)*Tf/u;
else
X2 = X0
end
function c = SH(u,X1,X2,X3,X4,X5)
c1 = 1*u;
c2 = 2*u;
c3 = 3*u;
c4 = 4*u;
c5 = 5*u;
c = c1*X1 + c2*X2 + c3*X3 + c4*X4 + c5*X5;
end
I tried writing it as
c = @(~,state)SH(state.u,X1,X2,X3,X4,X5)
But it does not seem to be right. How to write this correctly? I'd appreciate any help! Thank you very much!
Best regards, Shengyue
  2 Comments
TADA
TADA on 1 Nov 2018
are you trying to pass c as input to ICN and WF or the other way arround?
Shengyue Shan
Shengyue Shan on 1 Nov 2018
No, pass X1 and X2 as input to SH. I just figured it out. Thanks!

Sign in to comment.

Accepted Answer

dpb
dpb on 1 Nov 2018
Edited: dpb on 1 Nov 2018
In main script or calling function
...
u=...
X0=...
X3=...
X4=...
X5=...
Xb=...
Tf=...
X1=ICN(u,X0,Xb,Tf)
X2=WF(u,X0,Xb,Tf)
c=SH(u,X1,X2,X3,X4,X5)
NB: Function
function c = SH(u,X)
c=cumprod((1:5)*u,X);
end
if you would not write independent variables for X1 thru X5 but use an array (it's the MATLAB way...)
  1 Comment
Shengyue Shan
Shengyue Shan on 2 Nov 2018
Hello dpb, Thank you very much for your answer! I have another way for the subscript, but thank you for the suggestions! I will try it for the future; ) Best regards, Shengyue Shan

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!