Innlevering 3

Du leverer besvarelsen din som to filer:

Oppgavene 1-3: Finnes i Oppgave filen på wikisiden og besvarelsen leveres som en pdf fil gjennom Ovsys2.

Oppgave 4: Finnes her. Legg til svarene dine og lever Jupyter notebook gjennom Ovsys2.

4) The goal of this exercise is to approximate solutions of two nonlinear systems of ODEs from the book:

-) The population dynamics: \begin{align*} \frac{d y_1}{dt}& = C_1y_1 (1-b_1y_1-d_2y_2),\\ \frac{dy_2}{dt}&= -C_2y_2(1-b_2y_2-d_1y_1), \end{align*} where $y_1$ and $y_2$ denote the two competing populations, $C_1$ and $C_2$ represent the growth rates of the two populations, $d_1$ and $d_2$ govern the interaction between the two populations and $b_1$ and $b_2$ are related to the available quantity of nutrients.

-) The Baseball trajectory: \begin{align*} \frac{d{\bf x}}{dt}& = {\bf v},\\ \frac{d{\bf v}}{dt}& = {\bf F}, \end{align*} where ${\bf x}(t)=(x(t), y(t), z(t))^{T}$ denotes the position of the ball at time $t$ and ${\bf v}(t)= (v_x(t), v_y(t), v_z(t))^{T}$ its velocity. Moreover, the vector ${\bf F}$ is given by \begin{align*} F_x& =-F(v)vv_x+B\omega(v_z\sin(\phi)-v_y\cos(\phi)),\\ F_y& =-F(v)vv_y+B\omega v_x\cos(\phi),\\ F_z&= -g-F(v)vv_z-B\omega v_x\sin(\phi), \end{align*} where \begin{equation*} F(v)= 0.0039+\frac{0.0058}{1+e^{\frac{v-35}{5}}} \end{equation*} is a friction coefficient, $g=9.8$, $v$ denotes the modulus of ${\bf v}$, $B= 4.1 \cdot10^{-4}$, $\phi=1.4$ degree is the pitching angle, and $\omega=180\cdot 1,047198$ degree per second, the modulus of the angular velocity impressed to the ball from the pitcher.

a) Read the documentation of solve_ivp to understand exactly how to implement the above ODEs in the format required for the use of the function "solve_ivp" of scipy using the option "method='RK45'.

Here we implemented the population dynamics problem. Use this as an exampe for implementing the first order ODE system describing the Baseball projectory.

b) The function "generateA" provided her can be used to generate a random $n\times n$ matrix $A$, which is guaranteed to be diagonalizable. This allows us to define a linear system of ODEs $y'=Ay$, $y(t_0)=y_0$.

Examine the above code and explain briefly what the code of "generateA" does. In particular, explain why the resulting $n\times n$ matrix $A$ is guaranteed to be diagonalizable.

Write your answer here.

c) Use the code "solve_ivp" of scipy.integrate with relative tolerance rtol=2.3*1e-14, and absolute tolerance atol=1e-15 to solve the following 3 ODE problems:

  1. the population dynamics system on the time interval $[0,10]$ with initial value y0= np.array([2.0 , 2.0]). Furthermore, make a phase-space plot, i.e., plot the solution in the plane with y[0] on the x-axis and y[1] on the y-axis. What do you observe?
  2. the Baseball trajectory system on the time interval $[0,10]$ with initial velocity ${\bf v}(0)=v_0(\cos(\phi), 0 \sin(\phi))^{T}$, $v_0=38m/s$. If $\textbf{x}(0)=\textbf{0}$, after how many seconds (approximately) will the ball touch the ground, i.e., $z(t)=0$?
  3. the linear ODE system with A generated by "generateA" with $n=10$, $20$ and $30$, on the time interval $[0,1]$.

For each system plot the numerical solution obtained by "solve_ivp" on the given time intervals.

Your answer to: What did you observe?

Your answer to: After how many seconds does the ball touch the ground?

How accurate do you expect the solution to be when using tolerances rtol=2.3*1e-14, and atol=1e-15? What does it mean that you use " method='RK45' "? Read the documentation of "solve_ivp" and provide (short) answers.

Write your answer here.

d) The function "generateA" also provides approximations of the eigenvalues and eigenvectors of the matrix $A$ using the routine "numpy.linalg.eig", which computes accurate approximations of eigenvalues and eigenvectors of a given matrix $A$ of moderate size.

Use this information to provide a code, which uses the method described in Section 6.9 in the book A. Hole, Kalkulus and linear algebra, Bind 2, that generates approximations of the solution of the linear ODE problem at times $[t_0,t_1,\dots ,t_N]$ where $t_N=T$ is the final time of the interval of integration $[0,T]$, $t_{k+1}-t_k=h$, and $h=T/N$, where $N$ is the total number of time values. For comparison, plot in the same plot the numerical solution you obtain with this method and the numerical solution you obtained earlier with "solve_ivp".

Furthermore, compare also the two solutions at final time $T$ by computing the norm of the difference of the two numerical solutions. Make sure to use the same initial vector and print the value of the norm of the difference.

e) Finally, you will have to implement your own version of the following method of order $3$ invented by Kutta: \begin{align*} y_{n+1}&=y_n+\frac{h}{6}(k_1+4k_2+k_3)\\ k_1& =f(t_n,y_n)\\ k_2& =f(t_n+\frac{h}{2},y_n+\frac{h}{2}k_1)\\ k_3& =f(t_n+h,y_n-hk_1+2hk_2). \end{align*}

You find below an implementation of the Forward Euler and of the Improved Euler method. Make your implementation of the Kutta method in the same format as used for these two methods.

Next, design a numerical experiment that checks numerically the order of the methods for each of the three ODE problems: the population dynamics, the baseball trajectory and linear ODE systems.

Do you observe order $3$ for the Kutta method?

Your answer to: Do you observe order 3 for the Kutta method?