MATLAB: How to plug the symbol vector into function.m file for ode solving

MATLABodeode45

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.

Best Answer

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().
Related Question