Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

calculus tutorial

Views: 142
Kernel: Python 3 (Ubuntu Linux)
import numpy as np f = np.exp # The function x_0 = 1 # The x loc of P y_0 = f(x_0) # The y loc of P increments = [1,1/2,1/4,1/8,1/16] a_pos = x_0 + np.array(increments) a_neg = np.flip(x_0 - np.array(increments),axis=0) a = np.concatenate((a_pos,a_neg)) Q = [(_a,f(_a)) for _a in a] import pandas as pd data = {'P':[(x_0,y_0)]*len(a),'Q':Q,'slope of secant':(f(a)-y_0)/(a-x_0)} df = pd.DataFrame(data=data) df
P Q slope of secant
0 (1, 2.718281828459045) (2.0, 7.38905609893065) 4.670774
1 (1, 2.718281828459045) (1.5, 4.4816890703380645) 3.526814
2 (1, 2.718281828459045) (1.25, 3.4903429574618414) 3.088245
3 (1, 2.718281828459045) (1.125, 3.080216848918031) 2.895480
4 (1, 2.718281828459045) (1.0625, 2.893595944171761) 2.805026
5 (1, 2.718281828459045) (0.9375, 2.553589458062927) 2.635078
6 (1, 2.718281828459045) (0.875, 2.398875293967098) 2.555252
7 (1, 2.718281828459045) (0.75, 2.117000016612675) 2.405127
8 (1, 2.718281828459045) (0.5, 1.6487212707001282) 2.139121
9 (1, 2.718281828459045) (0.0, 1.0) 1.718282
%matplotlib inline import matplotlib.pyplot as plt fig = plt.gcf() fig.set_size_inches(18.5, 10.5) Qx = [q[0] for q in Q] Qy = [q[1] for q in Q] x = np.arange(min(Qx),1.3*max(Qx),0.1) plt.plot(x,f(x)) plt.scatter(Qx,Qy) plt.title(r"Secant approximations of the tangent line") for index, row in df.iterrows(): plt.plot([row['P'][0], row['Q'][0]],[row['P'][1], row['Q'][1]])
Image in a Jupyter notebook
fig.savefig('secants_arctan.png', dpi=100)