フィルターのクリア

Conclude Variables for an Input Argument

1 回表示 (過去 30 日間)
Marlon
Marlon 2023 年 5 月 26 日
回答済み: Abhijeet 2023 年 5 月 30 日
Hey guys,
So i got following function:
function dxdt = odefcn(x, m, Fvy, Fhy, Fvx, Fhx, Fwx,Fwy, Mav, Mah, Mbv, Mbh, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat,t)
Is there a way to apply a callname to the odefcn for easier handling and more clearness.
When I use it as an Input argument in
[t,x] = ode45(@(t,x) odefcn(x, m, Fvy, Fhy, Fvx, Fhx, Fwx,Fwy, Mav, Mah, Mbv, Mbh, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat,t),tspan,x);
I don't want to write down every variable again. Maybe theres even a way to define a line range, in which every variable should be used?
Thanks in advance!
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 5 月 26 日
Are the variables other than x constant? If so, then you can define them inside the ODE function.
Stephen23
Stephen23 2023 年 5 月 26 日
編集済み: Stephen23 2023 年 5 月 26 日
More than a handful of positional arguments should generally be avoided. Rather than doing that, use a structure:
S.hello = pi;
S.world = Inf;
[t,x] = ode45(@(t,x) odefcn(t,x,S), tspan, x0);
And within the function, refer to its fields.

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

回答 (1 件)

Abhijeet
Abhijeet 2023 年 5 月 30 日
Hi,
Unfortunately there is no way to define a line range for the variables to be used. Every necessary variable should be explicitly included in the function definition.
However, you can create structures for similar params that are needed and then use the same in your function call, and within the function you can reference the variables that you need.
%Define the funtion with fewer arguments by grouping
function dxdt = odefcn(x, m, F, M, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat,t)
% Call the function with the modified arguments
[t,x] = ode45(@(t,x) odefcn(x, m, F, M, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat,t),tspan,x)
For example,
You can group the F & M params together and reference their variables inside the function.
% You can reference the variables in the structure inside your function
% like this
Fvy = F.vy;
Fhy = F.hy;
Fvx = F.vx;
Fhx = F.hx;
Fwx = F.wx;
Fwy = F.wy;
Mav = M.av;
Mah = M.ah;
Mbv = M.bv;
Mbh = M.bh;
This can help you enhance the readability and make the code more structured.
Thanks

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by