This commit is contained in:
Imbus 2024-05-21 04:23:33 +02:00
parent 98cd3a8d1e
commit 115e21b80e
5 changed files with 180 additions and 0 deletions

38
bode.py Normal file
View file

@ -0,0 +1,38 @@
import numpy as np
import matplotlib.pyplot as plt
# Define the frequency range for the Bode plot
omega = np.logspace(-2, 2, 1000) # Frequency range from 0.01 to 100 rad/s
# Define the transfer function parameters
T_values = [0.2, 1, 5]
# Create subplots
fig, (ax_mag, ax_phase) = plt.subplots(2, 1, figsize=(10, 8))
# Plot for each T value
for T in T_values:
G_mag = 20 * np.log10(1 / np.sqrt(1 + (omega * T)**2))
G_phase = -np.degrees(np.arctan(omega * T))
ax_mag.plot(omega, G_mag, label=f'T = {T}')
ax_phase.plot(omega, G_phase, label=f'T = {T}')
# Magnitude plot settings
ax_mag.set_xscale('log')
ax_mag.set_title('Bode Magnitude Plot')
ax_mag.set_ylabel('Magnitude (dB)')
ax_mag.grid(True)
ax_mag.legend()
# Phase plot settings
ax_phase.set_xscale('log')
ax_phase.set_title('Bode Phase Plot')
ax_phase.set_ylabel('Phase (degrees)')
ax_phase.set_xlabel('Frequency (rad/s)')
ax_phase.grid(True)
ax_phase.legend()
plt.tight_layout()
plt.show()

32
nyquist.py Normal file
View file

@ -0,0 +1,32 @@
# Requires 'control' package:
# pip install control
import numpy as np
import matplotlib.pyplot as plt
import control as ctrl
def nyquist_diagram(T):
# Define the numerator and denominator of the transfer function
numerator = [1] # Numerator of the transfer function
den1 = [1, 1, 1] # First part of the denominator
den2 = [T, 1] # Second part of the denominator
# Convolve the two denominator parts
denominator = np.convolve(den1, den2)
# Create the transfer function
system = ctrl.TransferFunction(numerator, denominator)
# Generate the Nyquist plot
ctrl.nyquist_plot(system)
# Enhance plot appearance
plt.title('Nyquist Diagram')
plt.xlabel('Real')
plt.ylabel('Imaginary')
plt.grid(True)
plt.show()
# Example usage
T = 1 # Time constant
nyquist_diagram(T)

37
step.py Normal file
View file

@ -0,0 +1,37 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import TransferFunction, step
def step_rc(R, C, t_end, step_amplitude):
# Define the transfer function H(s) = 1 / (RCs + 1)
num = [1]
den = [R * C, 1]
system = TransferFunction(num, den)
# Generate time points
t = np.linspace(0, t_end, 1000)
# Calculate the step response
t, response = step(system, T=t)
# Scale the response by the step amplitude
response *= step_amplitude
# Plot the step response
plt.plot(t, response)
plt.title('Step Response of RC Circuit')
plt.xlabel('Time (s)')
plt.ylabel('Response (V)')
plt.grid(True)
plt.show()
return t, response
# Example usage
R = 1000 # Ohms
C = 1e-6 # Farads
t_end = 0.01 # seconds
step_amplitude = 5 # Volts
t, response = step_rc(R, C, t_end, step_amplitude)

32
step2.py Normal file
View file

@ -0,0 +1,32 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import TransferFunction, step
def step_response(T, t_end):
# Define the transfer function H(s) = (-Ts + 1) / (s^3 + 3s^2 + 2s + 1)
num = [-T, 1]
den = [1, 3, 2, 1]
system = TransferFunction(num, den)
# Generate time points
t = np.linspace(0, t_end, 1000)
# Calculate the step response
t, response = step(system, T=t)
# Plot the step response
plt.plot(t, response)
plt.title('Step Response of the System')
plt.xlabel('Time (s)')
plt.ylabel('Response')
plt.grid(True)
plt.show()
return t, response
# Example usage
T = 1 # Time constant
t_end = 10 # seconds
t, response = step_response(T, t_end)

41
step3.py Normal file
View file

@ -0,0 +1,41 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import TransferFunction, step, convolve
def step_response(numerator, denominator, t_end, step_amplitude):
# Define the transfer function H(s) = numerator / denominator
system = TransferFunction(numerator, denominator)
# Generate time points
t = np.linspace(0, t_end, 1000)
# Calculate the step response
t, response = step(system, T=t)
# Scale the response by the step amplitude
response *= step_amplitude
# Plot the step response
plt.plot(t, response)
plt.title('Step Response of the System')
plt.xlabel('Time (s)')
plt.ylabel('Response')
plt.grid(True)
plt.show()
return t, response
# Example usage
T = 1 # Time constant
numerator = [1] # Numerator of the transfer function
den1 = [1, 1, 1] # First part of the denominator
den2 = [T, 1] # Second part of the denominator
# Convolution of the two denominator parts
denominator = convolve(den1, den2)
t_end = 20 # Duration of the simulation
step_amplitude = 0.1 # Step amplitude
t, response = step_response(numerator, denominator, t_end, step_amplitude)