How to solve the second-oder differential equations with two variables using ODE45 or whatever
8 views (last 30 days)
Show older comments
嵩人 藤森
on 4 May 2021
Commented: Bjorn Gustavsson
on 6 May 2021
Hello,
I'm trying to solve the following equations using ODE45 but I'm not confident that my codes work correct. So please teach me whether the codes are correct or not, and if not, teach me how I can analyze the equation numerically.
----—the equation———
mx’’+Dx’+Kx=-f
Tf’ + f = Gx
%parameters
m=3
D=2
K=1
T=1
G=5
%variables
x, f
———my codes———
% I know ode45 only can solve one oder differential equations so I did a change of variables. However I would rather avoid using this change because it’s hard to see x and f in a plotted graph with the new variable appearing on it.
m=3
D=2
K=1
T=1
G=5
% I did the following change [X=x’] so the equation becomes[ X=x’ ][mX’+DX+Kx=-f][Tf’ + f = Gx]
%F(1) matches x, F(2) matches X, F(3) matches f
% x(0)=0.5
[t,F]=ode45(@(t,F)[F(2);(-F(3)-D*F(2)-K*F(1))/m;(G*F(1)+F(3))/T],[0,1],[0.5,0,0]);
plot(t,F);
0 Comments
Accepted Answer
Bjorn Gustavsson
on 4 May 2021
To the best of my "reading-ability" you seem to have it right in ODE-defining function. From your equations I expect them to be something like this:
% mx’’+Dx’+Kx=-f
% Tf' + f = Gx
% From which we define our 3 first order ODEs as:
% dxdt = v;
% dvdt = -D/m*v-K/m*x-f/m
% dfdt = -f/T + G/T*x
% Which we can put into a dynamical function:
ODExvf = @(t,y,m,D,K,T,G) [y(2);
-D/m*y(2)-K/m*y(1)-y(3)/m;
-y(3)/T + G/T*y(1)];
m=3;
D=2;
K=1;
T=1;
G=5;
[t,F]=ode45(@(t,y) ODExvf(t,y,m,D,K,T,G),[0,1],[0.5,0,0]);
plot(t,F);
It might be preferable to define the ODE more "easily human-readable", and sometimes it's neat to have the opportunity to modify the parameters without having to redefine the function.
HTH
6 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!