Input struct in function

When I try to run a function that needs constants defined in a struct outside of it, it give the error "Not enough input arguments.". How can I give as input in the function the data struct?
data.fluid.rho = 890;
data.accumulator.V_N2 = 10e-3;
data.accumulator.P_N2 = 2.5e6;
data.accumulator.p0 = 21e6;
data.accumulator.gamma = 1.2;
data.delivery.D23 = 1;
function dydt = problem(t,y,data)
A23 = (data.delivery.D23^2/4)*pi;
end
This is just an example with some line of codes, I get an error on A23, not enough input arguments. I suspect the function doesnt know what data.delivery.D23 is even if its written outside, how can I pass the data structure in the function?

3 件のコメント

Stephen23
Stephen23 2021 年 10 月 31 日
編集済み: Stephen23 2021 年 10 月 31 日
"how can I pass the data structure in the function?"
t_in = ..
y_in = ..
data = ..
out = problem(t_in,y_in,data)
Note that your function returns one output argument dydt which is not defined anywhere inside the function.
Matt J
Matt J 2021 年 10 月 31 日
The error is not caused by anything in the code you've shown us. It is caused by the code where problem() is actually invoked.
Nader Mohamed
Nader Mohamed 2021 年 11 月 1 日
編集済み: Nader Mohamed 2021 年 11 月 1 日
Yes you're right. I'm trying to call the function in an ODE15s.
options = odeset('RelTol',1e-9,'AbsTol',1e-9,'Events',@event);
[tt,yy] = ode15s(@problem,[t0,tf],[0,0,1e-3,data.accumulator.Vf],options);
I don't understand what's the form to say to the ode solver to grab the function problem with the inputs data.
P.S This is just an example with some parts of the full code, I just want to understand how to make ode15s know to grab the data inputs of problem

サインインしてコメントする。

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 10 月 31 日

0 投票

data.fluid.rho = 890;
data.accumulator.V_N2 = 10e-3;
data.accumulator.P_N2 = 2.5e6;
data.accumulator.p0 = 21e6;
data.accumulator.gamma = 1.2;
data.delivery.D23 = 1;
% A few things are missing:
t = ... % initialize t
y = ... % initialize y
function dydt = problem(t,y,data)
A23 = (data.delivery.D23^2/4)*pi;
dydt = ... % Define dydt
end

2 件のコメント

Nader Mohamed
Nader Mohamed 2021 年 11 月 1 日
Hello! I know some parts are missing! I just wanted to understand how to call the data parameters inside the function when i run an ode solver like
options = odeset('RelTol',1e-9,'AbsTol',1e-9,'Events',@event);
[tt,yy] = ode15s(@problem,[t0,tf],[0,0,1e-3,data.accumulator.Vf],options);
How can I make ode15s understand that it needs to grab @ problem with the data parameters?
Walter Roberson
Walter Roberson 2021 年 11 月 1 日
options = odeset('RelTol',1e-9,'AbsTol',1e-9,'Events',@event);
[tt,yy] = ode15s(@(t,y)problem(t,y,data),[t0,tf],[0,0,1e-3,data.accumulator.Vf],options);

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2018b

質問済み:

2021 年 10 月 31 日

コメント済み:

2021 年 11 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by