How to plot 2d and 3D phase portrait as in the image attachment for chaotic visualization? Also see my code.

7 views (last 30 days)
function chaotic
% Define the parameters
k = 1;
lambda_1 = 1;
k_1 = 1;
k_2 = 2;
k_3 = 1;
v_1 = 0.4;
rho = 2;
omega = 1;
mu = 1;
% Calculate W_1 and W_2 based on the given parameters
W_1 = 6 * (k * lambda_1 + rho) / (3 * k * k_3 + 3 * k_2);
W_2 = (k^3 * k_3 + 3*k^2*k_2 + 6*(k*k_1 - v_1)) / (3 * k * k_3 + 3 * k_2);
% Define the time span
tspan = [0 100];
% Generate a grid of initial conditions
[G0, P0] = meshgrid(linspace(0, 0.2, 10), linspace(0, 0.2, 10));
initial_conditions = [G0(:), P0(:)];
% Prepare figures
figure('Name', '2D Phase Portrait');
figure('Name', '3D Phase Portrait');
% Loop over the initial conditions
for i = 1:size(initial_conditions, 1)
% Solve the ODE
[T, Y] = ode45(@(t, y) odesys(t, y, W_1, W_2, omega, mu), tspan, initial_conditions(i, :));
% Plot the 2D phase portrait
figure(findobj('Name', '2D Phase Portrait'));
plot(Y(:, 1), Y(:, 2));
hold on;
% Plot the 3D phase portrait
figure(findobj('Name', '3D Phase Portrait'));
plot3(T, Y(:, 1), Y(:, 2));
hold on;
end
% Finalize the 2D figure
figure(findobj('Name', '2D Phase Portrait'));
xlabel('G');
ylabel('P');
title('2D Phase Portrait');
grid on;
hold off;
% Finalize the 3D figure
figure(findobj('Name', '3D Phase Portrait'));
xlabel('Time');
ylabel('G');
zlabel('P');
title('3D Phase Portrait');
grid on;
hold off;
end
% Nested function for the system of ODEs
function dydt = odesys(t, y, W_1, W_2, omega, mu)
G = y(1);
P = y(2);
dGdt = P;
dPdt = W_1 * G^3 + W_2 * G + omega * cos(mu * t)^2;
dydt = [dGdt; dPdt];
end

Answers (1)

sanidhyak
sanidhyak on 1 Sep 2025 at 10:33
I understand that you are trying to plot 2D and 3D phase portraits for chaotic visualization using MATLAB. When running the code you shared, I too observed that while the ODE integration works fine, the visualization can be improved to properly display chaotic attractors similar to what you have shared in the figure.
Actually the current implementation loops over a grid of initial conditions and directly plots the results, which can make the plots look cluttered. For a clear chaotic phase portrait, you should use a single trajectory over a longer simulation time.
Kindly refer to the following corrected code for the same:
function chaotic_phase
% Parameters
k = 1; lambda_1 = 1; k_1 = 1; k_2 = 2; k_3 = 1;
v_1 = 0.4; rho = 2; omega = 1; mu = 1;
% Calculate W1 and W2
W1 = 6*(k*lambda_1 + rho) / (3*k*k_3 + 3*k_2);
W2 = (k^3*k_3 + 3*k^2*k_2 + 6*(k*k_1 - v_1)) / (3*k*k_3 + 3*k_2);
% Time span
tspan = [0 200];
% Initial condition
y0 = [0.1, 0.1];
% Solve ODE
[T, Y] = ode45(@(t,y) odesys(t,y,W1,W2,omega,mu), tspan, y0);
% 2D Phase Portrait
figure;
plot(Y(:,1), Y(:,2), 'g');
xlabel('G'); ylabel('P');
title('2D Chaotic Phase Portrait');
set(gca,'color','k'); grid on; axis tight;
% 3D Phase Portrait
figure;
plot3(Y(:,1), Y(:,2), T, 'g');
xlabel('G'); ylabel('P'); zlabel('t');
title('3D Chaotic Phase Portrait');
set(gca,'color','k'); grid on; axis tight;
end
% System of ODEs
function dydt = odesys(t,y,W1,W2,omega,mu)
G = y(1);
P = y(2);
dGdt = P;
dPdt = W1*G^3 + W2*G + omega*(cos(mu*t))^2;
dydt = [dGdt; dPdt];
end
Here, we have done the following changes:
  • Used a single initial condition.
  • Increased the simulation time (“tspan = [0 200]”) to observe chaotic dynamics.
  • Added black background with green traces (same as that you shared).
  • Plotted “P vs G” (2D) and “(G, P, t)” in 3D for clearer chaotic attractor visualization.
For more details on phase portrait plotting, you may refer to the MATLAB documentation:
I hope this helps!

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!