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

import interpolate_exp_cos as p1
import plot_sin_eps as p2
import integrate_exp as p3
import diff_functions as p4

import matplotlib.pyplot as plt

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

# Homework 5

## Chinmai Raman

### 3/18/2016

### B.1 Interpolating a discrete function

Part 1:


```python
p1.f(-.45)
```




    -0.77671500104510183




```python
p1.S_k(-1, 1, -.45, 10)
```




    -0.93597357483441579



Part 2:

Value of function f(x) = $e^(-x^2) * cos(2\pi x)$ at x = -0.45 :

### q = 2


```python
p1.f(-.45)
```




    -0.77671500104510183




```python
p1.S_k(-1, 1, -.45, 2)
```




    1.284454251472851



Error in approximation at q = 2:


```python
p1.error(-1, 1, -.45, 2)
```




    2.0611692525179528



### q = 4


```python
p1.S_k(-1, 1, -.45, 4)
```




    2.6009207047642646



Error in approximation at q = 4:


```python
p1.error(-1, 1, -.45, 4)
```




    3.3776357058093662



### q = 8


```python
p1.S_k(-1, 1, -.45, 8)
```




    -0.79999999999999982



Error in approximation at q = 8:


```python
p1.error(-1, 1, -.45, 8)
```




    0.023284998954897995



### q= 16


```python
p1.S_k(-1, 1, -.45, 16)
```




    -0.98295202860868525



Error in approximation at q = 16:


```python
p1.error(-1, 1, -.45, 16)
```




    0.20623702756358342



### B.2 Studying a function for different parameter values

$f(x) = sin\dfrac{1}{x+eps}$ in [0,1]


```python
p2.sin_graph(0.2, 10)
```


```python
p2.multigraph(0.2, 10)
```

How large n needs to be in order for the difference between the max of the function $f(x) = sin\dfrac{1}{x+eps}$ in [0,1] using n nodes and n+10 nodes to be less than 0.1:


```python
p2.choose_n(0.2)
```




    3




```python
p2.choose_n(0.1)
```




    3




```python
p2.choose_n(0.05)
```




    1



n = 200 * eps (for an epsilon of 0.2). Increasing n further does not change the plot so that it is visible on the screen.


```python
p2.multigraph(0.2, 40)
```

### B.6 Using the trapezoid method to approximate integrals


```python
p3.graph()
```

The plot is symmetric over the y-axis. Therefore the integral of the function from -$\infty$ to $\infty$ will be the same as twice the integral of the function from 0 to $\infty$


```python
p3.T(100, 10)
```




    1.7370047738874057




```python
p3.trap(100, 10) * 2
```




    1.7370047738874057



$$\int_{-L}^{L} {e}^{-x^2} dx = 2\int_{0}^{L} {e}^{-x^2} dx$$


```python
p3.table()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>L = 2</th>
      <th>L = 4</th>
      <th>L = 6</th>
      <th>L = 8</th>
      <th>L = 10</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>n = 100</th>
      <td>1.729607</td>
      <td>1.737005</td>
      <td>1.737005</td>
      <td>1.737005</td>
      <td>1.737005</td>
    </tr>
    <tr>
      <th>n = 200</th>
      <td>1.746886</td>
      <td>1.754729</td>
      <td>1.754729</td>
      <td>1.754729</td>
      <td>1.754729</td>
    </tr>
    <tr>
      <th>n = 300</th>
      <td>1.752645</td>
      <td>1.760637</td>
      <td>1.760637</td>
      <td>1.760637</td>
      <td>1.760637</td>
    </tr>
    <tr>
      <th>n = 400</th>
      <td>1.755525</td>
      <td>1.763592</td>
      <td>1.763592</td>
      <td>1.763592</td>
      <td>1.763592</td>
    </tr>
    <tr>
      <th>n = 500</th>
      <td>1.757252</td>
      <td>1.765364</td>
      <td>1.765364</td>
      <td>1.765364</td>
      <td>1.765364</td>
    </tr>
  </tbody>
</table>
</div>




```python
p3.error()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>L = 2</th>
      <th>L = 4</th>
      <th>L = 6</th>
      <th>L = 8</th>
      <th>L = 10</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>n = 100</th>
      <td>-0.042847</td>
      <td>-0.035449</td>
      <td>-0.035449</td>
      <td>-0.035449</td>
      <td>-0.035449</td>
    </tr>
    <tr>
      <th>n = 200</th>
      <td>-0.025568</td>
      <td>-0.017725</td>
      <td>-0.017725</td>
      <td>-0.017725</td>
      <td>-0.017725</td>
    </tr>
    <tr>
      <th>n = 300</th>
      <td>-0.019808</td>
      <td>-0.011816</td>
      <td>-0.011816</td>
      <td>-0.011816</td>
      <td>-0.011816</td>
    </tr>
    <tr>
      <th>n = 400</th>
      <td>-0.016929</td>
      <td>-0.008862</td>
      <td>-0.008862</td>
      <td>-0.008862</td>
      <td>-0.008862</td>
    </tr>
    <tr>
      <th>n = 500</th>
      <td>-0.015201</td>
      <td>-0.007090</td>
      <td>-0.007090</td>
      <td>-0.007090</td>
      <td>-0.007090</td>
    </tr>
  </tbody>
</table>
</div>



The error decreases as n increases and as L increases

### B.8 Plotting functions and their derivatives

$$f'(x)={\dfrac{1}{x+\dfrac{1}{100}}}$$


```python
p4.graph(p4.diff(p4.f, 1/1000, 1, 100))
```


```python
p4.multigraph(p4.diff(p4.f, 1/1000, 1, 100), p4.fprime)
```

$$g'(x)-10{e}^{10x}sin({e}^{10x})$$


```python
p4.graph(p4.diff(p4.g, 1/1000, 1, 100))
```


```python
p4.multigraph(p4.diff(p4.g, 1/1000, 1, 100), p4.gprime)
```

$$h'(x)=(ln x)x^x+xx^{x-1}$$


```python
p4.graph(p4.diff(p4.h, 1/1000, 1, 100))
```


```python
p4.multigraph(p4.diff(p4.h, 1/1000, 1, 100), p4.hprime)
```

For the functions f(x) and h(x), an n of 100 seems to be sufficient as to make the graphs of the analytical and discrete derivatives visibly indistinguishable. For g(x), however, it is unlikely that any value of n will make the graphs indistinguishable upon superimposition, as the slope of g(x) as x -> 1 approaches very high values.


```python

```

25bc7befad89eeeb91f9cc633e79296aba60ccdd
ipynb
Jupyter Notebook
HW5.ipynb
chapman-phys227-2016s/hw-5-ChinmaiRaman
dacc14de7a02de069715561c8169f72e04703b04
MIT
HW5.ipynb
chapman-phys227-2016s/hw-5-ChinmaiRaman
dacc14de7a02de069715561c8169f72e04703b04
MIT
HW5.ipynb
chapman-phys227-2016s/hw-5-ChinmaiRaman
dacc14de7a02de069715561c8169f72e04703b04
MIT
Qwen/Qwen-72B
1. YES
2. YES