필터 지우기
필터 지우기

Output from a function as an argument to another function

조회 수: 3 (최근 30일)
Shengyue Shan
Shengyue Shan 2018년 11월 1일
댓글: Shengyue Shan 2018년 11월 2일
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
TADA
TADA 2018년 11월 1일
are you trying to pass c as input to ICN and WF or the other way arround?
Shengyue Shan
Shengyue Shan 2018년 11월 1일
No, pass X1 and X2 as input to SH. I just figured it out. Thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

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