← Back to Course
Lab 6

Lab 6 - MPC for a Linear System

Lab 6: MPC for a Linear System

Objective

Implement and understand Model Predictive Control (MPC) for a constrained linear system through interactive experimentation.


🎮 Interactive MPC Demo

🚀 New! Experience Model Predictive Control in real-time with our interactive simulation

Try It Now

Experiment with MPC parameters and see instant results! The simulator below runs a mass-spring-damper system (similar to car suspension) using Model Predictive Control.

Direct Link: Open in Full Screen →


📊 Embedded Interactive Demo (may take 10-15 seconds to load)


🎯 What You Can Do:

Parameter What It Controls Try This
Horizon (N) How far ahead MPC predicts Start at 10, increase to 20, see smoother control
State Weight (Q) Tracking accuracy priority Increase to 50 - faster response, more aggressive
Input Weight (R) Control smoothness priority Increase to 20 - gentler, energy-efficient control
Max Force (u_max) Actuator limit Reduce to 10 - see how constraints affect performance

📈 What You'll Observe:

  • Position Tracking: How well the system reaches the target (1 meter)
  • Velocity Profile: System dynamics over time
  • Control Input: Force applied by the MPC controller
  • Performance Metrics: Settling time, overshoot, control effort

Tasks

1. Understanding MPC Parameters

Use the interactive demo above to:

  • Vary the Prediction Horizon (N):

    • Try N = 5, 10, 20, 30
    • Observe how longer horizons affect performance
    • Note the trade-off between performance and computation
  • Adjust State Weight (Q):

    • Start with Q = 10, then try Q = 50, Q = 100
    • Higher Q = tighter tracking, more aggressive control
    • Observe overshoot and settling time
  • Modify Input Weight (R):

    • Try R = 1, R = 10, R = 30
    • Higher R = smoother control, less aggressive
    • See how it affects control energy
  • Test Control Limits (u_max):

    • Reduce u_max from 20 to 10, then 5
    • See how constraints affect achievable performance
    • Important for real-world systems with actuator limits

2. Analysis Questions

Answer these questions based on your experiments:

  1. What happens to settling time when you increase the horizon N?
  2. How does increasing Q affect overshoot?
  3. What's the trade-off between Q and R?
  4. How do control constraints limit performance?

3. Implementation Exercise

Now implement your own MPC controller:

import numpy as np
import cvxpy as cp

# TODO: Implement MPC class based on what you learned
class SimpleMPC:
    def __init__(self, A, B, Q, R, N, u_min, u_max):
        # Your code here
        pass
    
    def solve(self, x0, x_ref):
        # Your code here
        pass

Deliverables

  • Completed Questions: Answer all analysis questions
  • Parameter Study: Document optimal parameters you found
  • MPC Implementation: Working Python code
  • Performance Comparison: Compare your implementation with the interactive demo

Resources

⏱️ 3-4 hours (reduced with interactive learning!)


Tip: The interactive demo uses the same MPC formulation you'll implement. Use it to build intuition before coding!