MATLAB: Ode45 Set of 3 second order ODE not solving correctly

2nd order odedifferential equationsMATLABodeode45oder

Attempting to solve 3 2nd order ODE's (broken down into 6 1st orders here) using ode45. My answer does not appear to come out correctly and am not sure what I am doing wrong.
CODE:
tspan=[0 60]; %Time Boundaries
x0=[0 0 0 0 0 0]; %Inital Values of disp and vel
[t,x]=ode45(@output,tspan, x0) %Implement ode45 solver
plot(t,x)
xlabel('time')
ylabel('displacement/velocity')
title('Problem 25.18')
legend('Displacement','Velocity')
function dxdt=output(t,x) %Create system of equations
m1=60;
m2=70;
m3=80;
k1=50;
k2=100;
k3=50;
g=9.81;
dxdt=zeros(6,1);
dxdt(1)=x(1);
dxdt(2)=x(2);
dxdt(3)=x(3);
dxdt(4)=g+(k2/m1)*(x(2)-x(1))-(k1/m1)*x(1);
dxdt(5)=g+(k3/m2)*(x(3)-x(2))+(k2/m2)*(x(1)-x(2));
dxdt(6)=g+(k3/m3)*(x(2)-x(3));
end

Best Answer

Hi William,
you are close on this. x1,x2,x3 are postions and x4,x5,x6 are velocities so the appropriate lines of code are
dxdt(1)=x(4);
dxdt(2)=x(5);
dxdt(3)=x(6);
in which case you get oscillations.
g is positive which makes positive x in the direction of down. That's all right as long as you acknowledge the fact in a picture or something. g = -9.81 would put positive x up which is more common.