Output from a function as an argument to another function

3 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2018-11-1
编辑:dpb 2018-11-1
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 个评论
Shengyue Shan
Shengyue Shan 2018-11-2
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by