Substituing symbolic variable with the element of a matrix before using ode solver

2 views (last 30 days)
So, I have this explicit ODE system (named Y):
Y =
(0.5*(1.95e290*cos(sin(3.0*t))^2*cos(sin(t))*sin(3.0*t) + [...] + 3... Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display.
-(0.5*(8.58e290*cos(sin(3.0*t))^2*sin(sin(t))*sin(3.0*t) + [...] + 4.16e253*cos(sin(3.0*t))*sin(sin(3.0*t))^4*cos(sin(t))^2*sin(sin(t))... Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display.
-(0.5*(2.13e295*cos(sin(3.0*t))^2*cos(sin(t))^2*sin(t) + [...] +3.98e260*cos(sin(3.0*t))^2*sin(sin(3.0*t))^3*cos(sin(t))*sin(sin(t))^4*sin(30.0*t)^3*sin(t) + 4.16e2 ... Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display.
in which each equation of the equations systems depends of 4 variables, which are the time "t", "theta1p(t)", "theta2p(t)" and "theta3p(t)". Now, as you can see, the equations of this system are kinda long and I need to find a way to substitute the thetaXp by a y(X) so I can use this system of equation with the ODE solver function ode45. That means that in Y above, I need to substitute:
theta1p with y(1)
theta2p with y(2)
theta3p with y(3)
So that I may be able to do:
tspan=[0 10]
initcond=[0 ; 0 ; 0]
[t,y45]=ode45(Y,tspan,initcond)
I already tried doing substitutions such as
[t,y45]=ode45(@(t,y) (subs(Y,[theta1p ; theta2p ; theta3p],[y(1) ; y(2) ; y(3)])),tspan,initcond)
but it doesn't seem to work. If it's of any help, I've joined my code to this post and I identified the section where I'm stuck by a series of "HERE" that are hard to miss (it's near the end of the code)

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jun 2016
Use matlabFunction with the 'Vars' option that gives a vector of variable names within a cell array, rather than a vector of variable names. Any names specified in a vector inside a cell array are bundled into a vector rather than as individual inputs.
For example,
f = matlabFunction(x^2+y,'Vars', {t, [x,y]})
To be more complete: the items are put into an array and the appropriate column of the array is pulled at, so the generated function can be used as vectorized by putting different input value combinations in different rows. The ode* routines pass inputs as a column vector, but the generated code will expect the inputs (for any one case) as a row vector. You will need to transpose, so...
f = matlabFunction(Y,'Vars', {t, [theta1p; theta2p; theta3p]} );
[t,y45] = ode45( @(t, y) f(t, y(:)), tspan, initcond)

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!