🟩 Complete every # TODO. Solution: 04_StateSpace_NumericalIntegration.ipynb.
import numpy as np
import matplotlib.pyplot as plt
import control
from scipy.integrate import solve_ivp
plt.rcParams["figure.figsize"] = (7, 4.2)
plt.rcParams["axes.grid"] = TrueExercise (handwritten) — identify A and B¶
🟥 From , , write and identify , (and if observing position).
Exercise “EX3” — free vibration from a state-space model¶
🟩 Build A, B, C, D for , create sys = control.ss(A,B,C,D), and get the
initial-condition response to with control.initial_response.
m, c, k = 1.0, 0.1, 1.0
A = ... # TODO
B = ... # TODO
C = ... # TODO
D = ... # TODO
sys = ... # TODO
T = np.linspace(0, 50, 1000)
x0 = [1, 0]
t_out, y_out = ... # TODO
fig, ax = plt.subplots()
ax.plot(t_out, y_out)
plt.show()Exercise “EX3b” — cross-check with solve_ivp¶
🟩 Write sdof_state(t, x, m, c, k) returning and integrate with
solve_ivp. Compare with the state-space result above.
def sdof_state(t, x, m, c, k):
A = ... # TODO
return ... # TODO
sol = ... # TODO: solve_ivp(...)
# TODO: plot both curves and print the max absolute differenceExercise “EX3c” — forced vibration¶
🟩 For , sinusoidal forcing ,N at ,rad/s: build the
state-space forcing vector and solve both via
control.forced_response and via solve_ivp on the raw ODE. They should agree.
m, k, c = 100.0, 1000.0, 25.0
w, F0 = 2.5, 1000.0
A = ... # TODO
B = ... # TODO
C = ... # TODO
D = ... # TODO
sys = ... # TODO
t = np.arange(0, 10 + 1e-9, 0.01)
u = ... # TODO
t_lsim, x_lsim = ... # TODO
def sdof_forced_state(t, x, m, c, k, F0, w):
A = ... # TODO
f = ... # TODO
return A @ x + f
sol = ... # TODO
# TODO: plot both on the same axesHomework 3 — rectangular force pulse¶
🟦 Solve numerically (use numpy.heaviside), for
. Then verify your answer by superposing a step response
starting at and a step response starting at
(control.step_response, shifted and subtracted). The two should match.
m, c, k = 1.0, 0.2, 4.0
F0, t1 = 5.0, 6.0
t = np.linspace(0, 30, 3000)
def pulse_ode(t, x, m, c, k, F0, t1):
A = ... # TODO
F = ... # TODO: F0 * (1 - heaviside(t - t1))
f = ... # TODO
return A @ x + f
sol = ... # TODO
# TODO: build the step-response superposition and compare