Taylor series method

sympy symbols, expressions

In [1]:
import sympy as sp
sp.init_printing()
In [6]:
x = sp.symbols('x')
In [5]:
x
Out[5]:
$\displaystyle y$
In [7]:
type(x)
Out[7]:
sympy.core.symbol.Symbol
In [10]:
y = x**3 + 2*x
y
Out[10]:
$\displaystyle x^{3} + 2 x$
In [11]:
y**2
Out[11]:
$\displaystyle \left(x^{3} + 2 x\right)^{2}$
In [12]:
sp.expand(y**2)
Out[12]:
$\displaystyle x^{6} + 4 x^{4} + 4 x^{2}$
In [13]:
y.diff(x)
Out[13]:
$\displaystyle 3 x^{2} + 2$
In [ ]:
 
In [ ]:
 

multiple variables

In [15]:
a,b,c = sp.symbols(['a','b','c'])
a,b**2,c**3
Out[15]:
$\displaystyle \left( a, \ b^{2}, \ c^{3}\right)$
In [17]:
y = a+b**2-c*a
y
Out[17]:
$\displaystyle - a c + a + b^{2}$

evaluation

In [27]:
y.subs( {a:17,c:100} )
Out[27]:
$\displaystyle b^{2} - 1683$

faster evaluation with lambdify

Converts an expression to a function

In [29]:
y = x**2 +1
y
Out[29]:
$\displaystyle x^{2} + 1$
In [31]:
f = sp.lambdify( x, y   )
f
Out[31]:
<function _lambdifygenerated(x)>
In [32]:
f(3)
Out[32]:
$\displaystyle 10$
In [33]:
y = a+b**2-c*a
y
Out[33]:
$\displaystyle - a c + a + b^{2}$
In [34]:
g = sp.lambdify( (a,b,c), y   )
g
Out[34]:
<function _lambdifygenerated(a, b, c)>
In [36]:
g(5,6,70)
Out[36]:
$\displaystyle -309$

Generate numpy-friendly functions

In [42]:
f = sp.lambdify( x, x**2+1, 'numpy' )
f
Out[42]:
<function _lambdifygenerated(x)>
In [43]:
import numpy as np
xa = np.array([1,10,100])
f(xa)
Out[43]:
array([    2,   101, 10001])
In [47]:
y
Out[47]:
$\displaystyle - a c + a + b^{2}$
In [44]:
y = a+b**2-c*a
h = sp.lambdify( (a,b,c), y, 'numpy' )
In [50]:
aa = np.array([1,6,7])
ba = np.array([2,60,70])
ca = np.array([3,2,5])
h(aa,ba,ca)
Out[50]:
array([   2, 3594, 4872])
In [48]:
aa = 6
ba = np.array([2,60,70])
ca = np.array([3,2,5])
h(aa,ba,ca)
Out[48]:
array([  -8, 3594, 4876])
In [51]:
z = np.array([1,2,4])
100*z
Out[51]:
array([100, 200, 400])

python dicts

In [ ]:
 
In [ ]:
 
In [ ]:
 

a dict is a set of key-value pairs

In [19]:
d = { 'apple':'round fruit', 3:[5,6,7], 'z':15  }
d
Out[19]:
{'apple': 'round fruit', 3: [5, 6, 7], 'z': 15}
In [20]:
d['apple']
Out[20]:
'round fruit'
In [22]:
list(d.keys())
Out[22]:
['apple', 3, 'z']
In [23]:
list(d.values())
Out[23]:
['round fruit', [5, 6, 7], 15]
In [24]:
d[3]
Out[24]:
$\displaystyle \left[ 5, \ 6, \ 7\right]$
In [25]:
d[3][2] 
Out[25]:
$\displaystyle 7$
In [26]:
d['zebra'] = 'striped animal'
d
Out[26]:
{'apple': 'round fruit', 3: [5, 6, 7], 'z': 15, 'zebra': 'striped animal'}

sympy function

In [55]:
t = sp.symbols('t')
u = sp.Function('u')
u(t).diff(t,2)
Out[55]:
$\displaystyle \frac{d^{2}}{d t^{2}} u{\left(t \right)}$

Constructing Taylor polynomial approximations to the solution

In [57]:
t = sp.symbols('t')
h = sp.symbols('h')
u = sp.Function('u')
v = sp.Function('v')
p = sp.Function('p')
q = sp.Function('q')
In [58]:
uprime = u(t).diff(t)  # shorthand for deriv of u(t)
vprime = v(t).diff(t)
de = {uprime:-p(t)*v(t), vprime:q(t)*u(t)}  # our DE
In [61]:
uders = [u(t),de[uprime]]
vders = [v(t),de[vprime]]
uders
Out[61]:
$\displaystyle \left[ u{\left(t \right)}, \ - p{\left(t \right)} v{\left(t \right)}\right]$
In [63]:
uders[-1].diff(t)
Out[63]:
$\displaystyle - p{\left(t \right)} \frac{d}{d t} v{\left(t \right)} - v{\left(t \right)} \frac{d}{d t} p{\left(t \right)}$
In [64]:
uders[-1].diff(t).subs(de)
Out[64]:
$\displaystyle - p{\left(t \right)} q{\left(t \right)} u{\left(t \right)} - v{\left(t \right)} \frac{d}{d t} p{\left(t \right)}$
In [65]:
uders.append(uders[-1].diff(t).subs(de))
uders
Out[65]:
$\displaystyle \left[ u{\left(t \right)}, \ - p{\left(t \right)} v{\left(t \right)}, \ - p{\left(t \right)} q{\left(t \right)} u{\left(t \right)} - v{\left(t \right)} \frac{d}{d t} p{\left(t \right)}\right]$
In [66]:
vders.append(vders[-1].diff(t).subs(de))
vders
Out[66]:
$\displaystyle \left[ v{\left(t \right)}, \ q{\left(t \right)} u{\left(t \right)}, \ - p{\left(t \right)} q{\left(t \right)} v{\left(t \right)} + u{\left(t \right)} \frac{d}{d t} q{\left(t \right)}\right]$
In [67]:
uders.append(uders[-1].diff(t).subs(de))
uders
Out[67]:
$\displaystyle \left[ u{\left(t \right)}, \ - p{\left(t \right)} v{\left(t \right)}, \ - p{\left(t \right)} q{\left(t \right)} u{\left(t \right)} - v{\left(t \right)} \frac{d}{d t} p{\left(t \right)}, \ p^{2}{\left(t \right)} q{\left(t \right)} v{\left(t \right)} - p{\left(t \right)} u{\left(t \right)} \frac{d}{d t} q{\left(t \right)} - 2 q{\left(t \right)} u{\left(t \right)} \frac{d}{d t} p{\left(t \right)} - v{\left(t \right)} \frac{d^{2}}{d t^{2}} p{\left(t \right)}\right]$
In [83]:
uprime = u(t).diff(t)  # shorthand for deriv of u(t)
vprime = v(t).diff(t)
#de = {uprime:-p(t)*v(t), vprime:q(t)*u(t)}  # our DE
de = {uprime:-sp.sin(t)*v(t), vprime:t*u(t)}  # our DE

uders = [u(t),de[uprime]]
vders = [v(t),de[vprime]]
for k in range(4):
    uders.append(uders[-1].diff(t).subs(de))
    vders.append(vders[-1].diff(t).subs(de))
#display(uders)
#display(vders)
uTp = sum( [h**i/sp.factorial(i)*uders[i] for i in range(len(uders))] )
vTp = sum( [h**i/sp.factorial(i)*vders[i] for i in range(len(vders))] )
uTp
Out[83]:
$\displaystyle \frac{h^{5} \left(6 t^{2} u{\left(t \right)} \sin{\left(t \right)} \cos{\left(t \right)} - t^{2} v{\left(t \right)} \sin^{3}{\left(t \right)} + 4 t u{\left(t \right)} \sin^{2}{\left(t \right)} + 4 t u{\left(t \right)} \cos{\left(t \right)} - 7 t v{\left(t \right)} \sin^{2}{\left(t \right)} + 4 t v{\left(t \right)} \cos^{2}{\left(t \right)} + 6 u{\left(t \right)} \sin{\left(t \right)} + 11 v{\left(t \right)} \sin{\left(t \right)} \cos{\left(t \right)} - v{\left(t \right)} \sin{\left(t \right)}\right)}{120} + \frac{h^{4} \left(t^{2} u{\left(t \right)} \sin^{2}{\left(t \right)} + 3 t u{\left(t \right)} \sin{\left(t \right)} + 4 t v{\left(t \right)} \sin{\left(t \right)} \cos{\left(t \right)} - 3 u{\left(t \right)} \cos{\left(t \right)} + 2 v{\left(t \right)} \sin^{2}{\left(t \right)} + v{\left(t \right)} \cos{\left(t \right)}\right)}{24} + \frac{h^{3} \left(- 2 t u{\left(t \right)} \cos{\left(t \right)} + t v{\left(t \right)} \sin^{2}{\left(t \right)} - u{\left(t \right)} \sin{\left(t \right)} + v{\left(t \right)} \sin{\left(t \right)}\right)}{6} + \frac{h^{2} \left(- t u{\left(t \right)} \sin{\left(t \right)} - v{\left(t \right)} \cos{\left(t \right)}\right)}{2} - h v{\left(t \right)} \sin{\left(t \right)} + u{\left(t \right)}$
In [71]:
sp.sin(sp.pi/2)
Out[71]:
$\displaystyle 1$
In [85]:
nextY = sp.lambdify( (u(t),v(t),t,h), (uTp,vTp), 'numpy' )
In [86]:
nextY( 3,1,0,.5 )
Out[86]:
$\displaystyle \left( 2.8541666666666665, \ 1.3671875\right)$
In [87]:
[item.subs({u(t):3,v(t):0,t:0}) for item in uders]
Out[87]:
$\displaystyle \left[ 3, \ 0, \ 0, \ 0, \ -9, \ 0\right]$
In [ ]: