Daniel_cleveland asked . 2021-10-04

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)

 

mesh , meshgrid , lyapunov , plot , matrix , matrix manipulation , matrix mulitplication

Expert Answer

John Williams answered . 2025-02-17 06:29:38

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

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.