MATLAB: Solve system of ODEs MATLAB with ode45

MATLABodeode45

I have 4 variables y1, y2,y3,y4 and I know their second time derivatives as functions of y1, y2, y3, y4 (displacements and their accelerations):
y1"= y1(y1,y2,y3,y4)
y2"= y1(y1,y2,y3,y4)
y3"= y1(y1,y2,y3,y4)
y4"= y1(y1,y2,y3,y4)
I am trying to use ode45 to get y1, y2,y3, y4
I tried making the ODE first order by using 4 more variables so:
y5= y1', y6= y2', y7= y3', y8= y4'
so
y5'= y1", y6'= y2", y7'=y3", y8'= y4"
So for my MATLAB code:
function dydt=myequ(t,y)
g = 9.81; m=0.5 ; k = 1000; l0 = 0.02;
dydt(1)=y(5);
dydt(2)=y(6);
dydt(3)=y(7);
dydt(4)=y(8);
A = (y(2)-y(1)).*(1-(l0./(sqrt((y(2)-y(1)).^2+(y(3)-y(4)).^2))));
B = (y(4)-y(3)).*(1-(l0./(sqrt((y(2)-y(1)).^2+(y(3)-y(4)).^2))));
dydt(5)= [y(5); (k.*A)./m];
dydt(6)= [y(6);-(k.*A)./m] ;
dydt(7)= [y(7);(k.*B - m*g)./m] ;
dydt(8)= [y(8);(-k.*B - m*g)./m] ;
And
function stuffff
l0 = 0.02; v0=1;
tspan=[0 .144];
y0=[-0.5*l0 0 0 0 0.5*l0 v0/sqrt(2) 0 v0/sqrt(2)]; % Initial conditions
[t,y]=ode45(@myequ,tspan,y0)
Im getting the error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
I was using this as a reference
but I changed dy to dydt because I am taking time derivative.

Best Answer

dydt(5)= [y(5); (k.*A)./m];
attempts to store two values, y(5) and a calculated value, in the single location dydt(5) . I suspect you just want
dydt(5)= (k.*A)./m;
dydt(6)= -(k.*A)./m ;
dydt(7)= (k.*B - m*g)./m ;
dydt(8)= (-k.*B - m*g)./m ;