MATLAB: Help me with ode45 please

clear all, close all, clc
comp=3;;
global a1 a2 lc1 lc2 lc3 m1 m2 m3 I1 I2 I3 gc d1 d2
global KP KD comp gfin
global qd1 qd2 qd3 ts ta
a1=1; a2=1; a3=1;
lc1=0.4; lc2=0.4; lc3=0.4; m1=30; m2=20; m3=10; I1=3; I2=2; I3=1; gc=9.8;
% d1=50; d2=50;
d1=0; d2=0;
qd1=[pi/4; pi/4];
qd2=[pi/3; pi/2];
qd3=[pi/2; 0];
q0=[-pi/4; pi/4];
qp0=[0; 0];
dt=0.01;
Tfin=15;
ts=5;
KP=400*eye(2); KD=50*eye(2);
switch comp
case 0
tipocontrollo='PD senza compensazione della gravità';
case 1
tipocontrollo='PD con compensazione esatta della gravità';
case 2
tipocontrollo='PD con compensazione nel punto finale';
case 3
tipocontrollo='Computed Torque';
end
Tsim=0:dt:Tfin;
Nsim=max(size(Tsim));
x0=[q0; qp0];
[T,Xout]=ode45('robot3R',Tsim,x0);
N=max(size(T));
q1sim=Xout(:,1); q2sim=Xout(:,2); q3sim=Xout(:,3)
qp1sim=Xout(:,4); qp2sim=Xout(:,5); qp3sim=Xout(:,6)
if ts>=Tfin
tt=Tfin;
else
tt=ts;
end
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in robot3R_control_main (line 86)
[T,Xout]=ode45('robot3R',Tsim,x0);
It keeps giving me this kind of errors but I can't understand where i have to work on, can anyone help me please?
P.S. The code can have several errors in its writing, i just started working on it.

Best Answer

Your q0 and qp0 are each vectors of length 2, for a total of 4, but inside robot3D you extract from x as if they were each of length 3.
You have a bunch of variables that you assume are length 2, but your ode needs to be length 3.
This is made worse by your use of global variables, making it less obvious what connection there is between initialization and sizes for calculations.
I made some changes to calculate something in a consistent manner. I have no idea whether it is calculating the right thing -- probably not. But it will perhaps make it easier for you to advance.
No message was observed about syntax problems.... just about inconsistent matrix sizes, and subscript out of range, and so on.