Search code examples
matlabmathlorenz-system

3D printing the Lorenz Attractor using MATLAB


I'm using MATLAB to plot the Lorenz attractor and was wondering how I could export the XYZ coordinates to a 3D printable file! I'm having trouble going from the XYZ coordinates to a surface (should I even do that?). In addition, even thinking of the Lorenz Attractor as a "surface" seems not right to me, since my understanding is that it's more of a "path."

My code so far:

clear all; clc; clf;

dt = .01;
n = 100; %Number of Drawings
t = 0:dt:n; %Time Scale

%INITIAL CONDITIONS
x = 1;
y = 1;
z = 1;
xyz0 = [x y z];

%SOLVE ODE
[t,xyz] = ode45(@rhs, t, xyz0);

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%     PLOTTING    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%plot3(x,y,z);

rotate3d on;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%         RHS FOR ODE45     %%%%%%%%%%%%%%%%%%%%%%%%
function xyzdot = rhs(t,xyz)

    sigma = 10;
    b     = 8/3;
    r     = 100;

    x = xyz(1);
    y = xyz(2);
    z = xyz(3);

    dxdt = sigma*(y - x);
    dydt = r*x - y - x*z;
    dzdt = x*y - b*z; 

    xyzdot = [dxdt; dydt; dzdt];

end

I tried this earlier:

[X,Y] = meshgrid(x,y);
surf(X, Y, z);

But nothing's working. The error says z must be a matrix, and I'm a bit lost.


Solution

  • mesh cannot work because you are plotting a curve. Mesh wants matrices as input. To plot a curve you can use plot3. Try this:

    figure;
    plot3(x, y, z);
    grid on