Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook calculus/calculus1/SFCFA16/assignments/project-1/project-1.ipynb

21 views
Kernel: Python 2 (SageMath)

Sage project 1

Math 110A SF City College Fall 2016

Name: [Hyungjin Ahn]


In this project we shall use the Jupyter notebook to explore the exponential functions and their derivatives. We first import the scientific functionality (modules) we need, then we start computing. We look at different exponent bases, and we graph each function with the associated tangent lines.

Part 1: Exponential Functions

We want to be able to differentiate exponential functions. We use the limit definition of the derivative for the function y=bxy=b^x: limh0bx+hbxh \lim_{h \to 0} \dfrac{b^{x+h}-b^x}{h} which leads us to computing limits of the form limh0bh1h \lim_{h \to 0} \dfrac{b^h-1}{h}

Exponent base 2

import numpy as np # import module numpy to compute arrays import matplotlib.pyplot as plt # import module matplotlib to plot graphs from scipy.misc import derivative # import module to compute derivative

Check the documentation for the command np.linspace, if necessary. (Type ?np.linspace at the command line.) Also check the documentation for derivative.

base = 2 n = 4 h = np.linspace(0.1, 0.001, n) c = 0 def exp_fun(exp): return (base)**(exp) df_approx = (exp_fun(h[1:n])-1)/h[1:n] df_true = derivative(exp_fun, c, dx=1e-6) print 'Approximations', df_approx print 'Relative error:', np.abs((df_approx-df_true)/df_true) print 'Percent error:', np.abs((df_approx-df_true)/df_true)*100
Approximations [ 0.70949443 0.70137942 0.69338746] Relative error: [ 0.0235841 0.01187662 0.00034665] Percent error: [ 2.3584102 1.18766173 0.03466537]
### You Try It! Task 1 Run the above code snippet a few times with different values for n, and take some notes on what you observe. Things to think about: can you notice a relation between the value n and the relative error? By relation, we mean a mathematical relation i.e. linear, quadratic, polynomial, logarithmic, ...
n = 2 h = np.linspace(0.1, 10**(-n), n) df_approx = (exp_fun(h[1:n])-1)/h[1:n] print results()

Exponent base 3

base = 3 df_approx = (exp_fun(h[1:n])-1)/h[1:n] df_true = derivative(exp_fun, c, dx=1e-6) print 'Approximations', df_approx print 'Relative error:', np.abs((df_approx-df_true)/df_true) print 'Percent error:', np.abs((df_approx-df_true)/df_true)*100
Approximations [ 1.14005565 1.11938829 1.09921598] Relative error: [ 0.03772338 0.01891114 0.00054951] Percent error: [ 3.7723375 1.89111355 0.05495073]

Consider the limit above as a function where the input is the base and the output is the limit. Notice that the approximations for base 2 are less than 1 while the approximations for base 3 are more than 1. Now, recall the Intermediate Value Theorem. What can we say? What can we conclude? Is there a special number such that the limit above is precisely 1?

### You Try It! Task 2 Run the above code snippet with different bases. Try the cases where b=2.7b=2.7 and b=2.8b=2.8. Write some notes about what you observe and what you may conclude.
base = 2.7 df approx = (exp_fun(h(1:n))-1)/h[1:n] df_true = derivative{exp_fun, c, dx=1e-6} print_results()

Part 2: Graphical representation

We plot the graph of the function y=2xy = 2^x together with the tangent line at x=0x=0.

def format_axes(ax): # function to make nice graph ax.axis([-2,2,0,4]) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') base = 2 x = np.linspace(-2,2) y = exp_fun(x) m = derivative(exp_fun, c, dx=1e-6) # slope of tangent line b = exp_fun(0) f, ax = plt.subplots() plt.plot(x, y, 'r', linewidth=2) # plot exponential function plt.plot(x, m*x+b, 'b', linewidth=2, linestyle='-.') # plot tangent line format_axes(ax) plt.title('Graph of $y=2^x$ with the tangent at $x=0$') plt.show()
Image in a Jupyter notebook
### You Try It! Task 3 Try graphing the curves y=bxy=b^x where b=2.7b=2.7 and b=2.8b=2.8 together with their tangent lines at x=0x=0.

Part 3 Limit as a function

Now let's treat the limit above as a function and compute its values.

def lim_fun(base): def exp_fun(exp): return (base)**(exp) return ((exp_fun(h[1:n])-1)/h[1:n])[-1] # [-1] returns the last entry in the array
### You Try It! Task 4 Use the `np.linspace` command to generate a bunch of numbers between 2.71 and 2.72 and compute the value of the function above for each one of those inputs.

Now write a conclusion about that special number such that the limit above is precisely 1. The ubiquitous ee.