def influence_of_theta0(theta0, parameters, time, x0, theta0_test): maxposition = np.ones(len(theta0)) minposition = np.ones(len(theta0)) maxvelocity = np.ones(len(k)) period = np.ones(len(k)) ''' the change of amplitude, the range of motion, and the period in function of k ''' for i in range(0, len(theta0)): parameters.s_theta_ref1 = theta0[i] parameters.s_theta_ref2 = theta0[i] res = integrate(pendulum_integration, x0, time, args=(parameters, )) maxposition[i] = max(res.state[:, 0]) minposition[i] = min(res.state[:, 0]) maxvelocity[i] = max(res.state[:, 1]) period[i] = period_(dt, res.state[:, 1]) plt.plot(theta0, maxposition) plt.plot(theta0, minposition) plt.plot(theta0, maxvelocity) plt.plot(theta0, period) plt.title( 'The change of the amplitude, the range of motion, and the period in function of k' ) plt.legend([ 'amplitude of right position(rad)', 'amplitude of left position(rad)', 'amplitude of velocity(rad/s)', 'period of movement(s)' ]) plt.grid() plt.show() for i in range(0, 2): for theta0_ in theta0_test: parameters.s_theta_ref1 = theta0_ parameters.s_theta_ref2 = theta0_ res = integrate(pendulum_integration, x0, time, args=(parameters, )) plt.plot(time, res.state[:, i]) if i == 0: plt.title('Position-time') else: plt.title('Velocity-time') plt.legend([ 'theta01 = %s' % (theta0_test[0]), 'theta02 = %s' % (theta0_test[1]) ]) plt.grid() plt.show() return None
def influence_of_k(k,parameters,time,x0,k_test): maxposition = np.ones(len(k)) maxvelocity = np.ones(len(k)) period = np.ones(len(k)) ''' the change of amplitude and the period in function of k ''' for i in range(0,len(k)): parameters.k1 = k[i] parameters.k2 = k[i] res = integrate(pendulum_integration, x0, time, args=(parameters, )) maxposition[i] = max(res.state[:,0]) maxvelocity[i] = max(res.state[:,1]) period[i] = period_(dt,res.state[:,1]) plt.plot(k,maxposition) plt.plot(k,maxvelocity) plt.plot(k,period) plt.grid() plt.title('The change of the amplitude and the period in function of k') plt.legend(['amplitude of position(rad)','amplitude of velocity(rad/s)','period of movement(s)']) plt.show() ''' plot position and velocity seperately for 2 k''' for i in range(0,2): for k_ in k_test: parameters.k1 = k_ parameters.k2 = k_ res = integrate(pendulum_integration, x0, time, args=(parameters, )) plt.plot(time,res.state[:,i]) if i == 0: plt.title('Position-time') else: plt.title('Velocity-time') plt.legend(['k1=k2 = %s'%(k_test[0]) , 'k1=k2 = %s'%(k_test[1])]) # plt.legend(['k = s']) plt.grid() plt.show() ''' the phase ''' for k_ in k_test: parameters.k1 = k_ parameters.k2 = k_ res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_phase("Phase") plt.title('Phase') plt.legend(['k1=k2 = %s'%(k_test[0]) , 'k1=k2 = %s'%(k_test[1])]) plt.show() parameters. __init__() return None
def simulate(self): self.res = integrate(self.derivative, self.x0, self.time, args=self.args, rk=True, tol=True)
def integration(x0, time, A, name, **kwargs): """ System integration """ labels = kwargs.pop("label", ["State {}".format(i) for i in range(2)]) sys_int = integrate(ode, x0, time, args=(A, )) sys_int.plot_state("{}_state".format(name), labels) sys_int.plot_phase("{}_phase".format(name)) return
def different_theta01_theta02(theta01, theta02, parameters, time, x0): parameters.s_theta_ref1 = theta01 parameters.s_theta_ref2 = theta02 res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_state("State") plt.title('State with theta01 = %s, theta02 = %s' % (theta01, theta02)) plt.show() return None
def different_k1_k2(k1, k2, parameters, time, x0): parameters.k1 = k1 parameters.k2 = k2 res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_state("State") plt.title('State with k1 = %s, k2 = %s' % (k1, k2)) plt.show() return None
def exercise1(): """ Exercise 1 """ biolog.info("Executing Lab 4 : Exercise 1") parameters = PendulumParameters() biolog.info( "Find more information about Pendulum Parameters in SystemParameters.py" ) biolog.info("Loading default pendulum parameters") biolog.info(parameters.showParameters()) # Simulation Parameters t_start = 0.0 t_stop = 10.0 dt = 0.001 biolog.warning("Using large time step dt={}".format(dt)) time0 = np.arange(t_start, t_stop / 2., dt) x0 = [np.pi / 2., 0.3] # x0[0] = initial position in rad, x0[1] = initial velocity time1 = np.arange(t_stop / 2., t_stop, dt) x1 = [1.0, 0.7] # x0[0] = initial position in rad, x0[1] = initial velocity res0 = integrate(pendulum_integration, x0, time0, args=(parameters, )) res1 = integrate(pendulum_integration, x1, time1, args=(parameters, )) res0.plot_phase("Phase") res1.plot_phase("Phase") res0.plot_state("State") res1.plot_state("State") if DEFAULT["save_figures"] is False: plt.show() font = {'family': 'normal', 'weight': 'normal', 'size': 16} plt.rc('font', **font) plt.title( " Init_Pos_0 = $\pi/2$, Init_Vel_0 = {}, Init_Pos_1 = {}, Init_Vel_1 = {},\nK1 = {}, K2 = {}, theta_ref_1 = {}, theta_ref_2 = {}" .format(x0[1], x1[0], x1[1], parameters.k1, parameters.k2, parameters.s_theta_ref1, parameters.s_theta_ref2)) plt.minorticks_on() plt.grid(which='major', linestyle='-', linewidth='0.5', color='black') plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black') return
def different_initial_conditions(position,parameters,time): for position_ in position: x0 = [position_, 0] res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_state("State") plt.title('State') res.plot_phase("Phase") plt.title('Phase') plt.show() parameters. __init__() return None
def coupled_hopf_ocillator(): """ 4b - Coupled Hopf oscillator simulation """ param_set = [ CoupledHopfParameters(mu=[1., 1.], omega=[1.0, 1.2], k=[-0.5, -0.5]), CoupledHopfParameters(mu=[1., 1.], omega=[1.0, 2.0], k=[-0.5, -0.5]) ] for param in param_set: for x0 in [ [0.0, 1.0, 0.0, 1.0], [1e-3] * 4 ]: # There are two osc (1 and 2)=: [x-dir(1), y-dir(1), x-dir(2),y-dir(2)], for two different set of initial conditions time = np.arange(0, 30, 0.01) hopf = integrate(coupled_hopf_equation, x0, time, args=(param, )) title = "Coupled Hopf oscillator {} (x0={}, {})" label = ["x0", "x1", "x2", "x3"] label_angle = ["angle0", "angle1"] hopf.plot_state(title.format("state", x0, param), label, n_subs=2) hopf.plot_angle(title.format("angle", x0, param), label_angle) return
def hopf_ocillator(): """ 4a - Hopf oscillator simulation """ biolog.info("Hopf oscillator implemented") params = HopfParameters(mu=1., omega=1.0) time = np.arange(0, 30, 0.01) title = "Hopf oscillator {} (x0={})" label = ["x0", "x1"] x0_list = [[1e-3, 1e-3], [1.0, 1.0]] # x0 = x -direction, x-1 = y-direction for x0 in x0_list: hopf = integrate(hopf_equation, x0, time, args=(params, )) hopf.plot_state(title.format("state", x0), label) hopf_multiple = integrate_multiple( # to display different graphs on the same plot hopf_equation, x0_list, time, args=(params, )) hopf_multiple.plot_phase(title.format("phase", x0_list), label=label) return
def exercise1b(): """ Exercise 1 """ biolog.info("Executing Lab 4 : Exercise 1") parameters = PendulumParameters() ### With damping t_start = 0.0 t_stop = 10.0 dt = 0.05 biolog.warning("Using large time step dt={}".format(dt)) time = np.arange(t_start, t_stop, dt) parameters.b1 = 0.5 parameters.b2 = 0.5 x0 = [np.pi / 3, 0] biolog.info(parameters.showParameters()) res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_state("State")
def exercise3(): """ Exercise 3 """ parameters = PendulumParameters() # Checkout pendulum.py for more info biolog.info(parameters) # Simulation parameters time = np.arange(0, 30, 0.01) # Simulation time x0 = [0.1, 0.0] # Initial state # To use/modify pendulum parameters (See PendulumParameters documentation): # parameters.g = 9.81 # Gravity constant # parameters.L = 1. # Length # parameters.d = 0.3 # damping # parameters.sin = np.sin # Sine function # parameters.dry = False # Use dry friction (True or False) # Example of system integration (Similar to lab1) # (NOTE: pendulum_equation must be imlpemented first) biolog.debug("Running integration example") res = integrate(pendulum_integration, x0, time, args=(parameters, )) res.plot_state("State") res.plot_phase("Phase") # Evolutions # Write code here (You can add functions for the different cases) biolog.warning( "Evolution of pendulum in normal conditions must be implemented") biolog.warning("Evolution of pendulum without damping must be implemented") biolog.warning( "Evolution of pendulum with perturbations must be implemented") biolog.warning( "Evolution of pendulum with dry friction must be implemented") # Show plots of all results if DEFAULT["save_figures"] is False: plt.show() return