Lorenz Attractor Animation with Frame-by-Frame Plotting

8 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Ag. de 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 comentarios
Walter Roberson
Walter Roberson el 9 de Ag. de 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
Athanasios Paraskevopoulos el 9 de Ag. de 2024
Oh understood! I tried to run it under MATLAB Answers. Thank you very much again

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by