Question
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)
Related Questions
Expert Answer
John Williams
PhD Expert
Answered Aug 14, 2026
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):
- Define the grid for ee and e?\dot{e} using
meshgrid. - Calculate VV at each point on the grid.
- Plot the 3D surface using MATLAB's
meshfunction.
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:
-
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.
-
meshgridUsage:meshgridgenerates two 2D matrices EE and EDED that define the grid of ee and e?\dot{e} values.
-
Double Loop:
- The double
forloop iterates over the grid points, computes VV for each (e,e?)(e, \dot{e}), and stores it in the corresponding entry of VV.
- The double
-
Plotting with
mesh:- The
meshfunction plots V(e,e?)V(e, \dot{e}) as a 3D surface.
- The
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!
Have a different question? Ask here