Sitemap

An Introduction to Hamiltonian and Lagrangian Methods for Economists

Jon Law
13 min readJun 8, 2025
A Hamiltonian & Lagrangian General Form

You may remember high school physics class and being introduced to Newtonian mechanics (F=ma, anyone?). If you stuck with physics, you would learn about two alternative, but complementary, approaches—Lagrangian and Hamiltonian classical mechanics.

So, how do we get from physics to economics? What’s all this about Hamiltonian and Lagrangian mechanics, and do we employ these methods in practical economic problems?

Those are the questions we’ll be answering in this article. We’ll start with the core connection between physics and economics and build up into applied methods via Lagrangian multipliers and the Hamiltonian.

For a deeper dive into some of the components mentioned in this article, you can check out this article to learn about states, actions, policies, rewards, & intertemporal optimization, this one for the Pontryagin Maximum and Control Theory, and this for the Envelope Theorem.

So, as with just about everything, our story starts with physics. Newtonian, Hamiltonian, and Lagrangian mechanics are all different approaches to describe the same mechanical systems—frameworks and approaches used to describe the same inherent truths. Depending upon the problem, one approach versus another may be faster or simpler, but they all could be used to solve the same problem.

Newton’s approach was centered on forces (say, gravity!), represented by vectors, which are played forward in time in tiny increments to calculate things like trajectories.

A century later, Lagrange converted the problem into that can sometimes be solved analytically (calculating an exact answer) by finding the minimum or maximum of an integral, instead of having to integrate forces step-by-step. Like so, he framed classical mechanics as an optimization problem over continuous curves (hence the integrals), where you need to minimize the trade-off between kinetic and potential energy.

Then, Hamilton recast these dynamics in phase space—the set of all possible states—to build a single scalar function, of which its partial derivatives generate the same paths otherwise calculated through Newtonian and Lagrangian methods. Central to his approach are costate variables, which equal the marginal value of the state (effects from slight changes in the state). The Hamiltonian function’s FOCs give the law of motion of these costate variables.

So, we have three approaches cementing three concepts: forward-looking forces (Newton), minimization and maximization problems subject to constraints (Lagrange), and optimization with marginal effects (Hamilton).

One conceptual layer deeper, each approach says that an evolving system—whether an orbiting planet, a firm, or a person—could behave or act in many ways, but that we can calculate the trajectory or behavior the system will actually follow, given all the things and constraints acting upon it.

Newtonians, Lagrangians, & Hamiltonian Mechanics

It is this core intuition that explains the deep parallels between physics and economics and why these mechanical systems are so useful in economic practice. Economics asks the same questions, albeit with different labels, about how systems evolve or behave over time.

In economics, we analyze and simulate prices, quantities, and behavior, which feature constraints, different things that can happen, and different things will optimally happen.

Case in point: say we have a satellite is drifting around a planet and we’re trying to find the best flight path to conserve energy, which requires a balancing act between kinetic and potential energy. Say we also have a household can save or consume capital throughout its lifespan—it can give up a little consumption today to save more and enjoy the payoff later or vice versa.

Framing both these problems in the same light, we’re looking to minimize the lifetime resource loss subject to constraints—whether energy for the satellite or utility for the household.

Or, imagine an orbiting planet and say we’re tracking its location and momentum. If we plot the possible locations and momentums, then the planet becomes a point moving along a trajectory (orbit) in a two-dimensional phase portrait (graph of states, basically).

If we swap that language out from astronomy to economics, it barely changes—let’s make the horizontal axis the labour stock of a firm and the vertical axis the marginal value of labour (MPL). The phase portrait, or state space, now describes net present value instead of energy, with the optimal growth path being the arrow threading its way toward a steady state.

Like so, mathematical approaches used to describe changing systems and control how complex, constrained objects act is just as useful in economics as anywhere else.

That’s all a lot of theory—let’s proceed to what exactly the Hamiltonian and Lagrangian frameworks are and how they’re used in economics.

A Note on Newton: In practice, Hamiltonian and Lagrangians are explicit approaches and structures used for all kinds of economic problems that you will most likely have to solve during studies or research. Calculus and differential equations, obviously, are used everywhere—but it’s not the focus from here on out. For that reason, no more talk of Newtonian mechanics!

Lagrangian Optimization is a method for solving optimization problems featuring constraints (budget, time, etc.). It uses what are called Lagrangian multipliers to do this, which aid in calculating local maxima and minima. These points represent the solutions, whether the cost-minimizing bundle or the profit-maximizing strategy.

Meanwhile, “The Hamiltonian” is a function used to solve optimal control problems in dynamic (changing) systems. Control theory is about designing inputs in such a way to achieve a desired outcome, while optimal Control theory is all about finding a “control”, or solution, to maximize an objective function over time. An objective function is kin to a reward function in programming—it’s like the goal of the game, and optimal control theory is about finding the best strategy for the game (I go into more detail about that in this article). Like so, when we’re dealing with a system, world, firm, or consumer that changes over time, the Hamiltonian is a tool to maximize some reward, profit, utility, etc.

A note on Laws of Motion: if not familiar already, you’ll soon run into laws of motion. Laws of motion describe how variables change over time in economics, whether a market, asset, competitor, or demand. Think of them like the laws of physics, except we get to define and build our own rules like gravity and thermodynamics, which may then be applied to elements we’re modeling.

Thus, both these methods are used for constrained optimization. When dealing with dynamic systems, it can be better to use the Hamiltonian, and when dealing with a static situation, it’s best to use Lagrangians. Here are a few common problems and the solution method we’d employ:

  • Consumer choosing a bundle of goods today to maximize utility given a budget constraint. Lagrangian.
  • Firm picking factor prices to minimize cost today. Lagrangian.
  • General equilibrium in a market where each firm is looking to maximize profit subject to resources constraints. Lagrangian.
  • Intertemporal lifecycle utility optimization for households in an economy that can consume or save earnings. Hamiltonian.
  • Firm picking how quickly to drill through an oil reservoir to maximize discounted lifetime profits. Hamiltonian.
  • A nation deciding at what rate to print money to balance debt load and inflation. Hamiltonian

So, mostly-correct rule of thumb: for static or one-period problems, Lagrangians beat out Hamiltonians, while if it involves continuous time, the inverse applies (discrete time problems will often use a Bellman setup—see here).

One note about solutions before we get into the actual setup and examples: for both methods, we’ll need to verify that solutions are both unique and actually exist. For Lagrangians, solution existence requires a continuous objective function and a non-empty feasible set (as defined by constraints). A theorem called the Weierstrass theorem then guarantees that at least one solution exists. Uniqueness requires additional conditions, like the objective function being strictly concave or convex. For Hamiltonians, verifying these conditions more complex — all that complexity is why it’s reasonable to stick with well-known functional forms like Cobb-Douglas and CES functions. With these, you don’t have to worry about no solutions, multiple solutions, or useless conclusions.

The Lagrangian

The actual “Lagrange Multiplier” is a single value denoted by lambda λ. It’s does not represent a variable—instead, it’s a core part of the Lagrangian structure, both simplifiying calculations and representing the “shadow value”—the marginal cost of the constraint.

Here’s the general form of the Lagrangian:

Lagrangian General Form in Economics

x is an input variable, f(x) is some objective (reward) function f, g(x) is the constraint, lambda is the Lagrangian multiplier, and this all forms the Lagrangian L.

With this setup, we’re trying to find the maximum or minumum of the function f subject to the constraint g(x)=0. That maximum or minimum is the point at which all partial derivatives are equal to zero, representing a stationary point.

A note on g(x)=0. The actual constraint is some function g(x) = c, like g(x)=$10 being the amount of money a consumer can spend. We can rewrite this as g(x) — c=0, which says that the value we spend must be equivalent to our constraint (thus, it cannot exceed, say, the amount of money we have). That’s why our constraint is g(x)=0, not just g(x).

Since the inputs of the Lagrangian are lambda and x, this means we want to satisfy this condition:

Lagrangian FOCs (1)

So, returning to the general form, we write out and solve the FOCs:

Lagrangian FOCs (2)

For the FOC with respect to x, we can’t simplify beyond f(x) + λg(x) = 0 without specifiying a functional form for f(x) and g(x). For the FOC with respect to lambda, since lambda isn’t present in f(x) or g(x), the condition simplifies to g(x) — you will always get the constraint for the FOC with respect to lambda. Setting both conditions equal to zero, we get:

Lagrangian FOCs (3)

If we did have a functional form for f(x) and g(x), here’s how we’d proceed:

  1. Compute the partial derivatives.
  2. Solve the systems of equations.
  3. Check that the x we find satisfies g(x)=0.
  4. Check second-order conditions.
  5. Interpret the lambda value, which represents the marginal effect of reducing the constraint by one unit (the shadow price/value).

Here’s a quick example with functional forms. Let’s say we’re maximizing f(x,y) = xy subject to g(x,y) = x + y — 10 = 0. Here’s the math:

Lagrangian Example - Part 1

Now we can solve the system of equations with algebra:

Lagrangian Example — Part 2

Thus, the xy-bundle optimally equals (5,5), and the shadow price is -5 ( ∂f/∂10 = 5), meaning that relaxing the constraint x + y = 10 (increasing the value of 10 to, say, 11) reduces the maximum value of f(x), which is currently 25, by 5.

This same approach can be used on a host of optimization problems, and can be adapted to even more problems through other building blocks like Hessian matrices, KKT conditions, Arrow & Mangasarian conditions, and Euler equations.

All that said, next up is the Hamiltonian.

The Hamiltonian

As mentioned, the Hamiltonian is a function used in dynamic constrained optimization problems.

We start by writing out our maximization problem, whereas our agent (see this article for relevant terms) selects the value of a control variable u(t) over time to affect a state x(t). For the problem, we maximize an objective function (our reward) subject to constraints:

Within the objective function, we have a definite integral that adds up the pay-offs derived from each continuous moment in time. That payoff in this moment, in turn, is defined by a function F, which in reduced form depends upon the current state x(t), the choice u(t), and the time period t. The e block is a discount raised to a constant p - this is what discounts all future periods beyond the current period t up to the final period T.

Finally, the rightmost part of the function, also multiplied by a discount factor, is the terminal payoff—that’s a one-time reward or penalty dependent only on the final state at time T. It can be zero if there’s no such reward, and this just depends on the problem setup (if we’re dealing with an infinite time horizon optimization problem, then we also drop the Φ-term—it only applies in finite-horizon problems). Like so, the core part of the equation is the integral, which is really just a discounted reward stream given a current state, choice, and time.

Next, the objective function is subject to a law of motion x(t) - keep in mind that anything with a dot over represents the time-derivative of that variable, or the partial derivative with respect to t. That determines how choices (controls) translate into states, since choices do not deterministically predict states. We have a vector of these control variables representing all the various choices for its value, and a vector of state variables which evolves depending on the current state, chosen controls, and any exogenous shocks.

With that setup in place, we can construct the Hamiltonian function. We start by introducing (this is why we looked at the Lagrangian first!) a time-varying Lagrangian multiplier λ(t).

This makes the term inside the brackets the current-value Lagrangian with the law of motion g(x,u,t) and the objective function F(x,u,t). Now, we’ll integrate by parts to pull the constraint outside of the integral, with an incorporation of the costate equation through the maximum principle (I’m skipping the derivation here to focus on the concept of the Hamiltonian—there are great videos are textbooks out there walking through this step by step!):

Now, we collect everything inside the integral and rename it the Hamiltonian, termed as H:

Making the objective function:

With this setup, H already embeds the shadow, or marginal, value of slightly altering the state. This means that maximizing H at every continuous instant is the same as maximizing the whole intertemporal objective function and solving the problem.

Through the Hamiltonian, we can interpret the problem with the FOCs for the state, costate, and control equations, and the boundary conditions:

The first FOC of u(t) implies that a control is chosen today, given today’s marginal effect, such that slight state change can’t raise or lower the Hamiltonian.

The next, of x(t), says that the shadow price must change over time to keep track of how valuable a marginal unit of that state is.

The third FOC hardwires the original law of motion back in.

These three differential equations—the FOCs—still don’t give us a unique solution. Instead, since we have two first-order unknowns s(t) and λ(t), we need two anchors to pin down a unique trajectory. That means both a starting and a terminal (final) condition. Here they are:

The initial state condition x(0) = x0 is the starting stock for the variable reprsented by x — that could be a firm’s capital stock, consumer’s wealth, pollution stock, or so on. This starting value has to be exogenouesly fixed since the solution method is fundamentally forward-looking. After the fixed initial state, the state differential equation can take over to produce a trajectory going forward.

The terminal boundary condition (calculated by merging a boundary term for the integration with the discounted terminal reward, varying x(T), and setting the variation of total payoff to zero) states that the shadow value of one more unit of the state at period T—the marginal state effect—equals the direct marginal utility that the terminal payoff assigns to that extra unit. Intuitively, we’re setting marginal values equal to one another just like we would anywhere else, such that in the last period keeping or discarding a state unit doesn’t matter.

So, you can visualize these boundary conditions like the entrance and exit to the maze of possibilities within the phase space. With them, we can use the FOCs to solve forward and backward until the paths meet. That resulting path, often calculated numerically (in practice, meaning you let a computer guess a bunch and see what sticks!), is the optimal trajectory and the solution to the dynamic constrained optimization problem.

Hamiltonian Phase Space With Boundary Conditions

So, there you have it! Here’s a summary of what we’ve covered:

  • Physics roots: Newtonian forces, Lagrangian trade‑offs, and the Hamiltonian phase‑space.
  • Economic mapping: states and constraints as framing optimization problems.
  • Lagrangian optimization: static optimization with an objective function and constraints, solved with a Lagrangian multiplier.
  • Lagrange FOCs: partial derivatives set to zero that deliver the bundle and shadow value optimum.
  • The Hamiltonian & optimal control theory: dynamic optimization, laws of motion, costate functions, and the phase space.
  • Hamiltonian FOCs: state, costate, and control equations, along with the boundary conditions, determining the optimal path over time.

Hopefully, this helps you with all kinds of constrained optimization problems, which are a powerful tool to understand and predict real-world behavior.

If you liked this article, you’ll probably like this article I just wrote about Monte Carlo simulations, which rely on similar concepts to what we’ve employed here and are a useful applied means of simulating the real world in economics, finance, and beyond.

Or, if you’re into economics, you’re in the right place, and make sure to check out my other articles here.

--

--

Jon Law
Jon Law

Written by Jon Law

5x author - economics - 700k on YouTube @jonlawedu!

No responses yet