Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 35

LQR Time-Domain Simulation and Performance Analysis

Recommended resources

Hello! Welcome to the next lesson on our journey into multivariable control.

In the last lesson, we demystified the lqr command by exploring the Algebraic Riccati Equation (ARE). You learned that LQR design isn't about guessing the gain matrix , but about solving the ARE for a unique matrix , which then gives us the optimal gain .

Today, we move from theory to practice. You'll learn how to use the gain matrix to simulate your system's behavior and, most importantly, how to analyze the results. This lesson focuses on the core "art" of LQR design: tuning the and matrices to balance performance against control effort. This skill is central to achieving practical, real-world control performance for systems like your rotary inverted pendulum.

Our goal is to simulate the time-domain response of an LQR-controlled system and analyze the performance trade-offs.

1. Simulating the Closed-Loop System

Once we have the optimal gain matrix from solving the ARE, we can define our control law as:

This is a state feedback law. To simulate how the system behaves with this controller, we substitute the control law back into the original state equation:


This gives us the equation for the closed-loop system. We define the closed-loop state matrix as . The system dynamics are now described by the simple linear equation:

By solving for such that is stable (which the LQR algorithm guarantees), we ensure that if the system is disturbed from its equilibrium point , it will return to it. Simulating the response of this closed-loop system from a given initial condition is how we verify the controller's performance.

LQR Simulation for Nonzero Set Points in MATLAB
This image shows a typical simulation result, comparing the system's response with and without LQR control. The goal is to drive the states (position and velocity) back to a setpoint, in this case zero.

2. The Art of the Trade-Off: Tuning Q and R

The real design work in LQR comes from choosing the weighting matrices and . These matrices define what "optimal" means for your specific problem. There is no single "correct" set of and ; they are tuning knobs that you use to balance competing objectives.

The following video provides an excellent high-level explanation of this trade-off using a very intuitive analogy.

What Is Linear Quadratic Regulator (LQR) Optimal Control? | State Space, Part 4

To build intuition for the trade-offs involved in LQR design, watch this video from the MATLAB channel. It uses a great analogy of commuting to work and then a simple simulation to explain the balance between performance (state error) and effort (actuation).

Watch from 2:33 to 14:10. First, pay attention to the 'home to work' analogy, which perfectly explains the concept of a cost function and trade-offs. Then, follow the UFO simulation example. Observe how adjusting Q and R directly impacts the 'maneuver time' (performance) and 'fuel used' (control effort).

As the video demonstrates, the core LQR trade-off is:

  • High Q / Low R: This corresponds to an aggressive controller. You are heavily penalizing state errors () and placing little penalty on control effort ().
    • Expected Result: A fast response, with states returning to zero quickly. The cost is a large control signal , which might demand a powerful, expensive actuator or even exceed physical limits (saturation).
  • Low Q / High R: This corresponds to a conservative controller. You are more tolerant of state errors but heavily penalize control effort.
    • Expected Result: A slower, more sluggish response. The benefit is a small control signal , which is "cheaper" in terms of energy, fuel, or actuator size.

This iterative process of adjusting and and then simulating the response is the standard workflow for LQR design.


3. Practical Walkthrough: LQR for the Inverted Pendulum

Let's apply these concepts directly to the inverted pendulum, a system highly relevant to your project interests. We will see how tuning and affects the stabilization of the pendulum.

First, watch this short segment that demonstrates the effect of changing the control weighting, R, in a MATLAB simulation of an inverted pendulum.

Linear Quadratic Regulator (LQR) Control for the Inverted Pendulum on a Cart [Control Bootcamp]

This video by Steve Brunton provides a concise demonstration of LQR control for the inverted pendulum on a cart. It will show you exactly what to expect when you start tuning your own controller.

Watch from 7:08 to 8:27 and then from 11:21 to 12:42. In the first part, you'll see the MATLAB code and the resulting aggressive stabilization. In the second part, observe how making the control 'expensive' by increasing the R value leads to a much slower, more sluggish response.

Step-by-Step Simulation and Analysis

Now, let's walk through a complete example of designing, simulating, and refining an LQR controller for an inverted pendulum on a cart. The following tutorial from the Control Tutorials for MATLAB and Simulink (CTMS) is a classic resource that does exactly this.

Inverted Pendulum: State-Space Methods for Controller Design

This webpage provides a detailed, step-by-step guide to designing a state-space controller for an inverted pendulum. We will focus on the LQR design and simulation sections.

Please read the section titled 'Linear Quadratic Regulation (LQR)'. Follow these steps: Read through the initial design where Q = C'*C. Note the initial gain K and examine the first simulation plot. Observe that the response is unsatisfactory (slow settling time). Continue to the next part where the Q matrix is modified (Q(1,1) = 5000 and Q(3,3) = 100). Understand why these elements were increased—to put more weight on the cart's position error and the pendulum's angle error. Examine the new gain K (it's much larger) and the improved step response plot. The text explains the trade-off: this better response comes at the cost of greater control force.

This walkthrough encapsulates the entire process:

  1. Initial Design: Start with a simple choice for Q and R (e.g., based on the outputs or identity matrices).
  2. Simulate: Run the simulation and analyze the time-domain response.
  3. Analyze and Refine: Identify which aspects of the response are unsatisfactory (e.g., settling time of the cart, overshoot of the pendulum).
  4. Tune: Adjust the diagonal elements of Q or R to address the specific performance issues. Increasing a Q element corresponding to a specific state tells the controller to prioritize regulating that state. Increasing R tells the controller to use less overall effort.
  5. Iterate: Repeat the simulation and analysis until the performance meets your requirements.
LQR Control of Inverted Pendulum on Cart Simulation Results
A successful LQR controller stabilizes the inverted pendulum, driving its states (position, angle, and their velocities) to the desired setpoints, as shown in these simulation plots.
Test your understanding!

You are designing an LQR controller for the inverted pendulum. Your initial simulation shows that the pendulum angle (phi) is stabilized quickly, but the cart (x) drifts too far and takes a long time to settle back to its target position. Your actuator is a powerful motor that is not close to its saturation limit.

What is the most logical first step to improve the cart's response?

  1. Increase the value of R.
  2. Decrease the diagonal element of Q corresponding to the pendulum angle (phi).
  3. Increase the diagonal element of Q corresponding to the cart position (x).
  4. Decrease the value of R.
Show answer

The correct answer is 3. Increase the diagonal element of Q corresponding to the cart position (x).

Here's why:

  • The problem is that the cart's position error is not being penalized enough. By increasing the corresponding diagonal element in Q, you tell the cost function that errors in x are more "expensive," forcing the optimal controller to work harder to reduce them.
  • Increasing R (option 1) would make the controller more conservative overall, likely worsening the cart's sluggishness.
  • Decreasing the penalty on phi (option 2) might indirectly help the cart, but it's not the most direct approach and could compromise the primary goal of keeping the pendulum stable.
  • Decreasing R (option 4) would make the controller more aggressive overall. This could help speed up the cart's response, but it's a less targeted approach than directly increasing the penalty on the cart's state error. Since the problem is specific to the cart's performance, adjusting the Q matrix is the more precise method.

Conclusion

In this lesson, you have moved from the theory of LQR to its practical application. You now understand how to simulate an LQR-controlled system and, more importantly, how to interpret the results to make informed design decisions.

Key Takeaways:

  • The closed-loop system with LQR control is described by . Simulating this system shows how the controller regulates the states to zero.
  • LQR design is an iterative process of tuning the weighting matrices and to achieve a desired balance between performance and control effort.
  • Increasing diagonal elements in makes the controller prioritize stabilizing the corresponding states.
  • Increasing elements in makes the controller more conservative, reducing the magnitude of the control input at the expense of a slower response.

Preview of the Next Lesson:
Our LQR controller excels at regulation—driving states to zero. However, what if you want to move the cart to a new position (e.g., meters) and hold it there? The current controller, , will always try to drive back to 0. In the next lesson, we will introduce reference tracking, augmenting our controller to make the system follow non-zero setpoints.