Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook cloud-examples/sage/Picard.ipynb

Views: 62
Visibility: Unlisted (only visible to those who know the link)
Kernel: SageMath 9.2
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 = 4; N=40; b=8; x1=-5; x2=10; f = lambda t,x: sin(t)-2*x; assume(t>0) z=[picard_iteration(f, a, c, i) for i in range(N+1)] for i in range(N+1): show(z[i]) from sage.plot.colors import rainbow c=rainbow(N+1) where = [x,-2+1.5,b] p=plot(-1/5*(cos(t)*e^(2*t) - 2*e^(2*t)*sin(t) - 21)*e^(-2*t),where,ymin=x1,ymax=x2,color='gray',gridlines=True) #SoluciĆ³n exacta. #p+=plot(z[0],where,gridlines=True) for i in range(0,N+1): p+=plot(z[i],where,ymin=x1,ymax=x2,color=c[i]) show(p)
Image in a Jupyter notebook
N=50;b=1 from sage.plot.colors import rainbow c=rainbow(N+1) where = [x,0,b] p=plot(x^0,where,color='gray',gridlines=True) for i in range(1,N+1): p+=plot(x^i,where,color=c[i]) show(p)
Image in a Jupyter notebook
x = var('x') y = function('y')(x) show(desolve(diff(y,x) - exp(x+y), y))
x = var('x') y = function('y')(x) f = desolve(diff(y,x) -exp(x+y), y, ics=[0,1]); show(f)
t = var('t') x = function('x')(t) f = desolve(diff(x,t) -sin(t) + 2*x, x, ics=[0,4]); f
-1/5*(cos(t)*e^(2*t) - 2*e^(2*t)*sin(t) - 21)*e^(-2*t)
t = var('t') x = function('x')(t) f = desolve(diff(x,t) -sin(t) + 2*x, x, ics=[0,4]); show(f)
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) assume(s>0) 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; N=2; b=.5; f = lambda t,x: exp(x+t); assume(t>0) z=[picard_iteration(f, a, c, i) for i in range(N+1)] for i in range(N+1): show(z[i]) from sage.plot.colors import rainbow c=rainbow(N+1) where = [x,-b,b] p=plot(-log(abs(-1-exp(-1)+exp(t))),where,color='gray',gridlines=True) #SoluciĆ³n exacta. #p+=plot(z[0],where,gridlines=True) for i in range(N+1): p+=plot(z[i],where,color=c[i]) show(p)
Image in a Jupyter notebook