AAE364HW4Q4.2- Colaboratory

pdf

School

Purdue University *

*We aren’t endorsed by this school

Course

33300

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

3

Uploaded by yonghyunlee8963

Report
2/9/24, 7:08 PM AAE364HW4 - Colaboratory https://colab.research.google.com/drive/13VAZQxvHP4_Il8z6xFuoRE2TD6DBRN5J#scrollTo=d5C6wXEq1MbO 1/3 import numpy as np import matplotlib.pyplot as plt from google.colab import drive drive.mount(' /content/drive') file_path = ' /content/drive/MyDrive/Datahw4/prob4hw4.npz' try: npzfile = np.load(file_path) keys = list(npzfile.keys()) print("Keys present in the NPZ file:", keys) if 't' in keys and 'y' in keys: t_array = npzfile['t.npy'] y_array = npzfile['y.npy'] print("Contents of 't' array:", t_array) print("Contents of 'y' array:", y_array) else: print("One or more keys not found in the NPZ file.") except FileNotFoundError: print("File not found at the specified path:", file_path) except Exception as e: print("An error occurred:", e) Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True). Keys present in the NPZ file: ['t', 'y'] Contents of 't' array: [0.0000e+00 1.0000e-03 2.0000e-03 ... 2.4998e+01 2.4999e+01 2.5000e+01] Contents of 'y' array: [[0.02254821] [0.02255021] [0.02255621] ... [1.03736193] [1.03735521] [0.93738454]] # Finding the first peak max_index = np.argmax(y_array) first_max_value = y_array[max_index] time_of_first_max = t_array[max_index] print("Time of the first maximum value:", time_of_first_max) print("First maximum value of y_array:", first_max_value) Time of the first maximum value: 1.6600000000000001 First maximum value of y_array: [1.87264236] # Finding the nearest t based on y input_y = 1.48 closest_index = np.abs(y_array - input_y).argmin() closest_t = t_array[closest_index] print("Input value of y:", input_y) print("Closest value of t:", closest_t) print("Corresponding value of y:", y_array[closest_index]) Input value of y: 1.48 Closest value of t: 4.65 Corresponding value of y: [1.48090872]
2/9/24, 7:08 PM AAE364HW4 - Colaboratory https://colab.research.google.com/drive/13VAZQxvHP4_Il8z6xFuoRE2TD6DBRN5J#scrollTo=d5C6wXEq1MbO 2/3 # Finding the nearest y based on t data = np.load('prob4hw4.npz') t_array = data['t'] y_array = data['y'] desired_time = 7.7 index = np.argmin(np.abs(t_array - desired_time)) corresponding_value = y_array[index] print("At t =", desired_time, ", the closest y_array value is:", corresponding_value) At t = 7.7 , the closest y_array value is: [1.2386317] # For Problem 2 a = 0.21449 b = 2.1449 def step_response(t, a, b): y = np.zeros_like(t) for i, t_val in enumerate(t): y[i] = np.trapz(np.exp(-a*(t_val - t[:i+1])) * b, t[:i+1]) return y y_step_response = step_response(t_array, a, b) plt.plot(t_array, y_array, label='Noisy Data') plt.plot(t_array, y_step_response, label='Step Response', linestyle='-') plt.xlabel('Time (t)') plt.ylabel('Output (y)') plt.title('Step Response vs. Noisy Data') plt.legend() plt.grid(True) plt.show() # For Problem 4 from scipy.signal import TransferFunction, step import matplotlib.pyplot as plt import numpy as np with np.load('prob4hw4.npz') as data: t = data['t'] w = data['y'] # 1) Calculate 'ω_n` and `ζ` from the peaks by obervation. # 2) Set the trasnfer function using the libary sig.TransferFunction. # 3) Plot the step response of the given transfer function and noisy data in one figure. (For plotting the step response, you can use the ' #Find the peaks and peaks time peak_times = np.zeros((4,1)) peaks = np.zeros((4,1)) peak_times = np.array([1.66, 4.8, 7.7, 11]) peaks = np.array([1.872, 1.3716, 1.2386317, 1.05932741]) n = np.size(peak_times) peak_times, peaks, n # Calculating the time differences between successive peaks time_differences = np.diff(peak_times) # Calculating the T average_period = np.mean(time_differences) # Calculating the omega_d omega_d = 2 * np.pi / average_period omega_d, average_period # Calculating the zeta (see the discussion) zeta = 0.04063 ( )
2/9/24, 7:08 PM AAE364HW4 - Colaboratory https://colab.research.google.com/drive/13VAZQxvHP4_Il8z6xFuoRE2TD6DBRN5J#scrollTo=d5C6wXEq1MbO 3/3 # Calculating the omeag_n (see the discussion) omega_n = omega_d / np.sqrt(1 - zeta**2) # Setting the transfer function num = [omega_n**2] den = [1, 2 * zeta * omega_n, omega_n**2] system = TransferFunction(num, den) # Step response of the system t1, response = step(system, T=t) # Plotting the step response plt.figure(figsize=(12, 6)) plt.plot(t, w, label='Observed $y(t)$') plt.plot(t, response, label='Estimated $y(t)$') plt.xlabel('t') plt.ylabel('y(t)') plt.title('Step Response of the Given $ω_n$, ζ with Noisy Step Response') plt.grid() plt.show()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help