How can I plug the symbol vector into function.m file for ode solving?

6 views (last 30 days)
I want to solve the problem where a stage is that I need to use ode23 or ode45 to solve the time-varying variables. For instance, the varibales is nX1 space vector. So I need to build the nX1 differential equations like that
function f = ODE(t,y,Q,R,r)
f =[2*y(1) - 10*exp(-2*t) - 22*y(3) - (y(1)*(y(1)/2 + 25*y(2) - (51*y(3))/2))/(2*(t - 15)) - (25*y(2)*(y(1)/2 + 25*y(2) - (51*y(3))/2))/(t - 15) + (51*y(3)*(y(1)/2 + 25*y(2) - (51*y(3))/2))/(2*(t - 15))
51*y(2) - 60*y(3) - 11*y(6) - (y(1)*(y(2)/2 + 25*y(5) - (51*y(6))/2))/(2*(t - 15)) - (25*y(2)*(y(2)/2 + 25*y(5) - (51*y(6))/2))/(t - 15) + (51*y(3)*(y(2)/2 + 25*y(5) - (51*y(6))/2))/(2*(t - 15))
11*y(3) - 10*y(4) - 11*y(8) - (y(1)*(y(3)/2 + 25*y(6) - (51*y(8))/2))/(2*(t - 15)) - (25*y(2)*(y(3)/2 + 25*y(6) - (51*y(8))/2))/(t - 15) + (51*y(3)*(y(3)/2 + 25*y(6) - (51*y(8))/2))/(2*(t - 15))...]
Traditionally, I can solve it by using [t,y]=ode45(@ODE,tspan,InitialCondition) to find the curve of all of the variables.
My point is: Can I let the differnential equations be automatically solved as soon as I get the symbolic expression of matrix f. I have think about it for serval days already.
Thanks.
  8 Comments
Walter Roberson
Walter Roberson on 1 Dec 2020
When you build up ode equations symbolically you use symbolic functions x(t) and so on. The workflow converts the function and derivatives references into variable references so that the boundary condition vector can be indexed, and odeFunction knows to package in the form of f(t,x, additionals)

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 Dec 2020
Are you manually pasting your symboling expression in the ODE() function? You can use odetovectorfield(): https://www.mathworks.com/help/symbolic/odetovectorfield.html to solve the equations with ode45(). See the example "Solve Higher-Order Differential Equation Numerically" on the documentation page of odetovectorfield().
  13 Comments
Zhichao Shen
Zhichao Shen on 1 Dec 2020
Ameer,
The output of your code is also
2*Y[1]^2
Y[2]^2 + Y[1]
You have let the sequence be changed. But the sequence of the elements y1, y2 no change.
Ameer Hamza
Ameer Hamza on 2 Dec 2020
I don't think there is a good way to change that. But why does this order matter? The solution will still be the same.

Sign in to comment.

More Answers (1)

Zhichao Shen
Zhichao Shen on 2 Dec 2020
Ameer and Walter
Until now, I can say I make a breakthrough of the problem by using odeFunction(dydt,vars) where dydt is a series of ODEs that I derived like I showed originally, then vars is the vector of [y1(t), y2(t), ... yn(t)].
Please keep attention that dydt is set of right hand of ODEs, for instance,
dydt=[y2(t);y1(t)*y2(t)]
,which means y1'(t)=y2(t) and y2'(t)=y1(t)*y2(t) as default setting.
Then we can use ode function to solve the problem for any given inputs and n(variables).
All in all, the main and significant contribution is from Ameer Hamza and Walter Roberson. And I am glad Stephan response to me quickly.
If someone have similar question in the future, please see this summary answer.
Thank you, guys!

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!