Lyapunov Plotting: using 'mesh' and 'meshgrid' with matricies

Illustration
Daniel_cleveland - 2021-10-04T13:26:55+00:00
Question: Lyapunov Plotting: using 'mesh' and 'meshgrid' with matricies

I'm trying to plot a 3d graph of a Lyapunov function of a control system I've created. The function is:   V(x)=xT*P*x   where x is a 2x1 matrix of the errors, e and de/dt:   x = [e;ed]; Therefore x transpose is a 1x2 matrix: xT = [e, ed]; and the P matrix is 2x2 of constants (ie p1=const,p2=const,p3=const,p4=const): P = [ p1 p2 ; p3 p4]; "e" and "ed" are a 1xn set of data from my simulation thus V ends up being a 1xn matrix This is my code right now, I don't know what to do from here:   e = ErrorData.signals.values(:,1); ed = ErrorData.signals.values(:,2); for i=1:3420 V(i) = [e(i) , ed(i)] * P * [e(i) ; ed(i)] ; end I'm trying to plot (x,y,z) = (e,ed,V) in 3 dimensions but I can't seem to be able make my final vector V suitable for plotting in 3d. for e and ed you can use meshgrid, but I can't get V in the proper form.   I was using this code as an example from ( Lyapunov Example ):   x=[-4:.04:4]; y=x; [X,Y]=meshgrid(x,y); z=X.^2 + Y.^2; mesh(X,Y,z)  

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

To plot your Lyapunov function V(x)=xTPxV(x) = x^T P x in 3D, you need to generate a grid of ee and e?\dot{e} values and calculate VV at each point on that grid. Here's how you can do it step by step:


Steps to Plot V(x)V(x):

  1. Define the grid for ee and e?\dot{e} using meshgrid.
  2. Calculate VV at each point on the grid.
  3. Plot the 3D surface using MATLAB's mesh function.

Full MATLAB Code:

 

% Define P matrix (example values, modify as needed)
p1 = 1; p2 = 0.5; p3 = 0.5; p4 = 2;
P = [p1, p2; p3, p4];

% Define the range of e and ed values
e_range = -4:0.04:4; % Example range
ed_range = -4:0.04:4; % Example range

% Create a grid of e and ed values
[E, ED] = meshgrid(e_range, ed_range);

% Initialize V matrix
V = zeros(size(E));

% Compute V for each (e, ed) pair
for i = 1:size(E, 1)
    for j = 1:size(E, 2)
        x = [E(i, j); ED(i, j)];
        V(i, j) = x' * P * x; % Lyapunov function calculation
    end
end

% Plot the result
figure;
mesh(E, ED, V);
xlabel('e'); ylabel('ed'); zlabel('V(e, ed)');
title('Lyapunov Function V(e, ed)');
grid on;

Explanation:

  1. Matrix Dimensions:

    • EE and EDED are m×nm \times n matrices representing the grid of ee and e?\dot{e} values.
    • VV is also an m×nm \times n matrix calculated for each grid point.
  2. meshgrid Usage:

    • meshgrid generates two 2D matrices EE and EDED that define the grid of ee and e?\dot{e} values.
  3. Double Loop:

    • The double for loop iterates over the grid points, computes VV for each (e,e?)(e, \dot{e}), and stores it in the corresponding entry of VV.
  4. Plotting with mesh:

    • The mesh function plots V(e,e?)V(e, \dot{e}) as a 3D surface.

Optimization (Vectorized Calculation):

To avoid the double loop, you can vectorize the calculation of V:

% Vectorized computation of V
V = P(1,1)*E.^2 + (P(1,2) + P(2,1))*E.*ED + P(2,2)*ED.^2;

% Plot the result
figure;
mesh(E, ED, V);
xlabel('e'); ylabel('ed'); zlabel('V(e, ed)');
title('Lyapunov Function V(e, ed)');
grid on;

Output:

  • A 3D plot showing the Lyapunov function VV, where the x-axis is ee, the y-axis is e?\dot{e}, and the z-axis is V(e,e?)V(e, \dot{e}).

Let me know if you need further assistance!


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!