CoCalc Public Filescloud-examples / sage / Picard1.sagews
Views : 46
Compute Environment: Ubuntu 18.04 (Deprecated)
def picard_iteration(f, a, c, N):
'''
Computes the N-th Picard iterate for the IVP

x' = f(t,x), x(a) = c.

EXAMPLES:
sage: var('x t s')
(x, t, s)
sage: a = 0; c = 2
sage: f = lambda t,x: 1-x
sage: picard_iteration(f, a, c, 0)
2
sage: picard_iteration(f, a, c, 1)
2 - t
sage: picard_iteration(f, a, c, 2)
t^2/2 - t + 2
sage: picard_iteration(f, a, c, 3)
-t^3/6 + t^2/2 - t + 2
sage: var('x t s')
(x, t, s)
sage: a = 0; c = 2
sage: f = lambda t,x: (x+t)^2
sage: picard_iteration(f, a, c, 0)
2
sage: picard_iteration(f, a, c, 1)
t^3/3 + 2*t^2 + 4*t + 2
sage: picard_iteration(f, a, c, 2)
t^7/63 + 2*t^6/9 + 22*t^5/15 + 16*t^4/3 + 11*t^3 + 10*t^2 + 4*t + 2

'''
if N == 0:
return c*t**0
if N == 1:
#print integral(f(s,c*s**0), s, a, t)
x0 = lambda t: c + integral(f(s,c*s**0), s, a, t)
return expand(x0(t))
for i in range(N):
x_old = lambda s: picard_iteration(f, a, c, N-1).subs(t=s)
#print x_old(s)
x0 = lambda t: c + integral(f(s,x_old(s)), s, a, t)
return expand(x0(t))
v=var('x t s')
a = 0; c = 1
f = lambda t,x: x
z=[]
for i in range(5):
z.append(picard_iteration(f, a, c, i))
z[i]
from sage.plot.colors import rainbow
c=rainbow(7)
where = [x,a,3]
p=plot(exp(t),where,color='gray')                     #Solución exacta.
p+=plot(z[0],where,gridlines=True)
for i in range(1,5):
p+=plot(z[i],where,color=c[i])

p


1 t + 1 1/2*t^2 + t + 1 1/6*t^3 + 1/2*t^2 + t + 1 1/24*t^4 + 1/6*t^3 + 1/2*t^2 + t + 1