# How to solve two coupled differential equations using ode45.

176 views (last 30 days)
Musanna Galib on 18 Oct 2020
Commented: haohaoxuexi1 on 12 Aug 2022
I need help to solve two paired differential equations. After hours of unsuccessful attemps, I am asking for help from you. Any suggestion will be highly appriciated.
The equations are -
m*x''(t) + U*x(t) + γ*x'(t) + kv*V(t) - ζ = 0
V'(t) - kc*x'(t) + τ*V(t) = 0
initial conditions are x(0) = 1, X'(0) = 0, V(0) =0
x and V are the variables. All others are constant.

Alan Stevens on 19 Oct 2020
The following should help:
% tspan = [0 tend];
% IC = [1 0 0]; % initial conditions
% [t, X] = ode45(@fn, tspan, IC);
% x = X(:,1);
% z = X(:,2);
% V = X(:,3);
%
% function dXdt = fn(t,X)
% x = X(1); z = X(2); V = X(3);
% constants = ... list them with their values
% dXdt = [z;
% -(y*z+U*x+kv*V)/m;
% kc*z-tau*V];
% end
haohaoxuexi1 on 19 Jan 2022
dx(1)=(k1*x(3))/(knl_1+3*knl_3*x(1)^2+k1);
dx(2)=x(3);
dx(3)=(-c*x(3)-k1*(x(2)-x(1))+kAmp*cos(w*t)+theta_p*x(4))/m;
dx(4)=(-theta_p*(kspring/(kpiezo+kspring))*(x(3)-((k1*x(3))/(knl_1+3*knl_3*x(1)^2+k1)))*R_s-x(4))/R_s/C_p;
I changed the code to the above way and removed the mass matrix anymore, which follow your suggestions, and it worked with ode45.
I want to know if there is any logical mistake in my previous code? Why is my previous code is not working properly by treating the problem to be a DAE.
Also I noticed you wrote the fna fnb as separate function in your code instead of putting them all in dYdt function? Is there specific reason for you to do it this way? Like it can help calculating faster or it is just your habit?
Thank you for your help anyway.

Alan Stevens on 20 Jan 2022
"Why is my previous code is not working properly by treating the problem to be a DAE."
I don't know!
"Also I noticed you wrote the fna fnb as separate function in your code instead of putting them all in dYdt function? Is there specific reason for you to do it this way? Like it can help calculating faster or it is just your habit?"
You could put them inside the dYdt function here if you like. I tend to define them outside in case I need to call them separately outside of dYdt.
haohaoxuexi1 on 12 Aug 2022
Is there any way to solve it analytically? Or any sample code can be shared

### Categories

Find more on General Applications 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!

Translated by