Lorenz Attractor Animation with Frame-by-Frame Plotting

10 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Août 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 commentaires
Walter Roberson
Walter Roberson le 9 Août 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 le 9 Août 2024
Oh understood! I tried to run it under MATLAB Answers. Thank you very much again

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by