Page

# Assignment 2

This time your task is to use numerical methods to solve the following initial value problem (IVP):

\( y' = -2xy \)

where \( x \in [0,3]\) and \( y(0) = 2 \).

We provide you with the exact solution to help you debug your code.

Solution:

\(y(x)=2e^{-x^2}\)

You task is to create a file "rk4.py" that contains the following functionality:

- Create a function that returns the right hand side of the given ODE.
- Create a function that returns the exact solution of the given IVP.
- Create a function that implements Runge-Kutta method of the 4th order (RK4). The function should not use global variables (i.e. use the function arguments to transfer any values to the function).
- Use the function linspace from the numpy package to create evenly spaced intervals.
- Use the function odeint from the package scipy.integrate to solve the given IVP.
- Using your RK4 function solve the given IVP.
- Using the matplotlib package create a chart with 3 subplots on it:
- a plot that compares solutions
- a plot that displays the error of the RK4 method
- a plot that displays the error of the odeint method

- Save your chart as the file my_rk4.pdf .

Submit your rk4.py script file and the generated chart my_rk4.pdf to the 'Assignment Dropbox'.

Assignment will be graded on a 10 point basis.

Due date is November 21, 2019 at 11:55pm, with 0.5 point penalty per day for late submission until the cut-off date of November 28, 2019 at 11:00am.

Last modified: Tuesday, 3 October 2023, 8:54 AM