PicardIterations


In [2]:
from IPython.display import display, Math, Latex 
from IPython.html.widgets import interact
%matplotlib inline


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-c7e8d0b9fc07> in <module>()
      1 from IPython.display import display, Math, Latex
----> 2 from IPython.html.widgets import interact
      3 get_ipython().magic(u'matplotlib inline')

ImportError: No module named widgets

In [3]:
import sympy as sp
import numpy as np


def piterate(RHS, x0, y0, N):
    #PicardIterations for initial value problems
    #Solve y' = f(x,y(x)), y(x0) = y0 using sympy
    yp = y0
    for i in range(N):
        yp = y0+sp.integrate(RHS.subs(y,yp), (x,x0,x))
    Math(sp.latex(yp))
    return yp

def testinteract(x):
    print x


x = sp.symbols("x")
y = sp.symbols("y")

#Define RHS: f(x, y(x)) = ...
rhs = x-y*y #RHS of 1.order ODE
#c = piterate(RHS, 0,0, 3) #Solve nonlinear problem y'(x) = x-y*y to 3 order.
#interact(piterate, RHS = rhs, x0 = (0,1,1), y0 = (0,1,1), N = (0,10,1))
#Math(sp.latex(c))
interact(testinteract,x = (1.0,10.0,1.0))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-9feeea8972d9> in <module>()
     24 #interact(piterate, RHS = rhs, x0 = (0,1,1), y0 = (0,1,1), N = (0,10,1))
     25 #Math(sp.latex(c))
---> 26 interact(testinteract,x = (1.0,10.0,1.0))

NameError: name 'interact' is not defined

Testcase: Picard iterations


In [4]:
x,x_m,h,X = sp.symbols("x x_m h X")
sp.integrate(h/8*(1-X)**2, (X, -1, 1))
sp.integrate(h/8*(1+X)*(1-X), (X, -1, 1))
x = x_m + h/2*X
b_0 = sp.integrate(h/4*x*(1-x)*(1-X), (X, 0, sp.symbols("t")))
Math(sp.latex(b_0))
#Math(b_0)


Out[4]:
$$\frac{h^{3} t^{4}}{64} + t^{3} \left(- \frac{h^{3}}{48} + \frac{h^{2} x_{m}}{12} - \frac{h^{2}}{24}\right) + t^{2} \left(- \frac{h^{2} x_{m}}{8} + \frac{h^{2}}{16} + \frac{h x_{m}^{2}}{8} - \frac{h x_{m}}{8}\right) + t \left(- \frac{h x_{m}^{2}}{4} + \frac{h x_{m}}{4}\right)$$

In [ ]: