Clear Filters
Clear Filters

Conclude Variables for an Input Argument

1 view (last 30 days)
Marlon
Marlon on 26 May 2023
Answered: Abhijeet on 30 May 2023
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 Comments
Dyuman Joshi
Dyuman Joshi on 26 May 2023
Are the variables other than x constant? If so, then you can define them inside the ODE function.
Stephen23
Stephen23 on 26 May 2023
Edited: Stephen23 on 26 May 2023
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.

Sign in to comment.

Answers (1)

Abhijeet
Abhijeet on 30 May 2023
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

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!