Simulating Nonlinear Systems
Example 9.2
Simulate a nonlinear unicycle in Python.
First, load some Python packages.
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
The state equation can be encoded via the following function f
.
def f(t, x, u, c):
= [
dxdt 3]*np.cos(x[2]),
x[3]*np.sin(x[2]),
x[4],
x[1/c[0] * u(t)[0],
1/c[1] * u(t)[1]
]return dxdt
The input function u
must also be
defined.
def u(t):
return [
15*(1+np.cos(t)),
25*np.sin(3*t)
]
Define time spans, initial values, and constants
= np.linspace(0, 50, 300)
tspan = [0,0,0,0,0]
xinit = 10
mass = 10
inertia = [mass,inertia] c
Solve differential equation:
= solve_ivp(
sol lambda t, x: f(t, x, u, c),
0], tspan[-1]],
[tspan[
xinit, =tspan
t_eval )
Let’s first plot the trajectory and instantaneous velocity.
= sol.y[3]*np.cos(sol.y[2])
xp = sol.y[3]*np.sin(sol.y[2])
yp = plt.subplots()
fig, ax 0],sol.y[1])
plt.plot(sol.y[0],sol.y[1],xp,yp)
plt.quiver(sol.y['$x$')
plt.xlabel('$y$')
plt.ylabel( plt.show()
Online Resources for Section 9.3
No online resources.