Lorenz Attractor Animation with Frame-by-Frame Plotting

조회 수: 8 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2024년 8월 9일
% 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
Walter Roberson
Walter Roberson 2024년 8월 9일
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 2024년 8월 9일
Oh understood! I tried to run it under MATLAB Answers. Thank you very much again

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by