Matlab continue graph for second loop - matlab

%free fall of a ball
clc
clear all
close all
v0=5; % initial velocity up
g=9.8; %free fall acceleration
v1=(0.7/0.9)*v0
% time of fly
tup=v0/9;
nsteps=10; %number of frames
dt=tup/nsteps; %time step
Hmax=v0*tup+(-g)*tup*tup/2; % maximum altitude
altitude(1:nsteps+1)=0; %define array for position Y
time=0:dt:tup;% define time array
%initilaise plot
figure(1)
axis([0,2*tup,0,2*Hmax]);
hold on
% loop
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%loop bouncing
for i=1:nsteps
altitude(i)=v1*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%make movie
movie(M);
movie2avi(M, 'C:\Users\Mehmet\Desktop\avi\mm','compression','none');
%extra plots
figure(2)
plot(time(1:nsteps),altitude(1:nsteps))
figure(3)
plot(time(1:nsteps),altitude(1:nsteps),'ro')
We have this ball bouncing simulation. What we want to do is, to continue loop 2 after loop 1 in graph.So, it will be continious bouncing simulation.2 bouncings are shown from 1:10 steps but we want second loop to be shown after 10 steps.

There are a couple of approaches, depending upon what effect you're going for. The simplest is to simply add hold on as a separate command after each of your plot commands. This will keep accumulating new lines/dots without erasing the old ones. To stop this effect, add a hold off command. The next plot command will then erase everything and plot on a clean graph.
Alternatively, if you only want to show a fixed number of the previous steps (and not all of them, as the hold on will do), you'll have to explicitly hold onto the previous values. Something like this:
% loop
max_history = 10; %plot this many symbols
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
%get the historical and current data points to plot
start_ind = i-max_history+1; %here's the oldest point that you'd like
start_ind = max([start_ind, 1]) %but you can't plot negative indices
inds = [start_ind:i]; %here are the ones to plot
%update the plot
plot(time(inds),altitude(inds), 'ro');
grid on;
%you might need a "drawnow" here
M(i)=getframe;
end
Copy this same idea into your other for loops and you should be good-to-go!

Related

How to animate contour plot in Matlab

I have 32x21 matrix with x axis increment of one parameter (2 to 64 in steps of 2) and y axis increment of n other parameter,in steps of two again. I can plot it as contour plot, but I was wondering if there is a way to have contour plot as animation/movie.Thanks
Did you try to place your plot in a for loop which iterates through all the contours? Use drawnow
e.g.
hold on
for ii = 2:2:64
%do your plotting here
%use "pause" if too fast
drawnow
end
This should update the figure every iteration.
Also see this

Animate through multiple 2D Matlab plots

I have multiple 2D line plots in Matlab (they represent some wave moving through space). Each plot represents the wave at some time t. I want to animate through these plots (i.e. show the first plot for a fraction of a second, then show the next one, and the next, etc. I want to loop back to the beginning once it reaches the end time) to show the time evolution of the system. surf and mesh don't really do what I want since it is too difficult to see the changes with the number of time steps I have. Is there a way to do this in Matlab?
I assume with "2d-line" you mean a 2d-plot. This is done by the plot-function, so there is no need of surf or mesh. Sorry, when I got you wrong.
The following code does what I think you asked for:
% Generate some propagating wave
n = 20;
t = linspace(0,10,100);
x = cell(1,n);
for i = 1:n
x{i} = (1-abs(i/n-0.4))*sin(t+i*0.2);
end
% Create frames
figure;
for i = 1:length(x)
clf;
plot(t,x{i});
ylim([-1,1]);
myFrames(i) = getframe; %#ok<SAGROW>
end
% Show movie
figure;
movie(myFrames,2,2); % frames, repetitions, frames per second

Hold on only the axis, not the data

I want my graph to fixed axes and also plot the data one by one. Everything is already known, however if i use hold off to remove the first set of data, it also forgets the limits on the axes and automatically assigns new limits for the second set of data.
Is it somehow possible to keep the axes the same for each time a separate data piece is plotted in the same figure?
code for now is:
figure(4)
grid on
axis([xL yL zL])
for j = 1:n % n is amount of data sets
for i = 1:2 % two items drawn per data set
*plot data*
hold on
end
%This part has to be done every iteration again in order to make it work now
axis([xL yL zL])
xlabel = ...
ylabel
zlabel
title
pause(tstop)
hold off
end
after some searching the only relevant topic i found was; Matlab: Plot a subplot with hold on and hold off in a loop without always calling xlabel, ylabel, xlim, etc
However i do not understand it at all. It uses a parent figure, replacechildren, nextplot and such which i am not familiar with and also cant find much information about.
Here's an example that can be easily adapted to your needs:
xlim([0 10]) %// set x-axis limits
ylim([0 10]) %// set y-axis limits
set(gca,'nextplot','replacechildren') %// prevent axis limits from changing with
%// each new plot
plot(3:8,3:8); %// note axis limits are kept as [0 10]
pause(1)
plot(5:7,5:7); %// note axis limits are kept as [0 10]

Matlab: replace one plot maintaining others

I have a figure in which I plot some disperse points and then a trajectory. I want to switch between different trajectories by plotting them in the same figure as the points, but without creating new figures, i.e., "erasing" the first trajectory and then plotting the new one.
Is there a way of doing this?
Perhaps this little demo will be helpful:
xy = rand(20,2);
figure
% Plot first iteration and output handles to each
h = plot(xy(:,1),xy(:,2),'b.',xy(1:2,1),xy(1:2,2),'r-');
axis([0 1 0 1])
% Update second plot by setting the XData and YData properties of the handle
for i = 2:size(xy,1)-1
set(h(2),{'XData','YData'},{xy(i:i+1,1),xy(i:i+1,2)})
drawnow
pause(0.1);
end
You should read up on handle graphics in Matlab and the get and set functions.

Matlab change a single point in a plot without replotting

So I have a plot of N points in the 2D plane (N can be very large). I am writing a script that is to show the workings of an algorithm. So I have for loops. At each step in the for loop I'd like to change the color of the current point (actually probably make a stem plot with just this point).
However, at the end of the step I'd like to remove the coloring of the current point so that I can color the next one. Currently I have to redraw the whole plot (incl. the 2D points). I'm not sure whether Matlab detects such things in the plotting commands but is there a way to do this without redrawing the whole plot?
For example:
plot(x,y, '*');
for j = 1:N-1
for i = j:N
hold on;
%Do stuff
plot(x,y, '*');
hold on;
stem(x(1), y(1), 'g*');
end
end
A quick example:
%# plot some data
x = 1:100;
y = cumsum(rand(size(x))-0.5);
plot(x,y,'*-')
%# animate by going through each point
hold on
h = stem(x(1),y(1),'g');
hold off
for i=1:numel(x)
%# update the stem x/y data
set(h, 'XData',x(i), 'YData',y(i));
%# slow down a bit, drawnow was too fast!
pause(.1)
end
Take a look at the documentation of handle graphics objects.
I'd recommend plotting the whole set of points as one object. Then, for each iteration, plot the point of interest. Save a handle to it (as in, h = plot(...);). When you're ready for the next iteration, delete the object using that handle (delete(h)), and create the next one in the same manner.
%# outside the for loop (do this once)
plot(x,y,'*');
for...
h = stem(x(i),y(i),'g*');
...
%# next iteration... i has been incremented
delete(h);
h = stem(x(i),y(i),'g*');
end