A cost function (or objective function) is a scalar mathematical expression that quantifies the performance of a control system, defining the quantity to be minimized by the Model Predictive Control (MPC) optimizer over a finite prediction horizon. It typically penalizes deviations from a desired reference (tracking error), excessive control effort, and other economic or operational objectives, translating high-level goals into a solvable Optimal Control Problem (OCP).
Glossary
Cost Function (Objective Function)

What is a Cost Function (Objective Function)?
In Model Predictive Control (MPC), the cost function is the mathematical core that defines the controller's goal, quantifying performance to be minimized at each optimization step.
In practice, the function is often formulated as a quadratic sum of weighted terms, balancing competing priorities like accuracy and actuator wear. The MPC solver computes control inputs that minimize this future predicted cost while respecting state constraints and input constraints. This explicit optimization of a defined metric is what distinguishes MPC from simpler feedback controllers, enabling proactive, performance-driven behavior.
Key Components of an MPC Cost Function
The cost function, or objective function, is the mathematical core of a Model Predictive Controller. It quantifies the performance goals—such as tracking accuracy, energy use, and actuator wear—that the controller must minimize at each time step.
Tracking Error (Reference Deviation)
This is the primary term that penalizes deviation from a desired trajectory or setpoint. It is typically formulated as a quadratic function of the difference between the predicted system states or outputs and their target values over the prediction horizon.
- Mathematical Form: Often expressed as
(x - x_ref)^T * Q * (x - x_ref), whereQis a positive semi-definite weighting matrix. - Purpose: Drives the system to follow the commanded path as closely as possible.
- Design Choice: The matrix
Qallows the engineer to prioritize which states (e.g., position vs. velocity) are most critical to track accurately.
Control Effort (Input Cost)
This term penalizes the magnitude of the control inputs (e.g., motor torque, steering angle) applied to the system. Minimizing control effort reduces energy consumption, actuator wear, and aggressive, potentially destabilizing actions.
- Mathematical Form: Typically
u^T * R * u, whereRis a positive definite weighting matrix. - Purpose: Promotes smooth, efficient control actions and prevents actuator saturation.
- Trade-off: Tuned against the tracking error term. A high
Rweight results in slower, more conservative control, while a low weight allows for more aggressive tracking.
Terminal Cost
An additional cost term evaluated specifically at the final state of the prediction horizon. It is a crucial theoretical tool for ensuring closed-loop stability.
- Mathematical Form: Often
x_N^T * P * x_N, wherePis computed as the solution to an Algebraic Riccati Equation (ARE) related to the infinite-horizon Linear Quadratic Regulator (LQR). - Purpose: Approximates the cost-to-go beyond the finite prediction horizon, encouraging the predicted terminal state to be near a desirable equilibrium.
- Stability: When combined with a terminal constraint, it can provide formal guarantees of asymptotic stability for the MPC law.
Constraint Violation Penalty (Slack Variables)
A mechanism to handle soft constraints. When hard constraints (e.g., position limits) might make the optimization problem infeasible, slack variables are introduced and penalized in the cost function.
- How it works: A slack variable
εabsorbs constraint violations. The cost function includes a term likeρ * ε^2, whereρis a large, tunable penalty weight. - Purpose: Ensures the online optimizer always has a feasible solution, improving controller robustness to unexpected disturbances or infeasible commands.
- Use Case: Essential for practical deployment where temporary, minor constraint breaches are preferable to controller failure.
Rate of Change Penalty
Penalizes large changes in control inputs between consecutive time steps. This term directly influences the smoothness of the actuation signal.
- Mathematical Form:
Δu^T * S * Δu, whereΔu = u_k - u_{k-1}andSis a weighting matrix. - Purpose: Reduces mechanical stress on actuators, improves passenger comfort in vehicles, and filters out high-frequency noise from the control signal.
- Practical Impact: A well-tuned rate penalty prevents 'chattering'—rapid, oscillatory control actions that waste energy and cause wear.
Economic Objective
In Economic MPC (EMPC), the cost function moves beyond traditional regulation to directly encode a process economics or utility goal.
- Examples: Minimizing energy consumption in a building's HVAC system, maximizing throughput in a chemical reactor, or minimizing fuel use in a vehicle.
- Key Difference: The cost function is not necessarily positive definite around a setpoint. The controller seeks a dynamically optimal operating point, which may involve transient excursions to improve long-term economic performance.
- Application: Common in process industries and energy systems where operational profit, not just setpoint tracking, is the primary goal.
How the Cost Function Works in MPC
The cost function is the mathematical heart of Model Predictive Control (MPC), quantifying the performance objective that the controller strives to minimize at every time step.
In Model Predictive Control (MPC), the cost function (or objective function) is a scalar mathematical expression that quantifies the controller's performance over a finite prediction horizon. It is minimized online at each control step to compute an optimal sequence of future control inputs. The function typically penalizes tracking error (deviation from a reference) and control effort (actuator movement), often using a quadratic form for computational efficiency. This transforms the control problem into a constrained optimization problem.
The design of the cost function directly dictates the closed-loop behavior. A quadratic cost on state and input error leads to a smooth, energy-efficient response. Engineers can add terms for economic objectives, like minimizing fuel consumption, or use slack variables to soften constraints. The optimization solver, such as a Quadratic Programming (QP) solver for linear MPC, finds the control sequence that minimizes this cost while respecting the system's dynamic model and state and input constraints. Only the first control action is executed before the receding horizon principle resets the calculation.
Common Cost Function Types and Examples
In Model Predictive Control, the cost function quantifies the performance objective to be minimized. Different formulations prioritize tracking, energy use, or economic goals.
Quadratic Tracking Cost
The most common form in Linear MPC, penalizing the squared deviation of predicted states and inputs from their desired references. It leads to a convex Quadratic Programming (QP) problem.
- Mathematical Form: ( J = \sum_{k=0}^{N-1} (x_k - x_{ref})^T Q (x_k - x_{ref}) + (u_k - u_{ref})^T R (u_k - u_{ref}) + (x_N - x_{ref})^T P (x_N - x_{ref}) )
- Weights: ( Q ), ( R ), and ( P ) are positive (semi-)definite weighting matrices that tune the trade-off between state error and control effort.
- Example: In autonomous vehicle path following, ( x ) represents lateral deviation and heading error, weighted by ( Q ), while ( u ) represents steering angle, weighted by ( R ) to ensure smooth driving.
Economic Cost Function
Directly encodes operational economics, such as profit maximization or energy cost minimization, rather than traditional reference tracking. This is central to Economic MPC.
- Objective: Minimize a stage cost ( \ell(x_k, u_k) ) that represents instantaneous economic loss (e.g., $/hr).
- Key Challenge: Stability is not inherent and must be enforced via additional constraints or a terminal cost, unlike tracking MPC.
- Real-World Example: In a chemical reactor, the cost function could be ( J = \sum \text{(Price of Feedstock - Value of Product + Cost of Utilities)} ), optimized in real-time against a dynamic process model.
Terminal Cost & Constraint
Design elements added to the end of the prediction horizon to guarantee closed-loop stability, especially with short horizons.
- Terminal Cost: An additional term ( V_f(x_N) ) (e.g., from a Lyapunov function or Algebraic Riccati Equation) that penalizes the final state.
- Terminal Constraint: Requires the predicted state at the end of the horizon, ( x_N ), to lie within a terminal set ( \mathbb{X}_f ).
- Purpose: These components ensure the optimization approximates an infinite-horizon problem, providing a stability guarantee for the Receding Horizon Control law.
Handling Constraints: Slack Variables
A method to implement soft constraints by adding slack variables to the cost function, preventing infeasibility of the Optimal Control Problem (OCP).
- Mechanism: A constraint like ( y_k \leq y_{max} ) is reformulated as ( y_k \leq y_{max} + \epsilon_k ) with ( \epsilon_k \geq 0 ). A large penalty weight ( \rho ) on ( \epsilon_k ) is added to the cost: ( J = ... + \rho \sum \epsilon_k^2 ).
- Use Case: Critical for output constraints that may become temporarily unavoidable due to large disturbances, allowing a small, penalized violation instead of controller failure.
- Trade-off: The weight ( \rho ) creates a tunable balance between constraint satisfaction and primary objective performance.
Nonlinear & Non-Quadratic Costs
Used in Nonlinear MPC (NMPC) for problems where the performance metric is not adequately captured by a quadratic form.
- Formulation: ( J = \sum \ell(x_k, u_k) ), where ( \ell(\cdot) ) is a general nonlinear function.
- Common Types:
- Minimum-Time: ( \ell = 1 ), minimizing the number of steps to reach a goal.
- Obstacle Avoidance: High cost assigned to states inside obstacle regions.
- Fuel-Optimal (L1 Norm): ( \ell = |u_k| ), minimizing absolute control effort, common in aerospace.
- Solver Impact: Requires more complex Nonlinear Programming (NLP) solvers like Sequential Quadratic Programming (SQP) or Interior-Point Methods.
Cost Function in Robust & Stochastic MPC
Modified formulations that account for uncertainty in the dynamic model or disturbances.
- Worst-Case (Robust MPC): The cost is minimized for the worst-case realization of uncertainty within a bounded set (min-max optimization).
- Expected Value (Stochastic MPC): The cost is the expectation ( \mathbb{E}[J] ) taken over a distribution of possible disturbances or model parameters.
- Chance Constraints: System constraints are required to hold with a probability ( 1 - \alpha ). The cost function may include terms to minimize the risk of constraint violation.
- Result: These formulations yield controllers that are less aggressive but more reliable in the face of uncertainty, increasing robust stability.
Frequently Asked Questions
In Model Predictive Control (MPC), the cost function is the mathematical core that defines the controller's goal. These questions address its formulation, role, and practical implementation for engineers.
A cost function (or objective function) in Model Predictive Control (MPC) is a scalar mathematical expression, typically quadratic, that quantifies the performance of the controller over a future prediction horizon, which the online optimization solver is designed to minimize at each control step.
It formally encodes the control objectives, such as:
- Tracking Error: Minimizing the deviation of predicted system states or outputs from a desired reference trajectory.
- Control Effort: Penalizing the magnitude or rate of change of control inputs to avoid aggressive actuation and save energy.
- Economic Cost: Directly optimizing for a process economic metric, such as fuel consumption or production rate, in Economic MPC.
The standard form for a Linear Quadratic MPC problem is: J = Σ (x(k) - x_ref(k))^T Q (x(k) - x_ref(k)) + Σ u(k)^T R u(k), where Q and R are positive-definite weighting matrices that tune the trade-off between tracking performance and control effort.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
The cost function is a core component of the MPC optimization problem. These related terms define the mathematical and computational framework within which it operates.
Optimal Control Problem (OCP)
The Optimal Control Problem (OCP) is the complete mathematical formulation solved at each step of MPC. It defines the search for a control sequence that minimizes a cost function subject to a dynamic model and constraints. The OCP is the formal container for the cost function, which acts as its primary objective.
- Core Components: Dynamic model equations, cost function, initial state, state/input constraints, and time horizon.
- Role of Cost Function: The cost function provides the scalar metric the OCP solver aims to minimize.
- Example: For a drone, the OCP finds throttle and steering inputs that minimize a cost combining position error and fuel use, while respecting motor limits.
Quadratic Programming (QP) Solver
A Quadratic Programming (QP) Solver is the numerical optimization engine that computes the solution to the OCP in Linear MPC. When the cost function is quadratic and constraints are linear, the OCP reduces to a QP. The solver's performance directly determines if MPC can run in real-time.
- Connection to Cost Function: The solver minimizes the quadratic cost function (e.g., tracking error² + control effort²).
- Critical Requirement: Must solve the QP within one controller sampling period (e.g., < 10ms).
- Common Algorithms: Active-set methods, interior-point methods, and specialized real-time iterations.
State Constraints & Input Constraints
State constraints and input constraints are inequality bounds that define the safe and feasible operating region of the system. While the cost function defines what is optimal, these constraints define what is allowed. MPC explicitly enforces them over the prediction horizon.
- State Constraints: Limits on system variables (e.g., robot joint angles, tank levels, vehicle position).
- Input Constraints: Limits on actuator commands (e.g., motor torque, valve opening, steering angle).
- Interaction with Cost: The optimizer finds the control sequence that minimizes cost while satisfying all constraints. Infeasibility can be handled with soft constraints (penalized in the cost function).
Terminal Cost & Terminal Constraint
A terminal cost is an additional term added to the end of the MPC cost function, and a terminal constraint requires the final predicted state to lie within a specific set. These are advanced design tools used primarily to guarantee closed-loop stability for the finite-horizon MPC controller.
- Terminal Cost: Often chosen as the cost-to-go from a stabilizing controller (e.g., Linear Quadratic Regulator). It approximates the cost of the infinite future beyond the horizon.
- Terminal Constraint: Forces the predicted state at the horizon's end into a known positively invariant set.
- Design Purpose: Together, they ensure the finite-horizon optimization has the same stabilizing properties as an infinite-horizon one.
Economic MPC
Economic MPC is a variant where the cost function directly encodes a process economic or business objective, rather than a traditional technical objective like tracking error. The cost function becomes a direct measure of profit, energy consumption, or raw material cost.
- Cost Function Shift: Moves from
(setpoint - output)²to(price * input - revenue * output). - Example: In a chemical reactor, the cost function could be
-(profit = value_of_product - cost_of_reactants - cost_of_heating). - Challenge: Economic cost functions are often non-convex and non-quadratic, requiring Nonlinear MPC (NMPC) and more sophisticated solvers.
Sequential Quadratic Programming (SQP)
Sequential Quadratic Programming (SQP) is a leading iterative algorithm for solving the Nonlinear Programming (NLP) problems that arise in Nonlinear MPC (NMPC). It handles non-quadratic cost functions and nonlinear models by solving a series of approximate QP subproblems.
- Role in NMPC: When the cost function or model is nonlinear (e.g., aerodynamic drag, chemical kinetics), the OCP is an NLP. SQP is a standard method for its solution.
- Mechanism: At each iteration, it linearizes constraints and approximates the cost function as a quadratic (using the Hessian), creating a local QP to solve.
- Real-Time NMPC: Real-time iterations (RTI) of SQP, where only one QP is solved per time step, are crucial for practical NMPC.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us