Plot error, vector must be same length

1 view (last 30 days)
%
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.

Accepted Answer

Stephan
Stephan on 17 Mar 2019
Edited: Stephan on 17 Mar 2019
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:
plot_t.PNG
Best regards
Stephan

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!