フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by