Lorenz Attractor Animation with Frame-by-Frame Plotting

8 views (last 30 days)
I was trying to create an animation of Lorenz Attractor. I used the following code but it did not give me the result I want like https://en.wikipedia.org/wiki/Lorenz_system.
% Parameters
sigma = 10;
rho = 28;
beta = 8/3;
% Time span
tspan = [0 50]; % Adjusted for smooth animation
% Initial conditions
y0 = [1; 1; 1];
% Lorenz system of equations
lorenz = @(t, y) [sigma * (y(2) - y(1));
y(1) * (rho - y(3)) - y(2);
y(1) * y(2) - beta * y(3)];
% Solve the system using ode45
[t, y] = ode45(lorenz, tspan, y0);
% Set up the figure for animation
figure;
h = plot3(y(1,1), y(1,2), y(1,3));
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Lorenz Attractor Animation');
grid on;
axis([-20 20 -30 30 0 50]);
view(3);
hold on;
% Animate the Lorenz attractor
for i = 2:length(t)
% Update the plot data
h.XData = y(1:i, 1);
h.YData = y(1:i, 2);
h.ZData = y(1:i, 3);
% Refresh the figure
drawnow;
% Optional: Pause to slow down the animation if too fast
pause(0.01); % Adjust the pause time as needed
end

Accepted Answer

Walter Roberson
Walter Roberson on 9 Aug 2024
% Parameters
sigma = 10;
rho = 28;
beta = 8/3;
% Time span
tspan = [0 50]; % Adjusted for smooth animation
% Initial conditions
y0 = [1; 1; 1];
% Lorenz system of equations
lorenz = @(t, y) [sigma * (y(2) - y(1));
y(1) * (rho - y(3)) - y(2);
y(1) * y(2) - beta * y(3)];
% Solve the system using ode45
[t, y] = ode45(lorenz, tspan, y0);
% Set up the figure for animation
figure;
h = plot3(y(:,1), y(:,2), y(:,3));
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Lorenz Attractor Animation');
grid on;
axis([-20 20 -30 30 0 50]);
view(3);
hold on
h = plot3(y(1,1), y(1,2), y(1,3), 'k.', 'MarkerSize', 20);
% Animate the Lorenz attractor
for i = 2:length(t)
% Update the plot data
h.XData = y(i, 1);
h.YData = y(i, 2);
h.ZData = y(i, 3);
% Refresh the figure
drawnow;
% Optional: Pause to slow down the animation if too fast
pause(0.01); % Adjust the pause time as needed
end
  4 Comments
Walter Roberson
Walter Roberson on 9 Aug 2024
It works fine for me when I execute inside of MATLAB.
If you are running it under MATLAB Answers, then MATLAB Answers will show you only the final plot, not anything in-between.
Athanasios Paraskevopoulos
Oh understood! I tried to run it under MATLAB Answers. Thank you very much again

Sign in to comment.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!