In: Physics
Use Visual Python or equivalent, to write a program that allows the user to observe the following:
Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale that best allows the observation of the motion of the oscillators. Negative masses or spring constants should not be allowed.
import numpy as np from scipy.integrate import odeint import math import matplotlib.pyplot as plt %Defining the differential equation for damped and forced harmonic oscillator def system(theta,t,b(>0),g,l,m(>0): theta1 = theta[0] theta2 = theta[1] dtheta1_dt = theta2 dtheta2_dt = -(b/m)*theta2-g*math.sin(theta1) dtheta_dt = [dtheta1_dt,dtheta2_dt] return dtheta_dt %put inputs b = 0.05 g = 9.81 l = 1 m = 1 %put initial condition theta_0 = [0,5] %time plot t = np.linspace(0,10,100) %solving ordinary differential equation theta = odeint(system,theta_0,t,args = (b(>0),g,l,m(>0)) %For animation creating plot f =1 for i in range(0,100) filename = str(f)+'.png' f = f+1 plt.figure() plt.plot([5,l*math.sin(theta[i,0]) + 5)],[5,5-l*math.cos(theta[i,0])],marker = "s") plt.xlim([0,10]) plt.ylim([0,10]) plt.savefig(filename) plt.plot(t,theta[:,0],'r-') plt.plot(t,theta[:,1],'mo') plt.show()