MATLAB Answers

How to solve the second-oder differential equations with two variables using ODE45 or whatever

6 views (last 30 days)
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 its 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);

Accepted Answer

Bjorn Gustavsson
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

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by