How can I plug the symbol vector into function.m file for ode solving?
Show older comments
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
Stephan
on 1 Dec 2020
Can you give an example on how the input looks like and what you expect to get out? Is it always the same system of ode's with just other coefficients?
Zhichao Shen
on 1 Dec 2020
Stephan
on 1 Dec 2020
If the shown equations already are the result of your coding, then the way stated in Ameer's answer is the correct one. Additionally you will need
matlabFunction
to get the result of
odeToVectorField
suitable for
ode45
.
Walter Roberson
on 1 Dec 2020
Not matlabFunction: use the workflow for odeFunction()
Hi Walter, whats the advantage of odeFunction? I only know it from usage with algebraic systems from the documentation.
In the doc for odeToVectorField they use matlabFunction:
Can you tell more?
Zhichao Shen
on 1 Dec 2020
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)
Stephan
on 1 Dec 2020
Thank you.
Accepted Answer
More Answers (1)
Zhichao Shen
on 2 Dec 2020
0 votes
Categories
Find more on Numeric Solvers 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!