Lorenz Attractor Animation with Frame-by-Frame Plotting
8 ビュー (過去 30 日間)
古いコメントを表示
Athanasios Paraskevopoulos
2024 年 8 月 9 日
コメント済み: Athanasios Paraskevopoulos
2024 年 8 月 9 日
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
0 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!