Reputation: 3
I'm relatively new to Matlab and am trying to make a .avi file of several curves (Legendre polynomials) being traced out with a red dot tracing out each curve. I've done this before with a single set of parametric equations and linspace, and it seemed to work fine. I would think that the code would essentially be the same for what I'm wanting to do here, but I keep getting the following error message each time I try to run the program:
??? Index exceeds matrix dimensions.
Error in ==> legpolymov at 25
plot(x(1:2*i),y0(1:2*i));
My code is listed below. Is there something I'm doing wrong or just missing, or is there maybe a better/more efficient way of doing what I'm trying to do?
Thanks!
clc
clear all;
close all;
delete legpolymov.txt
diary legpolymov.txt
figure
hold all
t=linspace(-2,2,500);
x = t;
y0 = 1;
y1 = t;
y2 = (3/2)*t.^2 - (1/2);
y3 = (5/2)*t.^3 - ((3/2)*t);
y4 = (3/8) - ((15/4)*t.^2) + ((35/8)*t.^4);
y5 = ((15/8)*t) - ((35/4)*t.^3) + ((63/8)*t.^5);
plot(x,y0)
plot(x,y1)
plot(x,y2)
plot(x,y3)
plot(x,y4)
plot(x,y5)
axis equal
M=moviein(50);
for i=1:50
plot(x(1:2*i),y0(1:2*i));
plot(x(1:2*i),y1(1:2*i));
plot(x(1:2*i),y2(1:2*i));
plot(x(1:2*i),y3(1:2*i));
plot(x(1:2*i),y4(1:2*i));
plot(x(1:2*i),y5(1:2*i));
hold all
plot(x(2*i),y0(2*i),'k*');
plot(x(2*i),y1(2*i),'k*');
plot(x(2*i),y2(2*i),'k*');
plot(x(2*i),y3(2*i),'k*');
plot(x(2*i),y4(2*i),'k*');
plot(x(2*i),y5(2*i),'k*');
axis([-2 2 -1 1])
a = texlabel('alpha');
title('Legendre Polynomials for (1-x^2)y''''-2xy''+ \alpha * (\alpha +1) = 0');
xlabel('x-axis')
ylabel('y-axis')
M(i)=getframe;
hold off
end
movie(M)
movie2avi(M,'Legendre1','FPS',7)
diary off
Upvotes: 0
Views: 115
Reputation: 104464
The reason why is because your y0
variable is only a single value, yet the other values of y1,...y5
are arrays. As such, you're indexing y0
in your loop in such a way that you're assuming it's an array when it's not.
To ensure that the sizes are consistent when you're running this code, you need to make sure that y0
is a 1 x 500
array as well. As such, you need to replace your y0
statement with:
y0 = ones(1,500);
Your code should run after doing this replacement.
I just ran this code on my end, and it doesn't seem to be plotting properly using what you have above in its exact form, except for the y0
correction I made. In order to get this to run, I suggest you make the following changes:
hold all;
statement at the beginning of the for
loop. This way all of the points, including your curves, get plotted. Take it out from the middle of the for
loop and put it at the beginning.You have 500 data points, and your for
loop is plotting the data in multiples of 2. As such, you should increase your for
loop limits from 1:50
to 1:250
. In other words:
for i = 1 : 250
%// Insert code here
end
Once I made these changes, I got your code to work.
Upvotes: 1