Solving System of Differential Equations with Multiple Variables using ode45
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chad
am 24 Jun. 2020
Kommentiert: Star Strider
am 9 Feb. 2021
Lets say I have a system of six differential equations with multiple variables:
dA = E - C + B - D
dB = F - D + A - C
.....
dF = - B + D - F
How can I write a function which is solvable with ode45? I want a 1x6 initial condition input and a 1x6 output over a time span.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 24 Jun. 2020
If you assign ‘A(t)’ as ‘x(1)’ ... ‘F(t)’ as ‘x(6)’, then an anonymous function version would create this matrix:
dfcn = @(t,x) [x(5) - x(3) + x(2) + x(4)
x(6) - x(4) + x(1) - x(3)
. . .
-x(2) + x(4) + x(6)];
and then call ode45 with ‘dfcn’ and the appropriate initial conditions and time span.
The function gets a bit more complicated if involves derivatives of the functions on the right hand side. For that, I usually use the Symbolic Math Toolbox to create the equations, then odeToVectorField to create them as first-order equations, and matlabFunction to convert them to a system that ode45 can use. (Sure, I could do them by hand and then spend a bit of time sorting my algebra errors, but there is no reason to do that.)
2 Kommentare
Anna Jacobsen
am 9 Feb. 2021
This looks like a great solution! I am trying to solve a similar problem. Could you clarify what you mean by assigning A(t) to x(1)? Would A(t) be the differential equation dA/dt?
Star Strider
am 9 Feb. 2021
Anna Jacobsen — Thank you!
‘Could you clarify what you mean by assigning A(t) to x(1)?’
Here
is the first equation, ao ‘A(t)’ gets assigned as ‘x(1)’, with the other variables assigned in order, so ‘B(t)’ is ‘x(2)’ and similarly for the others.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/514202/image.png)
‘Would A(t) be the differential equation dA/dt?’
The derivatives are implicitly on the left-hand-side of the equation, so if this were not an anonymous function (and instead a function file), the code for
would be:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/514207/image.png)
dxdt(:,1) = x(5) - x(3) + x(2) + x(4);
and similarly for the others.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!