## Plot error, vector must be same length

Asked by Marc Martinez

### Marc Martinez (view profile)

on 17 Mar 2019 at 13:57
Latest activity Edited by Stephan

### Stephan (view profile)

on 17 Mar 2019 at 14:38
Accepted Answer by Stephan

### Stephan (view profile)

%
h = 0.08;
nmax = 800;
wx = zeros(1, nmax);
wy = wx;
wz = wx;
t = 0:pi/100:2*pi;
%
M = 3.14;
a = 3;
b = 2;
c = 1;
wx(1,1) = 1;
wy(1,1) = 1;
wz(1,1) = 1;
%
I1 = M * (b^2 + c^2)/5;
I2 = M * (a^2 + c^2)/5;
I3 = M * (a^2 + b^2)/5;
%
g1 = (I3 - I2)/I1;
g2 = (I1 - I3)/I2;
g3 = (I2 - I1)/I3;
%
for n = 1:nmax-1
kx1 = -h*g1*wy(n)*wz(n);
ky1 = -h*g2*wx(n)*wz(n);
kz1 = -h*g3*wx(n)*wy(n);
%
kx2 = -h*g1*(wy(n)+0.5*ky1)*(wz(n)+0.5*kz1);
ky2 = -h*g2*(wx(n)+0.5*kx1)*(wz(n)+0.5*kz1);
kz2 = -h*g3*(wx(n)+0.5*kx1)*(wy(n)+0.5*ky1);
%
wx(n+1) = wx(n) + kx2;
wy(n+1) = wy(n) + ky2;
wz(n+1) = wz(n) + kz2;
end
%
plot(t, wy, t, wz);
I am doing this section of code in matlab and i am encountering the following error:
Error using plot
Vectors must be the same length.
I want t to define the x axis grid.

### Stephan (view profile)

on 17 Mar 2019 at 14:37
Edited by Stephan

### Stephan (view profile)

on 17 Mar 2019 at 14:38

Hi,
make t same size then the vectors you want to plot:
%
h = 0.08;
nmax = 800;
wx = zeros(1, nmax);
wy = wx;
wz = wx;
t = linspace(0,2*pi,800);
%
M = 3.14;
a = 3;
b = 2;
c = 1;
wx(1,1) = 1;
wy(1,1) = 1;
wz(1,1) = 1;
%
I1 = M * (b^2 + c^2)/5;
I2 = M * (a^2 + c^2)/5;
I3 = M * (a^2 + b^2)/5;
%
g1 = (I3 - I2)/I1;
g2 = (I1 - I3)/I2;
g3 = (I2 - I1)/I3;
%
for n = 1:nmax-1
kx1 = -h*g1*wy(n)*wz(n);
ky1 = -h*g2*wx(n)*wz(n);
kz1 = -h*g3*wx(n)*wy(n);
%
kx2 = -h*g1*(wy(n)+0.5*ky1)*(wz(n)+0.5*kz1);
ky2 = -h*g2*(wx(n)+0.5*kx1)*(wz(n)+0.5*kz1);
kz2 = -h*g3*(wx(n)+0.5*kx1)*(wy(n)+0.5*ky1);
%
wx(n+1) = wx(n) + kx2;
wy(n+1) = wy(n) + ky2;
wz(n+1) = wz(n) + kz2;
end
%
plot(t, wy, t, wz);
then it wil work: Best regards
Stephan