First-Order System Exercises#

🧠 Quick Theory Recap#

A first-order linear system can be expressed as:

\[ m \dot{v}(t) + b v(t) = k_u u(t) - m g \sin(\theta(t)) \]

or equivalently:

\[ \dot{v}(t) = A v(t) + B u(t) + w(t) \]

where:
\( A = -\frac{b}{m} \), \( B = \frac{k_u}{m} \), and \( w(t) = -g \sin(\theta(t)) \)


πŸŽ› How to Use the Streamlit Visualizer#

  1. Launch your Streamlit app for the first-order system.

  2. Adjust the sliders for:

    • Vehicle mass \(m\)

    • Drag coefficient \(b\)

    • Force coefficient \(k_u\)

    • Input step magnitude, simulation duration, and road slope \(\theta(t)\).

  3. Observe:

    • Input (u vs t) β€” throttle input

    • Disturbance (w vs t) β€” effect of road slope

    • Output (v vs t) β€” system velocity

from IPython.display import display, HTML

download_url = "https://raw.githubusercontent.com/suann124/AIinTeaching/main/exercises/cruise_control_app.py"

# Jupyter front-end download button
button_html = f"""
<a href="{download_url}" download="cruise_control_app.py">
    <button style="padding:8px 16px; font-size:16px; background-color:#1a73e8; color:white; border:none; border-radius:4px; cursor:pointer;">
        Download cruise_control_app.py
    </button>
</a>
"""

display(HTML(button_html))

Create and activate a new conda environment (Python 3.9 recommended):

conda create -n first_order_system python=3.9
conda activate first_order_system

Install Streamlit within the environment: pip install streamlit streamlit-js-eval

Make sure cruise_control_app.py is in your working directory. Launch the Streamlit visualizer with: streamlit run cruise_control_app.py

Open the provided local URL in your browser to interact with the app.

You should see a dashboard with sliders and plots like this:

#

Streamlit Dashboard#

#

Click the β€œRun Analysis” button to update the plots after adjusting the sliders.

✏️ Exercise 1 β€” Model Derivation#

Given:

\[ m \dot{v}(t) + b v(t) = k_u u(t) - m g \sin(\theta(t)) \]
  1. Derive the canonical form \( \dot{v}(t) = A v(t) + B u(t) + w(t) \).

  2. Identify the parameters:

    • \( A = -b/m \)

    • \( B = k_u/m \)

    • \( T = -1/A \)

    • \( K = -B/A \)

Parameter Sets:

Case

m (kg)

b (NΒ·s/m)

k_u (N/unit)

1

1600

300

50

2

800

200

60

πŸ“Š Check in the Visualizer:
Run these parameters and confirm that the time constant (T) and steady-state velocity match your computed values.


✏️ Exercise 2 β€” Free Response#

Assume \( u(t)=0 \) and \( \theta(t)=0 \).

  1. Solve analytically: \( v(t) = v(0)e^{A t} \).

  2. For \( A = -0.2 \) and \( v(0)=1 \), plot \( v(t) \) for 0–20 seconds.

  3. Describe how the response changes for:

    • \( A < 0 \): Stable (decays to 0)

    • \( A = 0 \): Constant

    • \( A > 0 \): Unstable (grows exponentially)

πŸ“Š Check in the Visualizer:
Set throttle to zero and vary drag (b). Observe how the system decays or diverges.


✏️ Exercise 3 β€” Forced (Step) Response#

For constant input \( u(t) = U_0 \):

\[ v(t) = v(0)e^{A t} + \frac{B U_0}{-A}(1 - e^{A t}) \]
  1. Derive \( v_{ss} = -\frac{B}{A}U_0 \).

  2. Show that after one time constant \(T\), the response reaches ~63% of its final value.

  3. Compute expected settling time (β‰ˆ 5T).

πŸ“Š Check in the Visualizer:
Apply a throttle step and confirm these behaviors.


✏️ Exercise 4 β€” Disturbance Effect#

Now consider a slope disturbance:

\[\begin{split} \theta(t) = \begin{cases} 0, & t < 10 \\ 3Β°, & 10 \le t < 20 \\ 0, & t \ge 20 \end{cases} \end{split}\]
  1. Compute \( w(t) = -g \sin(\theta(t)) \).

  2. Predict how \(v(t)\) changes when the slope rises.

  3. Derive new steady-state velocity \( v_{ss,dist} = -\frac{B u + w}{A} \).

πŸ“Š Check in the Visualizer:
Enable road slope disturbance and observe the output change.


πŸ’¬ Exercise 6 β€” Reflection#

Answer briefly:

  1. Why does the first-order model always respond smoothly (no overshoot)?

  2. Why is the time constant a measure of system β€œspeed”?

  3. What physical factors determine stability?

  4. How does adding feedback change this behavior conceptually?