Matlab: Smooth Rotating Animation for Line Plots

I recently became stuck trying to create an animation which consists of a smooth rotation of a viewpoint around the Lorenz attractor. Methods I use for changing viewpoints with respect to surface objects were creating jerky, lagging animations when applied to my line plot.

This will work for any 3D line plot.

plot3(x,y,z);
axis vis3d

fps = 60; sec = 10;

vidObj = VideoWriter('plotrotation.avi');
vidObj.Quality = 100;
vidObj.FrameRate = fps;

open(vidObj);
for i=1:fps*sec
  camorbit(0.9,-0.1);
  writeVideo(vidObj,getframe(gcf));
end
close(vidObj);

This results in the following smooth animation: 




Matlab: Coaxial Cylinders (Polar Coordinates)

Let’s say we want to create an aesthetically pleasing visualization of 2 coaxial cylinders.

To do this, we’ll be adjusting the lighting, proportions, and transparency of the figure.


 The code to create this figure utilizes the coordinate transformation from Cartesian to 3D-Polar coordinates. Recall that the transformation between coordinate systems is as follows.

Instead of using Matlab’s built in cart2pol method, we will manually convert each Cartesian (x, y, z) coordinate to it’s equivalent polar coordinate (r, phi, z) .
Side note: 
I used phi above in the transformation equations and theta below in the code to make a point. The angular coordinate in the cylindrical (a.k.a polar) coordinate system is generally represented as either phi or theta.
These are equivalent, and the variable chosen is purely a matter of notation.

Y=-5:5;
theta=linspace(0,2*pi,40);
[Y,theta]=meshgrid(Y,theta);

r = 1.5
% calculate x and z
X=r*cos(theta);
Z=r*sin(theta);

hs = surf(X,Y,Z)

set(hs,'EdgeColor','None', ...
        'FaceColor', [0.5 0.5 0.5], 'FaceLighting', 'phong');
alpha(0.7);

hold on
camlight right;

r = 0.5
% recalculate x and z
X=r*cos(theta);
Z=r*sin(theta);

hs = surf(X,Y,Z)

axis equal

set(hs,'EdgeColor','None', ...
        'FaceColor', [0.5 0.5 0.5], 'FaceLighting', 'phong');
alpha(0.7);

axis off
camlight right;
lighting gouraud
view(140, 24)

% white background
set(gcf,'color','white')

Matlab: Lorenz Attractor

I’m a big fan of the Lorenz Attractor, which, when plotted, resembles the half open wings of a butterfly. This attractor was derived from a simplified model of convection in the earth’s atmosphere. One simple version of the Lorenz attractor is pictured below:

The Lorentz system is a set of ordinary differential equations notable for its chaotic solutions (see below).  Here $x$, $y$ and $z$ make up the system state, $t$ is time, and $sigma, row, beta$ are the system parameters.

The Lorentz attractor is a chaotic solution to this system found when $row = 28, sigma = 10. beta = 8/3$.

The series does not form limit cycles nor does it ever reach a steady state.

We can calculate and render the aforementioned chaotic solution to this ODE as follows:

function loren3
clear;clf
global A B R 
A = 10; 
B = 8/3; 
R = 28; 
u0 = 100*(rand(3,1) - 0.5); 
[t,u] = ode45(@lor2,[0,100],u0); 
N = find(t>10); v = u(N,:);

x = v(:,1);
y = v(:,2);
z = v(:,3);

plot3(x,y,z);
view(158, 14)
 

function uprime = lor2(t,u) 

global A B R 
uprime = zeros(3,1); 
uprime(1) = -A*u(1) + A*u(2); 
uprime(2) = R*u(1) - u(2) - u(1)*u(3); 
uprime(3) = -B*u(3) + u(1)*u(2);

This results in the figure:

To create a surface/mesh from this line plot, we proceed…

Matlab: Create Mesh or Surface From Line Plot

This is a continuation of Matlab: Lorentz Attractor, however, these methods can be applied to any line plot or collection of points.

A slightly more aesthetically pleasing representation of the Lorentz Attractor can be achieved by adding axis off. And altering the view’s azimuth and elevation: view(15, 48).

Now we’re talking. Let’s say I want to make a surface or mesh from this dandy line plot. Using surf or mesh will throw an error, since x, y, and z are all 1D vectors! Whatever shall we do!
Never fear, mathematicians will save the day.
Delaunay created a sweet method of triangulating points. If we treat this line plot as a collection of points, we can triangulate to find an approximate surface.
tri = delaunay(x,y);
plot(x,y,'.')

%determine amount of triangles
[r,c] = size(tri);
disp(r)

%plot with trisurf
h = trimesh(tri, x, y, z, 'FaceAlpha', 0.6);
alpha = 0.4
view(15, 48)
axis vis3d

axis off
l = light('Position',[-50 -15 29])
lighting phong
shading interp

 

What if we’d like a surface instead of the mesh? Then we’ll change trimesh to trisurf add transparency (alpha = 0.7) and find: