Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 127
Kernel: Python 2 (system-wide)
#MrG 2016.0624 Pythonic Calculus Executive Summary! from __future__ import division from math import sqrt,pi,sin,cos #1) define a function, ie f(x)=x**2-2 def f(x): return x**2-2 print "f(sqrt(2)) = ", f(sqrt(2))
f(sqrt(2)) = 4.4408920985e-16
#2) find the numerical derivative as a limit def diff(x,h): return (f(x+h)-f(x))/h print print "f'(2) = " for x in range(5): print diff(2,10**-x)
f'(2) = 5.0 4.1 4.01 4.001 4.00010000001
#3) find roots using newton's method as a limit def root(g): return g-f(g)/diff(g,10**-6) print print "the closest root of f(x)==0 near x=1 is:" g=1 for x in range(5): print g g=root(g)
the closest root of f(x)==0 near x=1 is: 1 1.49999975002 1.41666668056 1.41421568716 1.41421356238
#4) find the definite integral as a limit print print "the definite integral of f(x) from 0 to pi =" a=0 b=pi for x in range(5): n=10**x h=(b-a)/n l=sum([f(a+i*h)*h for i in range(n)]) r=l-f(a)*h+f(b)*h print (l+r)/2
the definite integral of f(x) from 0 to pi = 9.21995303297 4.10391738072 4.0527570242 4.05224542063 4.0522403046
#5) plots using numpy and matplotlib #5) f(x)=x**2-2 ==> f(1)==-1 #5) f'(x)=2*x ==> f'(1)==2 #5) tangent line to f(x) at x=1 is: #5) y-y1==m*(x-x1) ==> y-f(x1)==f'(x1)*(x-x1) #5) y+1==2*(x-1) ==> y+1==2*x-2 ==> y=2*x-3 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2, 8) y1=x**2-2 y2=2*x-3 plt.plot(x, y1) plt.plot(x, y2) plt.xlim((0, 2)) plt.xlabel(r'$x$') plt.ylabel(r'$y$')
<matplotlib.text.Text at 0x7fb2b4687a10>
Image in a Jupyter notebook
#6) what about parametric plots and polar plots? from numpy import * import matplotlib.pyplot as plt t = arange(0,2*pi,0.1) x = (2+3*sin(t))*cos(t) #x(theta)=r(theta)*cos(theta) y = (2+3*sin(t))*sin(t) #y(theta)=r(theta)*sin(theta) ll = plt.plot(x,y) plt.show()
Image in a Jupyter notebook