Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 235 additions & 0 deletions Chapter3/Active_s.ipynb

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions Chapter3/Active_s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# Define the system matrices
A = np.array([[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[-10.0, 10.0, -2.0, 2.0],
[60.0, -660.0, 12.0, -12.0]])
b1 = np.array([[0.0], [0.0], [0.0033], [-0.02]])
b2 = np.array([[0.0], [0.0], [0.0], [600.0]])
B = np.hstack((b1, b2))
C = np.array([[1.0, 0.0, 0.0, 0.0]])
D = np.array([[0.0]])

# Create the state space model of the active suspension system
active_suspension = signal.StateSpace(A, b2, C, D) # Note only Second input is used

# Define the time vector
t = np.arange(0.0, 7.0, 0.01)

# Initial condition
x0 = np.array([0.2, 0.0, 0.0, 0.0])

# Simulate initial response
t, y, x = signal.lsim(active_suspension, U=np.zeros_like(t), T=t, X0=x0)

# Plot initial response
plt.figure(figsize=(10, 6))
plt.plot(t, x[:, 0], 'k', label='x1')
plt.plot(t, x[:, 1], 'k-.', label='x2')
plt.grid()
plt.xlabel('Time (sec)')
plt.ylabel('State variables')
plt.legend()
plt.setp(plt.gca().get_lines(), linewidth=2)
plt.title('Initial Response of Active Suspension System')
plt.show()

# Define input u
u = 0.1 * (np.sin(5.0 * t) + np.sin(9.0 * t) + np.sin(13.0 * t) + np.sin(17.0 * t) + np.sin(21.0 * t))

# Simulate the system with input u and initial condition x0=0
t, y, x = signal.lsim(active_suspension, U=u, T=t, X0=np.zeros_like(x0))

# Plot the result
plt.figure(figsize=(10, 6))
plt.plot(t, x[:, 0], 'k', label='x1')
plt.plot(t, x[:, 1], 'k-.', label='x2')
plt.grid()
plt.xlabel('Time (sec)')
plt.ylabel('State variables')
plt.legend()
plt.setp(plt.gca().get_lines(), linewidth=2)
plt.title('Response of Active Suspension System with Input (Initial x0=0)')
plt.show()
167 changes: 167 additions & 0 deletions Chapter3/DCmotor.ipynb

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions Chapter3/DCmotor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# Define the system matrices
A = np.array([[0, 1, 0],
[0, 0, 4.438],
[0, -12, -24]])
b1 = np.array([[0], [0], [20]])
b2 = np.array([[0], [-7.396], [0]])
B = np.hstack((b1, b2))
C = np.array([[1, 0, 0],
[0, 1, 0]])
D = np.array([[0], [0]])

# Create the state space model of the DC motor
DC_motor = signal.StateSpace(A, b1, C, D) # Note only first input is used

# Define the time vector
t = np.arange(0.0, 4.0, 0.01)

# Generate input signal u
u = 6 * signal.square(2 * np.pi * 0.25 * t) - 3

# Simulate the system
t, y, x = signal.lsim(DC_motor, u, t)

# Plot the result
plt.figure(figsize=(10, 6))
plt.plot(t, x[:, 0], 'k', label='theta')
plt.plot(t, x[:, 1], 'k-.', label='omega')
plt.plot(t, x[:, 2], 'k:', label='i')
plt.grid()
plt.xlabel('Time (sec)')
plt.ylabel('State variables')
plt.legend()
plt.title('Simulation of DC Motor')
plt.show()
235 changes: 235 additions & 0 deletions Chapter3/DCmotor_transfun.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sympy as sp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define symbolic variables"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"s = sp.symbols('s')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the system matrices (DC motor example)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"A = sp.Matrix([[0, 1, 0],\n",
" [0, 0, 4.438],\n",
" [0, -12, -24]])\n",
"b1 = sp.Matrix([[0], [0], [20]])\n",
"b2 = sp.Matrix([[0], [-7.396], [0]])\n",
"B = sp.Matrix.hstack(b1, b2)\n",
"C = sp.Matrix([[1, 0, 0]]) # Only theta is used as output\n",
"D = sp.Matrix([[0, 0]]) # Two inputs, so D is 1x2 matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculate (sI - A)^(-1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"sI_A_inv = (s*sp.eye(A.shape[0]) - A).inv()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculate the transfer function G(s) = C(sI - A)^(-1)B + D"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"G = C*sI_A_inv*B + D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simplify expressions"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"sI_A_inv_simp = sp.simplify(sI_A_inv)\n",
"G_simp = sp.simplify(G)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Print the results"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(sI - A)^(-1) =\n",
"⎡1.0 1.0⋅s + 24.0 4.438 ⎤\n",
"⎢─── ──────────────────────────── ────────────────────────────⎥\n",
"⎢ s ⎛ 2 ⎞ ⎛ 2 ⎞⎥\n",
"⎢ s⋅⎝1.0⋅s + 24.0⋅s + 53.256⎠ s⋅⎝1.0⋅s + 24.0⋅s + 53.256⎠⎥\n",
"⎢ ⎥\n",
"⎢ 1.0⋅s + 24.0 4.438 ⎥\n",
"⎢ 0 ──────────────────────── ──────────────────────── ⎥\n",
"⎢ 2 2 ⎥\n",
"⎢ 1.0⋅s + 24.0⋅s + 53.256 1.0⋅s + 24.0⋅s + 53.256 ⎥\n",
"⎢ ⎥\n",
"⎢ -12.0 1.0⋅s ⎥\n",
"⎢ 0 ──────────────────────── ──────────────────────── ⎥\n",
"⎢ 2 2 ⎥\n",
"⎣ 1.0⋅s + 24.0⋅s + 53.256 1.0⋅s + 24.0⋅s + 53.256 ⎦\n",
"\n",
"G(s) =\n",
"⎡ 88.76 -7.396⋅s - 177.504 ⎤\n",
"⎢──────────────────────────── ────────────────────────────⎥\n",
"⎢ ⎛ 2 ⎞ ⎛ 2 ⎞⎥\n",
"⎣s⋅⎝1.0⋅s + 24.0⋅s + 53.256⎠ s⋅⎝1.0⋅s + 24.0⋅s + 53.256⎠⎦\n"
]
}
],
"source": [
"print(\"(sI - A)^(-1) =\")\n",
"sp.pprint(sI_A_inv_simp)\n",
"print(\"\\nG(s) =\")\n",
"sp.pprint(G_simp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Print the LaTeX formatted output"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"LaTeX formatted output:\n",
"$G(s) = $ \\left[\\begin{matrix}\\frac{88.76}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)} & \\frac{- 7.396 s - 177.504}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)}\\end{matrix}\\right]\n",
"$[sI - A]^{-1} = $ \\left[\\begin{matrix}\\frac{1.0}{s} & \\frac{1.0 s + 24.0}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)} & \\frac{4.438}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)}\\\\0 & \\frac{1.0 s + 24.0}{1.0 s^{2} + 24.0 s + 53.256} & \\frac{4.438}{1.0 s^{2} + 24.0 s + 53.256}\\\\0 & - \\frac{12.0}{1.0 s^{2} + 24.0 s + 53.256} & \\frac{1.0 s}{1.0 s^{2} + 24.0 s + 53.256}\\end{matrix}\\right]\n"
]
}
],
"source": [
"print(\"\\nLaTeX formatted output:\")\n",
"print(\"$G(s) = $\", sp.latex(G_simp))\n",
"print(\"$[sI - A]^{-1} = $\", sp.latex(sI_A_inv_simp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert the LaTeX formatted output to an image and display it"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\left[\\begin{matrix}\\frac{88.76}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)} & \\frac{- 7.396 s - 177.504}{s \\left(1.0 s^{2} + 24.0 s + 53.256\\right)}\\end{matrix}\\right]$"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Latex\n",
"display(Latex(\"$\" + sp.latex(G_simp) + \"$\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
36 changes: 36 additions & 0 deletions Chapter3/DCmotor_transfun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Import necessary libraries
import sympy as sp

# Define symbolic variables
s = sp.symbols('s')

# Define the system matrices (DC motor example)
A = sp.Matrix([[0, 1, 0],
[0, 0, 4.438],
[0, -12, -24]])
b1 = sp.Matrix([[0], [0], [20]])
b2 = sp.Matrix([[0], [-7.396], [0]])
B = sp.Matrix.hstack(b1, b2)
C = sp.Matrix([[1, 0, 0]]) # Only theta is used as output
D = sp.Matrix([[0, 0]]) # Two inputs, so D is 1x2 matrix

# Calculate (sI - A)^(-1)
sI_A_inv = (s*sp.eye(A.shape[0]) - A).inv()

# Calculate the transfer function G(s) = C(sI - A)^(-1)B + D
G = C*sI_A_inv*B + D

# Simplify expressions
sI_A_inv_simp = sp.simplify(sI_A_inv)
G_simp = sp.simplify(G)

# Print the results
print("(sI - A)^(-1) =")
sp.pprint(sI_A_inv_simp)
print("\nG(s) =")
sp.pprint(G_simp)

# Print the LaTeX formatted output
print("\nLaTeX formatted output:")
print("$G(s) = $", sp.latex(G_simp))
print("$[sI - A]^{-1} = $", sp.latex(sI_A_inv_simp))
Loading