sympy – proof of the derivative of sin(x) is cos(x)

In IPython-notebook on the web browser interface, checking value of expression is simply typing the name of expression in the following line.

expr0 = expand_trig(sin(x + h))
expr0

\sin{\left (h \right )} \cos{\left (x \right )} + \sin{\left (x \right )} \cos{\left (h \right )}

from sympy import *

def pprints(func, *funcs):
    breaker = '='*30
    pprint(func)
    print breaker
    if funcs is None:
        return

    for f in funcs:
        pprint(f)
        print breaker

init_printing()

# proof of deriative of sin(x) is cos(x)
# Find the limit of function f = (sin(x + h) - sin(x))/h, 
# where h approches to 0
x, h = symbols('x h')

# sin(x + h) = sin(x)*cos(h) + cos(x)*sin(h)
expr0 = expand_trig(sin(x + h))
pprint(expr0)

# sin(x)*cos(h) + cos(x)*sin(h) - sin(x)
expr1 = collect(expr0 - sin(x), sin(x))
pprint(expr1)

# The expression of the above limit of sin(x)
expr2 = expr1 / h
pprint(expr2)

# Apply limit to only h terms
# limit of the cosine term is 0
l_c = limit((cos(h) -1)/h, h, 0)

# limit of the sine term is 1
l_s = limit(sin(h)/h, h, 0)
pprints(l_c, l_s)

# The derivative of sin(x) is cos(x) followed by
expr_final = l_c * sin(x) + l_s * cos(x)
pprint(expr_final)

# numerical analysis of limit(sin(h)/h, h, 0)
# h is radian, 0 <= h <= 1
# delta_v = sin(h) - h 
f = 1/(2**x)
H = [f.subs(x, n) for n in range(20)]
n1 = H[1]
print n1, type(n1), type(n1.evalf()), float(n1)

ss = ' '
print "{0}h {1}sin(h) {2}sin(h)-h {3}sin(h)/h".format(
                            ss*10, ss*20, ss*17, ss*15)
# As h approaches to zero, sin(h)'s value become close to h.
# so limit of sin(h)/h will be 1/1, which becomes 1 eventually.
for h in H:
    sin_h = sin(h)
    delta_v = h - sin_h
    sin_h_h = sin_h/h
    # print h, sin_h.evalf(), delta_v.evalf(), sin_h_h.evalf()
    print "{:2.20f}  {:2.20f}  {:2.20f}  {:2.20f}".format(
             float(h.evalf()), float(sin_h.evalf()), float(delta_v.evalf()), float(sin_h_h.evalf()))

# Let's draw the graph of sin(x)/x 
plot(sin(x)/x, (x, -6, 6), ylim=(-1, 1))

# end of the proof

### output dump
sin(h)⋅cos(x) + sin(x)⋅cos(h)
(cos(h) - 1)⋅sin(x) + sin(h)⋅cos(x)
(cos(h) - 1)⋅sin(x) + sin(h)⋅cos(x)
───────────────────────────────────
                 h                 
0
==============================
1
==============================
cos(x)
1/2 <class 'sympy.core.numbers.Half'> <class 'sympy.core.numbers.Float'> 0.5
          h                     sin(h)                  sin(h)-h                sin(h)/h
1.00000000000000000000  0.84147098480789650488  0.15852901519210349512  0.84147098480789650488
0.50000000000000000000  0.47942553860420300538  0.02057446139579700156  0.95885107720840601075
0.25000000000000000000  0.24740395925452293713  0.00259604074547707024  0.98961583701809174851
0.12500000000000000000  0.12467473338522769288  0.00032526661477231004  0.99739786708182154307
0.06250000000000000000  0.06245931784238020062  0.00004068215761980141  0.99934908547808321000
0.03125000000000000000  0.03124491398532608030  0.00000508601467392126  0.99983724753043456968
0.01562500000000000000  0.01562436422488337230  0.00000063577511662783  0.99995931039253582728
0.00781250000000000000  0.00781242052738283059  0.00000007947261716895  0.99998982750500231553
0.00390625000000000000  0.00390624006590011659  0.00000000993409988345  0.99999745687042984610
0.00195312500000000000  0.00195312375823680404  0.00000000124176319597  0.99999936421724366920
0.00097656250000000000  0.00097656234477957831  0.00000000015522042170  0.99999984105428818548
0.00048828125000000000  0.00048828123059744660  0.00000000001940255341  0.99999996026357063084
0.00024414062500000000  0.00024414062257468080  0.00000000000242531920  0.99999999006589257444
0.00012207031250000000  0.00012207031219683510  0.00000000000030316490  0.99999999751647317137
0.00006103515625000000  0.00006103515621210439  0.00000000000003789561  0.99999999937911832060
0.00003051757812500000  0.00003051757812026305  0.00000000000000473695  0.99999999984477960790
0.00001525878906250000  0.00001525878906190788  0.00000000000000059212  0.99999999996119492973
0.00000762939453125000  0.00000762939453117599  0.00000000000000007401  0.99999999999029876019
0.00000381469726562500  0.00000381469726561575  0.00000000000000000925  0.99999999999757471780
0.00000190734863281250  0.00000190734863281134  0.00000000000000000116  0.99999999999939370721

e2.4-69

About janpenguin

Email: janpenguin [at] riseup [dot] net Every content on the blog is made by Free and Open Source Software in GNU/Linux.
This entry was posted in GNU/Linux, Python, Science and technologies and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s