MATLAB: Solve ode system with ode45

MATLABodeode45

Hello everyone, I would like to solve a system of differential equations using ode45, but I don't know how to proceed :
  • d^2 (x)/dt^2 = a * (d(x)/dt – d(y)/dt) + b * x^3
  • d^2 (y)/dt^2 = c * (d(y)/dt – d(x)/dt) + b * y^3
of course my system is much more complicated. Thanks a lot of your help.

Best Answer

So, basically the solution can be implemented as follows
%parameters
A=1;
B=1;
C=1;
%time interval and initial conditions
t_interval = [0,10];
init_cond = [0,0,0,0]';
%solution
[t,y] = ode45(@(t,Y) odefcn(t,Y,A,B,C) , t_interval , init_cond);
%plot
plot(t,y(:,1),'b',t,y(:,2),'r');
where your system of equations transformed into ode by sobstitution x'=z, y'=w, and the order of variables is Y=[x,y,z,w]:
function dYdt = odefcn(t,Y,A,B,C)
dYdt = [ Y(3);
Y(4);
A*(Y(3)-Y(4)) + B*Y(1)^3;
C*(Y(4)-Y(3)) + B*Y(2)^3];
end
Of course, you will notice that starting by null conditions (init_cond=[0,0,0,0]) the solution you get is x(t)=y(t)=0 for each t. Because they solve the equation. Change initial conditions according to your original problem.
Related Question