def exercise1(clargs): """ Exercise 1 """ # Setup pylog.info("Running exercise 1") # Setup time_max = 5 # Maximum simulation time time_step = 0.2 # Time step for ODE integration in simulation x0 = np.array([1.]) # Initial state # Integration methods (Exercises 1.a - 1.d) pylog.info("Running function integration using different methods") # Example pylog.debug("Running example plot for integration (remove)") example = example_integrate(x0, time_max, time_step) example.plot_state(figure="Example", label="Example", marker=".") # Analytical (1.a) time = np.arange(0, time_max, time_step) # Time vector x_a = analytic_function(time) analytical = Result(x_a, time) if x_a is not None else None # Euler (1.b) euler = euler_integrate(function, x0, time_max, time_step) # ODE (1.c) ode = ode_integrate(function, x0, time_max, time_step) # ODE Runge-Kutta (1.c) ode_rk = ode_integrate_rk(function_rk, x0, time_max, time_step) # Euler with lower time step (1.d) pylog.warning("Euler with smaller ts must be implemented") euler_time_step = None euler_ts_small = (euler_integrate(function, x0, time_max, euler_time_step) if euler_time_step is not None else None) # Plot integration results plot_integration_methods(analytical=analytical, euler=euler, ode=ode, ode_rk=ode_rk, euler_ts_small=euler_ts_small, euler_timestep=time_step, euler_timestep_small=euler_time_step) # Error analysis (Exercise 1.e) pylog.warning("Error analysis must be implemented") # Show plots of all results if not clargs.save_figures: plt.show() return
def exercise1(clargs): """ Exercise 1 """ # Setup pylog.info("Running exercise 1") # Setup time_max = 5 # Maximum simulation time time_step = 0.2 # Time step for ODE integration in simulation x0 = np.array([1.]) # Initial state # Integration methods (Exercises 1.a - 1.d) pylog.info("Running function integration using different methods") # Analytical (1.a) time = np.arange(0, time_max, time_step) # Time vector x_a = analytic_function(time) analytical = Result(x_a, time) if x_a is not None else None # Euler (1.b) euler = euler_integrate(function, x0, time_max, time_step) error(euler.state, analytical.state, "Euler") # ODE (1.c) ode = ode_integrate(function, x0, time_max, time_step) error(ode.state, analytical.state, "LSODA") # ODE Runge-Kutta (1.c) ode_rk = ode_integrate_rk(function_rk, x0, time_max, time_step) error(ode_rk.state, analytical.state, "Runge-Kutta") # Euler with lower time step (1.d) euler_time_step = 0.05 time_small = np.arange(0, time_max, euler_time_step) # Time vector x_a = analytic_function(time_small) analytical_ts_small = Result(x_a, time_small) euler_ts_small = (euler_integrate(function, x0, time_max, euler_time_step) if euler_time_step is not None else None) error(euler_ts_small.state, analytical_ts_small.state, "Euler (smaller ts)") # Plot integration results plot_integration_methods(analytical=analytical, euler=euler, ode=ode, ode_rk=ode_rk, euler_ts_small=euler_ts_small, euler_timestep=time_step, euler_timestep_small=euler_time_step) # Error analysis (Exercise 1.e) pylog.info("Studying integration error using different methods") dt_list = np.logspace(-3, 0, 20) # List of timesteps (powers of 10) integration_errors = [["L1", 1], ["L2", 2], ["Linf", 0]] methods = [["Euler", euler_integrate, function], ["Lsoda", ode_integrate, function], ["RK", ode_integrate_rk, function_rk]] for error_name, error_index in integration_errors: for name, integration_function, f in methods: compute_error(f, analytic_function, integration_function, x0, dt_list, time_max=time_max, figure=error_name, label=name, n=error_index) # Show plots of all results if not clargs.save_figures: plt.show() return