animate plot / trajectory in matlab / octave - matlab

I'm trying to animate this spiral using matlab / octave I want it to spiral up or down
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
plot3 (r.*sin(t), r.*cos(t), z);
I tried using a for loop to animate it but that just gives me a cone shape see code and image below
clear all, clc,clf,tic
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
for ii=1:length(r)
ii
plot3 (r.*sin(t(ii)), r.*cos(t(ii)), z);
hold on
%pause (.00001)
end
Image

You could also use the comet3() package, which animates the trajectory through the plot:
delay = 0.001 % seconds
figure
comet3(r.*sin(t), r.*cos(t), z, delay);
This animates a continuous trajectory which I prefer over a discrete sequence of *'s.
The one downside is that the version of comet and comet3 that shipped with Octave 3.6.4 are slow, regardless of the delay you use. But this can be overcome by using the following trick courtesy of andyras in this SO question:
% plot the first point to get started
h = plot3(x(1),y(1),z(1),"b");
axis([min(x), max(x), min(y), max(y), min(z), max(z)]);
% refresh the plot in a loop through the rest of the data
for k = 1:length(z);
set(h, 'XData', x(1:k));
set(h, 'YData', y(1:k));
set(h, 'ZData', z(1:k));
pause (0.001); % delay in seconds
% alternatively could provide a velocity function
% pause(sqrt(vx(k)^2+vy(k)^2+vz(k)^2));
endfor
Minor note: once you've modified the function, you'll need to force Octave to reload it as it won't do this by default. You can either restart, or better yet use clear comet and clear comet3. Then the next time those functions are called, their definitions will be refreshed.

The following appears to work in Octave 3.6.2
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
figure
axis([-1 1 -1 1 0 1])
hold on
for ii=1:length(r)
plot3 (r(ii)*sin(t(ii)), r(ii)*cos(t(ii)), z(ii),'*');
pause (.001)
end

Certainly not the prettiest, but these are the first changes you need to make to your code for it to do something close to what you want.
t = 0:0.1:10*pi;
z = linspace (1, 0, numel (t));
for ii=1:length(t)
plot3 (z(ii)*sin(t(ii)),z(ii)*cos(t(ii)), z(ii));
hold on
pause (.00001)
end

This is not the trajectory solution, but here is a spinning tornado.
phi = linspace(0, 10*pi, 300);
r = linspace (0, 1, 300);
z = linspace (0, 1, 300);
s = 100; %speed of turning
for t = 0:0.01:10 %t is time
plot3 (r.*sin(phi+t*s), r.*cos(phi+t*s), z);
pause(0.01)
end

Related

How to plot this integral function in MATLAB?

Struggling to figure out how to plot this function in MATLAB.
Thanks, any help would be appreciated!
#Rotem 's approach is very good, yet I would love to add few words. Since you haven't mentioned anything about the domain, I suppose function can be defined also for negative numbers. In that case you can use the alternative as:
t = linspace(-10, 10);
func = arrayfun(#(t) integral(#(x) (10*x.*(343-(x).^3))/50421, 0, min(7,t)), t);
plot(t, func);
I have fixed the x axis between -10 and 10 with linspace, you can manually change that to get what you desire. But, note that function will get a fixed value when t is greater than 7, since integral has a bound such as min(t,7).
I really hope my answer is correct...
The integral of x is from 0 to t, when t goes from 0 to 7:
t = linspace(0, 7);
sigma = arrayfun(#(m) integral(#(x) 10*x.*(343 - x.^3)/50421, 0, m), t);
plot(t, sigma);
%Test using for loop:
% y = zeros(size(t));
%
% for i = 1:length(t);
% y(i) = integral(#(x) 10*x.*(343 - x.^3)/50421, 0, t(i));
% end
%
% figure;plot(t, y);

Matlab animation of several points simultaneously

I am trying to simulate the trajectories of a few particles in 2D on Matlab. I have the x- and y- coordinates of these particles as a function of time, which I store as matrix x and y. The column in both x and y corresponds to the time, while the row corresponds to the particle number: 1, 2, etc.
I know how to do the animation for one particle with pause, but I am not sure how to customize the code for multiple particles' trajectories. Basically, my idea is that on the initial plot, I have 3 markers which correspond to the initial position of the particles, say particle A, B and C. Then, I would like to follow the movement of these 3 markers, and here is where I encountered the problem: I don't know how to sort the subsequent points according to the particle identity. For example, I want to specify the first point I plot in the second time point as particle A, second point as particle B and third point in particle C.
I have done something similar to this, but in my simulation, the number of particles may be 100, which makes it impractical to create x1, x2, ..., x100, y1, y2, ..., y100 for the animation to work:
y = rand(3, 20); % Generate random sample data.
x = rand(size(y, 1), size(y, 2));
% Now we have x and y sample data and we can begin.
% Extract into separate arrays
x1 = sort(x(1,:));
x2 = sort(x(2,:));
x3 = sort(x(3,:));
y1 = y(1,:);
y2 = y(2,:);
y3 = y(3,:);
for k = 1 : length(x1)
plot(x1(1:k), y1(1:k), 'r*-', 'LineWidth', 2);
xlim([min(x(:)), max(x(:))]);
ylim([min(y(:)), max(y(:))]);
grid on;
hold on;
plot(x2(1:k), y2(1:k), 'g*-', 'LineWidth', 2);
plot(x3(1:k), y3(1:k), 'b*-', 'LineWidth', 2);
hold off;
fprintf('Plotted points 1 through %d\n', k);
pause(0.8);
end
Any ideas or suggestions will be greatly appreciated!
In order to plot all graphs at once, we might make an 2D array.
Below is an example.
y = rand(3, 20); % Generate random sample data.
x = rand(size(y, 1), size(y, 2));
% Now we have x and y sample data and we can begin.
% Extract into separate arrays
x = sort(x');
y=y';
M=size(x);
N=M(2);
for k = 1 : length(x)
if k==1;
zeroPad=zeros(1,N);
x0=[zeroPad;x(1,1:N)];
y0=[zeroPad;y(1,1:N)];
plot(x0(1:2,1:N), y0(1:2,1:N), '*', 'LineWidth', 2);
else
plot(x(1:k,1:N), y(1:k,1:N), '*-', 'LineWidth', 2);
end
xlim([min(x(:)), max(x(:))]);
ylim([min(y(:)), max(y(:))]);
grid on;
fprintf('Plotted points 1 through %d\n', k);
pause(0.8);
end
One trick was added.
At the first iteration, I added zeros before x and y.
Some unnecessary codes were removed.

A way of copying a plot and plotting next to existing curve

If I have a curve plotted in an axis is there a way of copying the curve and plotting next to it without running the function.
I.e. I have a code to plot a function from -1 to 0, I dont want the function to be correct from 0 to 1, I simply want to replicate the curve next to it.
Sorry for vague question but Im at a loss here
Using the function sin(x) as an example, you could do this
x_min = -1;
x_max = 0;
x = x_min:0.1:x_max;
y = sin(x);
hold on
plot(x, y, 'LineWidth', 1.2)
plot(x + x_max - x_min, y, 'LineWidth', 1.2)
plot([x_max x_max], [sin(x_min) sin(x_max)], 'k--')
axis equal
to get the following

MATLAB Simple Point Plots

In a while loop, I need to plot the positions (x,y) of two entities. That is, all I need to do is generate a plot with two points on it. I need to scale the plot to a specific maximum x and y value. An additional requirement is the fact that one of the points needs to have three concentric rings placed around it, each with a given radius. Additionally, this all is to happen in a loop, thus I'm hoping that only a single plot window opens and that I don't get a whole slew of windows opening (one for each loop iteration).
Basically here's the pseudo-code I'm trying (and failing!) to implement:
-> Open new plot window, with a given x and y axis
while (running) {
-> Clear the plot, so figure is nice and clean
-> Plot the two points
-> Plot the three circles around point A
}
I found several items in MATLAB's documentation, but no single plotting functions seems to do what I want, or there are instances where I inadvertently create multiple plots with only some of the data (i.e., one plot has the points and another has the circles).
here's a sample code you can use in your while loop
x0=1; y0=4;
x1=2; y1=3; % the x-y points
r=[1 2 3]; % 3 radii of concentrating rings
ang=0:0.01:2*pi;
xc=cos(ang)'*r;
yc=sin(ang)'*r;
plot(x0,y0,'.',x1,y1,'.'); % plot the point A
hold on
plot(x1+xc,y1+yc); % plot the 3 circles
% set the limits of the plots (though Matlab does it for you already)
xlim([min([x0 x1])-max(r) max([x0 x1])+max(r)]);
ylim([min([y0 y1])-max(r) max([y0 y1])+max(r)]);
hold off
you can make this work in a loop quite easily, read matlab's documentation on how to do that.
Try something like this:
r = [0.25 0.125 0.0625];
d = (1:360) / 180 * pi;
xy_circle = [cos(d)' sin(d)'];
xy_circle_1 = r(1) * xy_circle;
xy_circle_2 = r(2) * xy_circle;
xy_circle_3 = r(3) * xy_circle;
h_plot = plot(0, 0, '.k');
hold on
h_circle_1 = plot(xy_circle_1(:, 1), xy_circle_1(:, 2), '-b');
h_circle_2 = plot(xy_circle_2(:, 1), xy_circle_2(:, 2), '-r');
h_circle_3 = plot(xy_circle_3(:, 1), xy_circle_3(:, 2), '-g');
axis equal
for hh = 1:100
xy = rand(2, 2) / 4 + 0.375;
xlim = [0 1];
ylim = [0 1];
set(h_plot, 'XData', xy(:, 1));
set(h_plot, 'YData', xy(:, 2));
set(gca, 'XLim', xlim)
set(gca, 'YLim', ylim)
set(h_circle_1, 'XData', xy_circle_1(:, 1) + xy(1, 1));
set(h_circle_1, 'YData', xy_circle_1(:, 2) + xy(1, 2));
set(h_circle_2, 'XData', xy_circle_2(:, 1) + xy(1, 1));
set(h_circle_2, 'YData', xy_circle_2(:, 2) + xy(1, 2));
set(h_circle_3, 'XData', xy_circle_3(:, 1) + xy(1, 1));
set(h_circle_3, 'YData', xy_circle_3(:, 2) + xy(1, 2));
pause(1)
end
You can change the parameters as you wish.
You can use the following functions
figure; %creates a figure
hold on; %overlays points and circles
clf; %clear the figure
and use two types of markers (. and o) of various sizes for the points and circles
plot(x,y, 'b.', 'MarkerSize', 4);
plot(x,y, 'ro', 'MarkerSize', 10);
plot(x,y, 'go', 'MarkerSize', 14);
plot(x,y, 'bo', 'MarkerSize', 18);

Matlab 3D interpolation

I have 3 matrices(129x129) corresponding to x, y and z coordinates. I used the function mesh
mesh(x,y,z);
to plot the corresponding figure. It comes out to be a sphere. Now, I have another set of x, y, z(again 129) which gives a different sphere. What I want is to use interpolation in MATLAB to obtain the figures that come in between. I looked at the function interp3 in MATLAB but could not figure out what to do with it.
It seems like you are interested in the evolution of the surface z(x,y) from one surface z0 to another z1. I would suggest the following process
T = 5; % number of "time steps" from z0 to z1
t = linspace( 0, 1, T );
for ii = 1 : T
zt = t(ii).*z1 + (1-t(ii)).*z0;
mesh( x, y, zt ); title( sprintf( 'time %d', ii ) );
drawnow;
pause(1); wait a sec
end