How do you animate using plots with a fixed marker?

2 visualizaciones (últimos 30 días)
Chace
Chace el 5 de Sept. de 2014
Editada: Geoff Hayes el 5 de Sept. de 2014
I am trying to simulate a pendulum. It keeps saying that my x and y for my first markers are undefined. And I need them to plot each individual point (500 plots) simultaneously. My problem is with marker 1 and 2.
clear; length= 1; %pendulum length in meters g=9.8; % acceleration due to gravity npoints = 500; %Discretize time into 250 intervals dt = 0.01; % time step in seconds
l = 1; m = 5; g= 9.81; r = 0.5;
% I = -(1/3)*(m*(l^2)); % theta2 = ((1/I)*(m*g*r*sin(theta(1)))); % %clear;
length= 1; % pendulum length in metres g=9.8; % acceleration due to gravity npoints = 500; % Discretize time into 250 intervals dt = 0.01; % time step in seconds omega = zeros(npoints,1); % initializes omega, a vector of dimension npoints X 1,to being all zeros theta = zeros(npoints,1); % initializes theta, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros theta(1)=40; % you need to have some initial displacement, otherwise the pendulum will not swing
for step = 1:npoints-1 % loop over the timesteps
omega(step+1) = omega(step) - (g/length)*theta(step)*dt;
theta(step+1) = theta(step)+omega(step+1)*dt; %note that this line is the only change between % this program and the standard Euler method
time(step+1) = time(step) + dt; end;
%MARKER 1 x(1,1:time*dt)= 0 y(1,1:time*dt)= 0
%MARKER 2 x(2, 1:time*dt)= (cos(theta)) y(2, 1:time*dt)= (-sin(theta))
delay = 0; %increase this number in small increments to slow down animation %this number should likely never exceed 0.2. framerate = 1; % increase (in increments of 1) to speed up animation numofmarkers = size(x,2); % determines how many markers in in data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % plots initial position of data, then waits for key press to animate figure(10); plot([x(1,1:numofmarkers)],[y(1,1:numofmarkers)],'k'); drawnow; xlim([-2 2]); ylim([-2 2]); pause; start=cputime; % animates data by plotting at each instant in time for i=1:framerate:size(x,1), plot([x(i,1:numofmarkers)],[y(i,1:numofmarkers)],'k'); xlim([-2 2]); ylim([-2 2]); drawnow; tic; while toc<delay, end; end; fprintf('animation time = %3.1f sec\n',cputime-start);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % clear extraneous variables clear delay i numofmarkers framerate start
plot(time,theta,'r' ); %plots the numerical solution in red xlabel('time (seconds) '); ylabel('theta (radians)');

Respuestas (1)

Geoff Hayes
Geoff Hayes el 5 de Sept. de 2014
Editada: Geoff Hayes el 5 de Sept. de 2014
Chace - in the future, please format your code (as written in your question) so that it is readable. Highlight that text which is code and press the {} Code button. Or, even better, just attach the code to your question using the paperclip button.
Your code will generate undefined x and y error messages since you do not define these variables anywhere in the code. Presumably theta is the angle of the pendulum relative to the y-axis. So with its initial value of 40 degrees, this means that the pendulum is in the lower-right quadrant of the xy plane (beneath the x-axis and to the right of the y-axis). We can determine the (x,y) coordinate for each position of the pendulum (given its angle relative to the y-axis) using the fact that the length of the "string" is one (which will be the hypotenuse of a triangle)
strLength = 1;
x = cosd(90-theta)*strLength;
y = -cosd(theta)*strLength;
If we want to plot the movement of the pendulum, we can do that as
% create the new figure
figure;
% set the limits on the x- and y-axes (add offsets of 0.02)
xlim([min(x)-0.02 max(x)+0.02]);
ylim([min(y)-0.02 0.02]);
hold on;
% draw the initial position of the pendulum as a green circle (go)
plotHandle = plot(x(1),y(1),'go');
% draw the "string" from the origin to the position of the pendulum
lineHandle = line([0 x(1)],[0 y(1)],'Color',[1 0 0]);
% move the pendulum
for k=2:size(x,1)
% pause the animation for 0.01 seconds
pause(0.01);
% change the x and y data (coordinate) for the pendulum
set(plotHandle,'XData',x(k),'YData',y(k));
% change the "string" so that it swings with the pendulum
set(lineHandle,'XData',[0 x(k)],'YData',[0 y(k)]);
end
You should be able to use the above for your use of markers.
I did notice that you defined a local variable as length. This conflicts with a built-in MATLAB function of the same name, so I suggest you rename your local variable to something else i.e. strLength.

Categorías

Más información sobre Animation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by