Output from a function as an argument to another function
조회 수: 1 (최근 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
댓글 수: 2
채택된 답변
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...)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!