```python
import numpy as np
import sympy as sp
import math

import yx_ODE as p1
import unstable_ODE as p2

import matplotlib.pyplot as plt

# Needed only in Jupyter to render properly in-notebook
%matplotlib inline
```

# Chinmai Raman

## Homework 6

## C.3 Solving an ODE Numerically and Symbolically

$\frac{dy}{dx} = \frac{1}{2(y-1)},   y(0) = 1 + \sqrt{\epsilon},   x \subset [0,4],   \epsilon = 10^{-3}$

The approximation of y for x = 4 using Euler's method given a step size of 1:


```python
print p1.euler(1, 1 + np.sqrt(1e-3))[1][-1]
```

    16.9375021945


The approximation of y for x = 4 using Euler's method given a step size of 0.25:


```python
print p1.euler(0.25, 1 + np.sqrt(1e-3))[1][-1]
```

    5.43162981571


The approximation of y for x = 4 using Euler's method given a step size of 0.01:


```python
print p1.euler(0.01, 1 + np.sqrt(1e-3))[1][-1]
```

    3.01196145341


The exact solution to the differential equation can be calculated symbolically. The value of C1 depends on the epsilon value.


```python
p1.sym_solve()
```




    f(x) == sqrt(C1 + x) + 1



The graph below shows Euler's method implemented for a step size of 1 (red), 0.25 (green), and 0.01 (blue). It also shows the exact solution (yellow). The problem is hard to solve numerically because it requires an extremely large number of points to come even close to estimating the exact solution.


```python
p1.graph_euler()
```

## C.4 Demonstrating instability of an ODE

$u^` = \alpha u, u(0) = u_0$

The graphs below show that $u_k = (1 + \alpha \Delta t)^k u_0$ and that the numerical solution of the above ODE problem will oscillate if $\Delta t > \frac{-1}{\alpha}$

$\alpha = -1, \Delta t = 1.1$


```python
p2.graph(-1, 1, 1.1, 0, 20)
```

$\alpha = -1, \Delta t = 1.5$


```python
p2.graph(-1, 1, 1.5, 0, 20)
```

$\alpha = -1, \Delta t = 1.9$


```python
p2.graph(-1, 1, 1.9, 0, 120)
```

$\alpha = -1, \Delta t = 2.1$


```python
p2.graph(-1, 1, 2.1, 0, 120)
```

$\alpha = -1, \Delta t = 1$


```python
p2.graph(-1, 1, 1, 0, 120)
```

These graphs show that as $ \Delta t$ increases (when $\frac{-1}{\alpha} < \Delta t < \frac{-2}{\alpha}$), the value of u as $k -> \infty$ converges to 0, but at a decreasing rate and with oscillations that fall lower into the fourth quadrant. This is because the term $(1 + \alpha \Delta t)^k$ will converge to 0 when the value inside the parenthesis is a fraction. It follows that when the value inside the parenthesis is $>1$, the sequence will diverge, as is the case when $\Delta t > \frac{-2}{\alpha}$

9b7640324d110de98ceccf6d098c027820e6192e
ipynb
Jupyter Notebook
hw6.ipynb
chapman-phys227-2016s/hw-6-ChinmaiRaman
8e5312bf5475e6e6cee85697ec6a91e671587101
MIT
hw6.ipynb
chapman-phys227-2016s/hw-6-ChinmaiRaman
8e5312bf5475e6e6cee85697ec6a91e671587101
MIT
hw6.ipynb
chapman-phys227-2016s/hw-6-ChinmaiRaman
8e5312bf5475e6e6cee85697ec6a91e671587101
MIT
Qwen/Qwen-72B
1. YES
2. YES