Mo Al Saleh
Mo Al Saleh

Reputation: 51

Basic Python: an equation and a plot using Jupyter Notebook not working

I keep getting error messages such as invalid syntax, or something is not defined.

here I defined the dependencies

import math #Basic math library
import numpy as np #Numpy -> tools for working with arrays
import matplotlib.pyplot as plt #Matplotlib -> tools for plotting

here I defined the constants

#CONSTANTS
pi = math.pi
omega_n = 30
xi = 0.05
beta = 0.1

here I defined the functions

O = (1/((1-beta**2)**2 + (2*xi*beta)**2)) #Capital Omega defined above
omega_d = omega_n*np.sqrt(1-xi**2) #Damped natural frequency

A = (2*xi*beta*np.cos(omega_n*t)*math.e**(-xi* omega_n* t)

C = 2*omega_n*(xi**2)*beta*math.e**(-xi*omega_n*t)
D = (1-beta**2)*np.sin(omega_d*t)
E = -2*xi*beta*np.cos(omega_d*t)




tmax = 60 #(sec) The max time
delta_t = 0.001 #(sec) The timestep
nPoints = tmax/delta_t #Number of equally spaced data points
t = np.linspace(0,tmax, int(nPoints)) # Time vector

ut = O* (A + omega_d + C)    #Transientresponse
us = O* (D + E)       #Steady-state response
rt= ut + us

plotting

#Plotting
fig = plt.figure() 
axes = fig.add_axes([0.1,0.1,2,1.5]) 
axes.plot(t,rt,label='Respond ratio of a damped system')

axes.set_xlim([0,tmax])
axes.set_xlabel('Time (sec)')
axes.set_ylabel('Respond ratio')
axes.set_title('Respond ratio-time history')
axes.grid()
axes.legend()
plt.show()

Upvotes: 1

Views: 229

Answers (1)

user13067694
user13067694

Reputation:

There are several errors here. First, you're missing a bracket in this line:

A = (2*xi*beta*np.cos(omega_n*t)*math.e**(-xi* omega_n* t))

Then you're using the variable t in the same line but you haven't defined t yet. You're defining t after this line. Similarly, you need to define tmax/delta_t before using them in nPoints

Upvotes: 1

Related Questions