## Problem Sheet Question 7b

The general form of the population growth differential equation
$$ y^{'}-y+x=0, \ \ (0 \leq x \leq 1) $$
with the initial condition
$$y(0)=0$$
For h=0.2.
# Midpoint method Solution
\begin{equation}
\frac{w_{i+1}-w_i}{h}=f(x_i+\frac{h}{2},w_i+\frac{h}{2}f(x_i,w_i))
\end{equation}
Rearranging 
\begin{equation}
w_{i+1}=w_i+hf(x_i+\frac{h}{2},w_i+\frac{h}{2}f(x_i,w_i))
\end{equation}
\begin{equation}
w_{i+1}=w_i+h(k_2)
\end{equation}
\begin{equation}
k_1=w_i-x_i+2
\end{equation}
\begin{equation}
k_2=w_i+\frac{h}{2}k_1-(x_i+\frac{h}{2})+2)
\end{equation}


```python
import numpy as np
import math 
%matplotlib inline
import matplotlib.pyplot as plt # side-stepping mpl backend
import matplotlib.gridspec as gridspec # subplots
import warnings
warnings.filterwarnings("ignore")
```


```python
def myfun_xy(x,y):
    return y-x+2

#PLOTS
def Midpoint_Question2(N,IC):

    x_start=0
    INTITIAL_CONDITION=IC
    h=0.2
    N=N+1
    x=np.zeros(N)
    w=np.zeros(N)
    k_mat=np.zeros((2,N))
    k=0
    w[0]=INTITIAL_CONDITION
    x[0]=x_start
    
    for k in range (0,N-1):
        k_mat[0,k]=myfun_xy(x[k],w[k])
        k_mat[1,k]=myfun_xy(x[k]+h/2,w[k]+h/2*k_mat[0,k])
        w[k+1]=w[k]+h*(k_mat[1,k])
        x[k+1]=x[k]+h


    fig = plt.figure(figsize=(10,4))
    plt.plot(x,w,'-.o',color='blue')
    plt.title('Numerical Solution h=%s'%(h))

    # --- title, explanatory text and save
    fig.suptitle(r"$y'=y-x+2$", fontsize=20)
    plt.tight_layout()
    plt.subplots_adjust(top=0.85)    
    print('x')
    print(x)
    print('k1')
    print(k_mat[0,:])
    print('k2')
    print(k_mat[1,:])
    print('w')
    print(w)
```


```python
# Midpoint_Question2(N,IC)
Midpoint_Question2(5,1)
```


```python

```


```python

```

ff431756cecc0023e702787716c82632cd27cd87
ipynb
Jupyter Notebook
Chapter 03 - Runge Kutta/Supplementary/.ipynb_checkpoints/02_RK Mid point Example - Review Question 7b-checkpoint.ipynb
jjcrofts77/Numerical-Analysis-Python
97e4b9274397f969810581ff95f4026f361a56a2
MIT
2019-09-05T21:39:12.000Z
2022-03-26T14:00:25.000Z
Chapter 03 - Runge Kutta/Supplementary/.ipynb_checkpoints/02_RK Mid point Example - Review Question 7b-checkpoint.ipynb
jjcrofts77/Numerical-Analysis-Python
97e4b9274397f969810581ff95f4026f361a56a2
MIT
Chapter 03 - Runge Kutta/Supplementary/.ipynb_checkpoints/02_RK Mid point Example - Review Question 7b-checkpoint.ipynb
jjcrofts77/Numerical-Analysis-Python
97e4b9274397f969810581ff95f4026f361a56a2
MIT
2021-06-17T15:34:04.000Z
2022-01-14T14:53:43.000Z
Qwen/Qwen-72B
1. YES
2. YES