This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org
Python Viv

vp.control.cartpole_model

The cart-pole linearized about the upright: A = df/ds (4 by 4) and B = df/du (4 by 1), by central differences of step h on the nonlinear dynamics, so the linear model is the one the simulation runs.

Part of the distribution vipyr; it runs from a checkout of the repository. Planned on PyPI as pip install vipyr.

Signature

vp.control.cartpole_model(h)

Parameters

h : float, required

Returns

A frozen CartpoleModelResult. Iterating it gives the fields below in order; it also has params, tests, log and check().

A : ndarray[float64]
The 4 by 4 state matrix df/ds at the upright, state (x, xdot, theta, thetadot).
B : ndarray[float64]
The 4 by 1 input matrix df/du, the force on the cart in newtons.

Raises

TypeError
An argument of the wrong type, or an unknown keyword.
vipyr.VivBuildError
An ImportError: no C compiler, or the program's C does not build.

Examples

import vipyr as vp
r = vp.control.cartpole_model(h=1e-5)
A, B = r
print(round(A[3, 2], 6), round(B[3, 0], 6))   # how fast the pole tips, and what a push does to it
assert r.check()
15.791707 -1.463415

Each example ran as written when these pages were generated, and the output shown is what it printed.

Checked against

Reference
Its own test: near the upright the nonlinear rate equals A s to 10−10 (lib/cartpole_api.viv); the LQR gains it leads to match the cart-pole post's (posts/2026-09-22-lqr-balances-the-cart-pole.viv).

Source