def exercise3(clargs): """ Exercise 3 """ parameters = PendulumParameters() # Checkout pendulum.py for more info pylog.info(parameters) # Simulation parameters time = np.arange(0, 30, 0.01) # Simulation time x0 = [1, 0.0] # Initial state # To use/modify pendulum parameters (See PendulumParameters documentation): # parameters.g = 9.81 # Gravity constant # parameters.m = 1. # Mass # parameters.L = 1. # Length # parameters.I = 1. # Inertia (Automatically computed!) # parameters.d = 0.3 # damping # parameters.sin = np.sin # Sine function # parameters.dry = False # Use dry friction (True or False) # --------------------------------------------------------------------------- # "Evolution of pendulum in normal conditions must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(parameters,)) res.plot_state("State - normal") res.plot_phase("Phase - normal") # --------------------------------------------------------------------------- # "Evolution of pendulum without damping must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(d=0),)) res.plot_state("State - d=0") res.plot_phase("Phase - d=0") # --------------------------------------------------------------------------- # "Evolution of pendulum with perturbations must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(), -2.0)) res.plot_state("State - torque") res.plot_phase("Phase - torque") # --------------------------------------------------------------------------- # "Evolution of pendulum with dry friction must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(dry=True), 0.0)) res.plot_state("State - dry") res.plot_phase("Phase - dry") # Show plots of all results if not clargs.save_figures: plt.show()
def evolution_dry(x0, time): """ Dry friction simulation """ pylog.info("Evolution with dry friction") parameters = PendulumParameters(d=0.03, dry=True) pylog.info(parameters) title = "{} with dry friction (x0={})" res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_perturbation(x0, time): """ Perturbation and no damping simulation """ pylog.info("Evolution with perturbations") parameters = PendulumParameters(d=0.0) pylog.info(parameters) title = "{} with perturbation (x0={})" res = integrate(pendulum_perturbation, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_no_damping(x0, time): """ No damping simulation """ pylog.info("Evolution with no damping") parameters = PendulumParameters(d=0.0) pylog.info(parameters) title = "{} without damping (x0={})" res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_cases(time): """ Normal simulation """ pylog.info("Evolution with basic paramater") x0_cases = [["Normal", [0.1, 0]], ["Stable", [0.0, 0.0]], ["Unstable", [np.pi, 0.0]], ["Multiple loops", [0.1, 10.0]]] title = "{} case {} (x0={})" parameters = PendulumParameters() for name, x0 in x0_cases: res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format(name, "state", x0)) res.plot_phase(title.format(name, "phase", x0))
def fixed_points_analysis(): """ Exercise 3a - Compute and analyse fixed points """ g, L, d, time = sp.symbols("g, L, d, t") params = PendulumParameters(g=g, L=L, d=d, sin=sp.sin) theta = sp.Function('theta')(time) dt_sym = theta.diff(time) d2t_sym = theta.diff(time, 2) # System sys_matrix = sp.Matrix(pendulum_system([theta, dt_sym], parameters=params)) sys = sp.Equality(sp.Matrix(np.array([[dt_sym], [d2t_sym]])), sys_matrix) pylog.info(u"Pendulum system:\n{}".format(sp.pretty(sys))) # Solve when dtheta = 0 and d2theta=0 dt = 0 d2t = 0 t_sol = sp.solveset( sp.Equality(d2t, pendulum_equation(theta, dt, parameters=params)), theta # Theta, the variable to be solved ) sys_fixed = sp.Equality(sp.Matrix(np.array([[0], [0]])), sys_matrix) pylog.info(u"Solution for fixed points:\n{}\n{}\n{}\n{}".format( sp.pretty(sys_fixed), sp.pretty(sp.Equality(theta, t_sol)), sp.pretty(sp.Equality(dt_sym, dt)), sp.pretty(sp.Equality(d2t_sym, d2t)))) # Alternative way of solving sol = sp.solve(sys_fixed, [theta, dt_sym]) pylog.info(u"Solution using an alternative way of solving:\n{}".format( sp.pretty(sol))) # Jacobian, eigenvalues and eigenvectors jac = sys_matrix.jacobian(sp.Matrix([theta, dt_sym])) eigenvals = jac.eigenvals() eigenvects = jac.eigenvects() pylog.info(u"Jacobian:\n{}\nEigenvalues:\n{}\nEigenvectors:\n{}".format( sp.pretty(jac), sp.pretty(eigenvals), sp.pretty(eigenvects))) # Stability analysis for i, s in enumerate(sol): pylog.info(u"Case {}:\n{}".format( i + 1, sp.pretty(sp.Equality(sp.Matrix([theta, dt_sym]), sp.Matrix(s))))) res = [None for _ in eigenvals.keys()] for j, e in enumerate(eigenvals.keys()): res[j] = e.subs({theta: s[0], dt_sym: s[1], g: 9.81, L: 1, d: 0.1}) pylog.info(u"{}\n{}".format( sp.pretty(sp.Equality(e, res[j])), "{} {}".format(sp.re(res[j]), "> 0" if sp.re(res[j]) > 0 else "< 0"))) fixed_point_type = None if all([sp.re(r) < 0 for r in res]): fixed_point_type = " stable point" elif all([sp.re(r) > 0 for r in res]): fixed_point_type = "n unstable point" else: fixed_point_type = " saddle point" pylog.info("Fixed point is a{}".format(fixed_point_type))
def exercise3(clargs): """ Exercise 3 """ fixed_points() # Optional parameters = PendulumParameters() # Checkout pendulum.py for more info pylog.info(parameters) # Simulation parameters time = np.arange(0, 30, 0.01) # Simulation time x0 = [0.1, 0.0] # Initial state # Evolutions evolution_cases(time) x0 = [0.1, 0] evolution_no_damping(x0, time) evolution_perturbation(x0, time) evolution_dry(x0, time) # Show plots of all results if not clargs.save_figures: plt.show()
def exercise3(clargs): """ Exercise 3 """ parameters = PendulumParameters() # Checkout pendulum.py for more info pylog.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.m = 1. # Mass # parameters.L = 1. # Length # parameters.I = 1. # Inertia (Automatically computed!) # 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) pylog.debug("Running integration example") res = integrate(pendulum_system, x0, time, args=(parameters,)) res.plot_state("State") res.plot_phase("Phase") # Evolutions # Write code here (You can add functions for the different cases) pylog.warning( "Evolution of pendulum in normal conditions must be implemented" ) pylog.warning( "Evolution of pendulum without damping must be implemented" ) pylog.warning( "Evolution of pendulum with perturbations must be implemented" ) pylog.warning( "Evolution of pendulum with dry friction must be implemented" ) # Show plots of all results if not clargs.save_figures: plt.show()