In [1]:
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:


In [3]:
p1.f(-.45)


Out[3]:
-0.77671500104510183

In [2]:
p1.S_k(-1, 1, -.45, 10)


Out[2]:
-0.93597357483441579

Part 2:

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

q = 2


In [2]:
p1.f(-.45)


Out[2]:
-0.77671500104510183

In [3]:
p1.S_k(-1, 1, -.45, 2)


Out[3]:
1.284454251472851

Error in approximation at q = 2:


In [4]:
p1.error(-1, 1, -.45, 2)


Out[4]:
2.0611692525179528

q = 4


In [5]:
p1.S_k(-1, 1, -.45, 4)


Out[5]:
2.6009207047642646

Error in approximation at q = 4:


In [6]:
p1.error(-1, 1, -.45, 4)


Out[6]:
3.3776357058093662

q = 8


In [9]:
p1.S_k(-1, 1, -.45, 8)


Out[9]:
-0.79999999999999982

Error in approximation at q = 8:


In [8]:
p1.error(-1, 1, -.45, 8)


Out[8]:
0.023284998954897995

q= 16


In [10]:
p1.S_k(-1, 1, -.45, 16)


Out[10]:
-0.98295202860868525

Error in approximation at q = 16:


In [13]:
p1.error(-1, 1, -.45, 16)


Out[13]:
0.20623702756358342

B.2 Studying a function for different parameter values

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


In [2]:
p2.sin_graph(0.2, 10)



In [2]:
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:


In [2]:
p2.choose_n(0.2)


Out[2]:
3

In [3]:
p2.choose_n(0.1)


Out[3]:
3

In [4]:
p2.choose_n(0.05)


Out[4]:
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.


In [8]:
p2.multigraph(0.2, 40)


B.6 Using the trapezoid method to approximate integrals


In [2]:
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$


In [3]:
p3.T(100, 10)


Out[3]:
1.7370047738874057

In [5]:
p3.trap(100, 10) * 2


Out[5]:
1.7370047738874057
$$\int_{-L}^{L} {e}^{-x^2} dx = 2\int_{0}^{L} {e}^{-x^2} dx$$

In [4]:
p3.table()


Out[4]:
L = 2 L = 4 L = 6 L = 8 L = 10
n = 100 1.729607 1.737005 1.737005 1.737005 1.737005
n = 200 1.746886 1.754729 1.754729 1.754729 1.754729
n = 300 1.752645 1.760637 1.760637 1.760637 1.760637
n = 400 1.755525 1.763592 1.763592 1.763592 1.763592
n = 500 1.757252 1.765364 1.765364 1.765364 1.765364

In [5]:
p3.error()


Out[5]:
L = 2 L = 4 L = 6 L = 8 L = 10
n = 100 -0.042847 -0.035449 -0.035449 -0.035449 -0.035449
n = 200 -0.025568 -0.017725 -0.017725 -0.017725 -0.017725
n = 300 -0.019808 -0.011816 -0.011816 -0.011816 -0.011816
n = 400 -0.016929 -0.008862 -0.008862 -0.008862 -0.008862
n = 500 -0.015201 -0.007090 -0.007090 -0.007090 -0.007090

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}}}$$

In [2]:
p4.graph(p4.diff(p4.f, 1/1000, 1, 100))



In [2]:
p4.multigraph(p4.diff(p4.f, 1/1000, 1, 100), p4.fprime)


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

In [3]:
p4.graph(p4.diff(p4.g, 1/1000, 1, 100))



In [5]:
p4.multigraph(p4.diff(p4.g, 1/1000, 1, 100), p4.gprime)


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

In [4]:
p4.graph(p4.diff(p4.h, 1/1000, 1, 100))



In [6]:
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.


In [ ]: