Skip to content
cccskills
FreePublished on GitHub

finite-horizon-lqr

Compute finite-horizon linear quadratic regulator (LQR) controls with dynamic programming for model predictive control (MPC). The skill outlines a backward Riccati recursion, forward simulation, and applying the first control at each time step.

OriginalShow the original description

Solving finite-horizon LQR via dynamic programming for MPC.

Run score Not tested

See how to install

Good for

  • Deriving finite-horizon LQR control inputs
  • Applying LQR within a model predictive control loop
  • Reviewing a backward Riccati recursion

Files included (1)

  • SKILL.md1.1 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

Finite-Horizon LQR for MPC

Problem Formulation

Minimize cost over horizon N:

J = Σ(k=0 to N-1) [x'Qx + u'Ru] + x_N' P x_N

Backward Riccati Recursion

Initialize: P_N = Q (or LQR solution for stability)

For k = N-1 down to 0:

K_k = inv(R + B'P_{k+1}B) @ B'P_{k+1}A
P_k = Q + A'P_{k+1}(A - B @ K_k)

Forward Simulation

Starting from x_0:

u_k = -K_k @ x_k
x_{k+1} = A @ x_k + B @ u_k

Python Implementation

def finite_horizon_lqr(A, B, Q, R, N, x0):
    nx, nu = A.shape[0], B.shape[1]
    K = np.zeros((nu, nx, N))
    P = Q.copy()

    # Backward pass
    for k in range(N-1, -1, -1):
        K[:,:,k] = np.linalg.solve(R + B.T @ P @ B, B.T @ P @ A)
        P = Q + A.T @ P @ (A - B @ K[:,:,k])

    # Return first control
    return -K[:,:,0] @ x0

MPC Application

At each timestep:

  1. Measure current state x
  2. Solve finite-horizon LQR from x
  3. Apply first control u_0
  4. Repeat next timestep

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

13f-analyzer

FreeSummary

Analyze SEC 13F filings to summarize a fund’s holdings and assets under management for a quarter, compare holdings across two quarters, and rank funds by the reported value of a stock position.

  • Summarizing a fund’s holdings for one quarter
  • Comparing a fund’s reported positions across quarters
  • Finding funds with the largest reported position in a stock
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

Compute active and reactive power flows at both ends of an AC power line, accounting for transformer tap ratios and phase shifts. The skill also covers apparent power, bus balance aggregation, MVA limit checks, and branch loading calculations.

  • Calculate power flow in both directions across a transmission line
  • Check branch apparent power against its MVA rating
  • Calculate and report the most heavily loaded branches
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

Prepare academic PDFs for blind review by removing matching identifying details before the References section. The skill focuses redaction on specific text matches, preserves references and self-citations, and checks that the saved document retains its pages and most of its text.

  • Anonymizing an academic paper for blind review
  • Removing author names and affiliations from a PDF
  • Checking a redacted paper for text loss
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

Check bathroom floor plans against simplified ADA-derived geometric rules for turning space, door width and swing, toilet placement, grab bars, and lavatory knee or toe clearance. It reports rule-specific violations and suggests local, traceable layout repairs.

  • Check turning space in a bathroom plan
  • Review door width and swing clearances
  • Verify toilet and grab bar placement
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

analyze-ci

FreeSummary

Reviews failed GitHub Actions job logs for a pull request or specific job URLs. It returns a concise summary of the likely root cause, error messages, test names, and relevant log excerpts, so you can see what needs attention.

  • Summarizing failed CI jobs for a pull request
  • Investigating an individual GitHub Actions job
  • Finding the error and related test in logs
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

Extract plan-view geometry from architectural DXF files by interpreting CAD layers and geometric entities. Normalize rooms, doors, fixtures, clearances, and grab bars into consistent, machine-checkable JSON, and describe repaired geometry in a new DXF when needed.

  • Turning an architectural DXF plan into structured JSON
  • Checking room and door geometry for accessibility
  • Extracting fixture bounds and grab bar details
benchflow-ai/skillsbench1.9KUpdated Oct 24, 2026

See all skills by benchflow-ai

Report a problem with this skill