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

1 vue (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 1 Déc 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)
Stephan
Stephan le 1 Déc 2020
Thank you.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 1 Déc 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 commentaires
Zhichao Shen
Zhichao Shen le 1 Déc 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 le 2 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Zhichao Shen
Zhichao Shen le 2 Déc 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

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by