Sharedcloud-examples / sage / Picard.ipynbOpen in CoCalc
Description: Jupyter notebook cloud-examples/sage/Picard.ipynb
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(1,N+1):
p+=plot(z[i],where,ymin=x1,ymax=x2,color=c[i])

show(p)

$4$
$-8 \, t - \cos\left(t\right) + 5$
$8 \, t^{2} - 10 \, t - \cos\left(t\right) + 2 \, \sin\left(t\right) + 5$
$-\frac{16}{3} \, t^{3} + 10 \, t^{2} - 10 \, t + 3 \, \cos\left(t\right) + 2 \, \sin\left(t\right) + 1$
$\frac{8}{3} \, t^{4} - \frac{20}{3} \, t^{3} + 10 \, t^{2} - 2 \, t + 3 \, \cos\left(t\right) - 6 \, \sin\left(t\right) + 1$
$-\frac{16}{15} \, t^{5} + \frac{10}{3} \, t^{4} - \frac{20}{3} \, t^{3} + 2 \, t^{2} - 2 \, t - 13 \, \cos\left(t\right) - 6 \, \sin\left(t\right) + 17$
$\frac{16}{45} \, t^{6} - \frac{4}{3} \, t^{5} + \frac{10}{3} \, t^{4} - \frac{4}{3} \, t^{3} + 2 \, t^{2} - 34 \, t - 13 \, \cos\left(t\right) + 26 \, \sin\left(t\right) + 17$
$-\frac{32}{315} \, t^{7} + \frac{4}{9} \, t^{6} - \frac{4}{3} \, t^{5} + \frac{2}{3} \, t^{4} - \frac{4}{3} \, t^{3} + 34 \, t^{2} - 34 \, t + 51 \, \cos\left(t\right) + 26 \, \sin\left(t\right) - 47$
$\frac{8}{315} \, t^{8} - \frac{8}{63} \, t^{7} + \frac{4}{9} \, t^{6} - \frac{4}{15} \, t^{5} + \frac{2}{3} \, t^{4} - \frac{68}{3} \, t^{3} + 34 \, t^{2} + 94 \, t + 51 \, \cos\left(t\right) - 102 \, \sin\left(t\right) - 47$
$-\frac{16}{2835} \, t^{9} + \frac{2}{63} \, t^{8} - \frac{8}{63} \, t^{7} + \frac{4}{45} \, t^{6} - \frac{4}{15} \, t^{5} + \frac{34}{3} \, t^{4} - \frac{68}{3} \, t^{3} - 94 \, t^{2} + 94 \, t - 205 \, \cos\left(t\right) - 102 \, \sin\left(t\right) + 209$
$\frac{16}{14175} \, t^{10} - \frac{4}{567} \, t^{9} + \frac{2}{63} \, t^{8} - \frac{8}{315} \, t^{7} + \frac{4}{45} \, t^{6} - \frac{68}{15} \, t^{5} + \frac{34}{3} \, t^{4} + \frac{188}{3} \, t^{3} - 94 \, t^{2} - 418 \, t - 205 \, \cos\left(t\right) + 410 \, \sin\left(t\right) + 209$
$-\frac{32}{155925} \, t^{11} + \frac{4}{2835} \, t^{10} - \frac{4}{567} \, t^{9} + \frac{2}{315} \, t^{8} - \frac{8}{315} \, t^{7} + \frac{68}{45} \, t^{6} - \frac{68}{15} \, t^{5} - \frac{94}{3} \, t^{4} + \frac{188}{3} \, t^{3} + 418 \, t^{2} - 418 \, t + 819 \, \cos\left(t\right) + 410 \, \sin\left(t\right) - 815$
$\frac{16}{467775} \, t^{12} - \frac{8}{31185} \, t^{11} + \frac{4}{2835} \, t^{10} - \frac{4}{2835} \, t^{9} + \frac{2}{315} \, t^{8} - \frac{136}{315} \, t^{7} + \frac{68}{45} \, t^{6} + \frac{188}{15} \, t^{5} - \frac{94}{3} \, t^{4} - \frac{836}{3} \, t^{3} + 418 \, t^{2} + 1630 \, t + 819 \, \cos\left(t\right) - 1638 \, \sin\left(t\right) - 815$
$-\frac{32}{6081075} \, t^{13} + \frac{4}{93555} \, t^{12} - \frac{8}{31185} \, t^{11} + \frac{4}{14175} \, t^{10} - \frac{4}{2835} \, t^{9} + \frac{34}{315} \, t^{8} - \frac{136}{315} \, t^{7} - \frac{188}{45} \, t^{6} + \frac{188}{15} \, t^{5} + \frac{418}{3} \, t^{4} - \frac{836}{3} \, t^{3} - 1630 \, t^{2} + 1630 \, t - 3277 \, \cos\left(t\right) - 1638 \, \sin\left(t\right) + 3281$
$\frac{32}{42567525} \, t^{14} - \frac{8}{1216215} \, t^{13} + \frac{4}{93555} \, t^{12} - \frac{8}{155925} \, t^{11} + \frac{4}{14175} \, t^{10} - \frac{68}{2835} \, t^{9} + \frac{34}{315} \, t^{8} + \frac{376}{315} \, t^{7} - \frac{188}{45} \, t^{6} - \frac{836}{15} \, t^{5} + \frac{418}{3} \, t^{4} + \frac{3260}{3} \, t^{3} - 1630 \, t^{2} - 6562 \, t - 3277 \, \cos\left(t\right) + 6554 \, \sin\left(t\right) + 3281$
$-\frac{64}{638512875} \, t^{15} + \frac{8}{8513505} \, t^{14} - \frac{8}{1216215} \, t^{13} + \frac{4}{467775} \, t^{12} - \frac{8}{155925} \, t^{11} + \frac{68}{14175} \, t^{10} - \frac{68}{2835} \, t^{9} - \frac{94}{315} \, t^{8} + \frac{376}{315} \, t^{7} + \frac{836}{45} \, t^{6} - \frac{836}{15} \, t^{5} - \frac{1630}{3} \, t^{4} + \frac{3260}{3} \, t^{3} + 6562 \, t^{2} - 6562 \, t + 13107 \, \cos\left(t\right) + 6554 \, \sin\left(t\right) - 13103$
$\frac{8}{638512875} \, t^{16} - \frac{16}{127702575} \, t^{15} + \frac{8}{8513505} \, t^{14} - \frac{8}{6081075} \, t^{13} + \frac{4}{467775} \, t^{12} - \frac{136}{155925} \, t^{11} + \frac{68}{14175} \, t^{10} + \frac{188}{2835} \, t^{9} - \frac{94}{315} \, t^{8} - \frac{1672}{315} \, t^{7} + \frac{836}{45} \, t^{6} + \frac{652}{3} \, t^{5} - \frac{1630}{3} \, t^{4} - \frac{13124}{3} \, t^{3} + 6562 \, t^{2} + 26206 \, t + 13107 \, \cos\left(t\right) - 26214 \, \sin\left(t\right) - 13103$
$-\frac{16}{10854718875} \, t^{17} + \frac{2}{127702575} \, t^{16} - \frac{16}{127702575} \, t^{15} + \frac{8}{42567525} \, t^{14} - \frac{8}{6081075} \, t^{13} + \frac{68}{467775} \, t^{12} - \frac{136}{155925} \, t^{11} - \frac{188}{14175} \, t^{10} + \frac{188}{2835} \, t^{9} + \frac{418}{315} \, t^{8} - \frac{1672}{315} \, t^{7} - \frac{652}{9} \, t^{6} + \frac{652}{3} \, t^{5} + \frac{6562}{3} \, t^{4} - \frac{13124}{3} \, t^{3} - 26206 \, t^{2} + 26206 \, t - 52429 \, \cos\left(t\right) - 26214 \, \sin\left(t\right) + 52433$
$\frac{16}{97692469875} \, t^{18} - \frac{4}{2170943775} \, t^{17} + \frac{2}{127702575} \, t^{16} - \frac{16}{638512875} \, t^{15} + \frac{8}{42567525} \, t^{14} - \frac{136}{6081075} \, t^{13} + \frac{68}{467775} \, t^{12} + \frac{376}{155925} \, t^{11} - \frac{188}{14175} \, t^{10} - \frac{836}{2835} \, t^{9} + \frac{418}{315} \, t^{8} + \frac{1304}{63} \, t^{7} - \frac{652}{9} \, t^{6} - \frac{13124}{15} \, t^{5} + \frac{6562}{3} \, t^{4} + \frac{52412}{3} \, t^{3} - 26206 \, t^{2} - 104866 \, t - 52429 \, \cos\left(t\right) + 104858 \, \sin\left(t\right) + 52433$
$-\frac{32}{1856156927625} \, t^{19} + \frac{4}{19538493975} \, t^{18} - \frac{4}{2170943775} \, t^{17} + \frac{2}{638512875} \, t^{16} - \frac{16}{638512875} \, t^{15} + \frac{136}{42567525} \, t^{14} - \frac{136}{6081075} \, t^{13} - \frac{188}{467775} \, t^{12} + \frac{376}{155925} \, t^{11} + \frac{836}{14175} \, t^{10} - \frac{836}{2835} \, t^{9} - \frac{326}{63} \, t^{8} + \frac{1304}{63} \, t^{7} + \frac{13124}{45} \, t^{6} - \frac{13124}{15} \, t^{5} - \frac{26206}{3} \, t^{4} + \frac{52412}{3} \, t^{3} + 104866 \, t^{2} - 104866 \, t + 209715 \, \cos\left(t\right) + 104858 \, \sin\left(t\right) - 209711$
$\frac{16}{9280784638125} \, t^{20} - \frac{8}{371231385525} \, t^{19} + \frac{4}{19538493975} \, t^{18} - \frac{4}{10854718875} \, t^{17} + \frac{2}{638512875} \, t^{16} - \frac{272}{638512875} \, t^{15} + \frac{136}{42567525} \, t^{14} + \frac{376}{6081075} \, t^{13} - \frac{188}{467775} \, t^{12} - \frac{152}{14175} \, t^{11} + \frac{836}{14175} \, t^{10} + \frac{652}{567} \, t^{9} - \frac{326}{63} \, t^{8} - \frac{26248}{315} \, t^{7} + \frac{13124}{45} \, t^{6} + \frac{52412}{15} \, t^{5} - \frac{26206}{3} \, t^{4} - \frac{209732}{3} \, t^{3} + 104866 \, t^{2} + 419422 \, t + 209715 \, \cos\left(t\right) - 419430 \, \sin\left(t\right) - 209711$
$-\frac{32}{194896477400625} \, t^{21} + \frac{4}{1856156927625} \, t^{20} - \frac{8}{371231385525} \, t^{19} + \frac{4}{97692469875} \, t^{18} - \frac{4}{10854718875} \, t^{17} + \frac{34}{638512875} \, t^{16} - \frac{272}{638512875} \, t^{15} - \frac{376}{42567525} \, t^{14} + \frac{376}{6081075} \, t^{13} + \frac{76}{42525} \, t^{12} - \frac{152}{14175} \, t^{11} - \frac{652}{2835} \, t^{10} + \frac{652}{567} \, t^{9} + \frac{6562}{315} \, t^{8} - \frac{26248}{315} \, t^{7} - \frac{52412}{45} \, t^{6} + \frac{52412}{15} \, t^{5} + \frac{104866}{3} \, t^{4} - \frac{209732}{3} \, t^{3} - 419422 \, t^{2} + 419422 \, t - 838861 \, \cos\left(t\right) - 419430 \, \sin\left(t\right) + 838865$
$\frac{32}{2143861251406875} \, t^{22} - \frac{8}{38979295480125} \, t^{21} + \frac{4}{1856156927625} \, t^{20} - \frac{8}{1856156927625} \, t^{19} + \frac{4}{97692469875} \, t^{18} - \frac{4}{638512875} \, t^{17} + \frac{34}{638512875} \, t^{16} + \frac{752}{638512875} \, t^{15} - \frac{376}{42567525} \, t^{14} - \frac{152}{552825} \, t^{13} + \frac{76}{42525} \, t^{12} + \frac{1304}{31185} \, t^{11} - \frac{652}{2835} \, t^{10} - \frac{13124}{2835} \, t^{9} + \frac{6562}{315} \, t^{8} + \frac{104824}{315} \, t^{7} - \frac{52412}{45} \, t^{6} - \frac{209732}{15} \, t^{5} + \frac{104866}{3} \, t^{4} + \frac{838844}{3} \, t^{3} - 419422 \, t^{2} - 1677730 \, t - 838861 \, \cos\left(t\right) + 1677722 \, \sin\left(t\right) + 838865$
$-\frac{64}{49308808782358125} \, t^{23} + \frac{8}{428772250281375} \, t^{22} - \frac{8}{38979295480125} \, t^{21} + \frac{4}{9280784638125} \, t^{20} - \frac{8}{1856156927625} \, t^{19} + \frac{4}{5746615875} \, t^{18} - \frac{4}{638512875} \, t^{17} - \frac{94}{638512875} \, t^{16} + \frac{752}{638512875} \, t^{15} + \frac{152}{3869775} \, t^{14} - \frac{152}{552825} \, t^{13} - \frac{652}{93555} \, t^{12} + \frac{1304}{31185} \, t^{11} + \frac{13124}{14175} \, t^{10} - \frac{13124}{2835} \, t^{9} - \frac{26206}{315} \, t^{8} + \frac{104824}{315} \, t^{7} + \frac{209732}{45} \, t^{6} - \frac{209732}{15} \, t^{5} - \frac{419422}{3} \, t^{4} + \frac{838844}{3} \, t^{3} + 1677730 \, t^{2} - 1677730 \, t + 3355443 \, \cos\left(t\right) + 1677722 \, \sin\left(t\right) - 3355439$
$\frac{16}{147926426347074375} \, t^{24} - \frac{16}{9861761756471625} \, t^{23} + \frac{8}{428772250281375} \, t^{22} - \frac{8}{194896477400625} \, t^{21} + \frac{4}{9280784638125} \, t^{20} - \frac{8}{109185701625} \, t^{19} + \frac{4}{5746615875} \, t^{18} + \frac{188}{10854718875} \, t^{17} - \frac{94}{638512875} \, t^{16} - \frac{304}{58046625} \, t^{15} + \frac{152}{3869775} \, t^{14} + \frac{1304}{1216215} \, t^{13} - \frac{652}{93555} \, t^{12} - \frac{26248}{155925} \, t^{11} + \frac{13124}{14175} \, t^{10} + \frac{52412}{2835} \, t^{9} - \frac{26206}{315} \, t^{8} - \frac{419464}{315} \, t^{7} + \frac{209732}{45} \, t^{6} + \frac{838844}{15} \, t^{5} - \frac{419422}{3} \, t^{4} - \frac{3355460}{3} \, t^{3} + 1677730 \, t^{2} + 6710878 \, t + 3355443 \, \cos\left(t\right) - 6710886 \, \sin\left(t\right) - 3355439$
$-\frac{32}{3698160658676859375} \, t^{25} + \frac{4}{29585285269414875} \, t^{24} - \frac{16}{9861761756471625} \, t^{23} + \frac{8}{2143861251406875} \, t^{22} - \frac{8}{194896477400625} \, t^{21} + \frac{4}{545928508125} \, t^{20} - \frac{8}{109185701625} \, t^{19} - \frac{188}{97692469875} \, t^{18} + \frac{188}{10854718875} \, t^{17} + \frac{38}{58046625} \, t^{16} - \frac{304}{58046625} \, t^{15} - \frac{1304}{8513505} \, t^{14} + \frac{1304}{1216215} \, t^{13} + \frac{13124}{467775} \, t^{12} - \frac{26248}{155925} \, t^{11} - \frac{52412}{14175} \, t^{10} + \frac{52412}{2835} \, t^{9} + \frac{104866}{315} \, t^{8} - \frac{419464}{315} \, t^{7} - \frac{838844}{45} \, t^{6} + \frac{838844}{15} \, t^{5} + \frac{1677730}{3} \, t^{4} - \frac{3355460}{3} \, t^{3} - 6710878 \, t^{2} + 6710878 \, t - 13421773 \, \cos\left(t\right) - 6710886 \, \sin\left(t\right) + 13421777$
$\frac{32}{48076088562799171875} \, t^{26} - \frac{8}{739632131735371875} \, t^{25} + \frac{4}{29585285269414875} \, t^{24} - \frac{16}{49308808782358125} \, t^{23} + \frac{8}{2143861251406875} \, t^{22} - \frac{8}{11464498670625} \, t^{21} + \frac{4}{545928508125} \, t^{20} + \frac{376}{1856156927625} \, t^{19} - \frac{188}{97692469875} \, t^{18} - \frac{76}{986792625} \, t^{17} + \frac{38}{58046625} \, t^{16} + \frac{2608}{127702575} \, t^{15} - \frac{1304}{8513505} \, t^{14} - \frac{26248}{6081075} \, t^{13} + \frac{13124}{467775} \, t^{12} + \frac{104824}{155925} \, t^{11} - \frac{52412}{14175} \, t^{10} - \frac{209732}{2835} \, t^{9} + \frac{104866}{315} \, t^{8} + \frac{1677688}{315} \, t^{7} - \frac{838844}{45} \, t^{6} - \frac{671092}{3} \, t^{5} + \frac{1677730}{3} \, t^{4} + \frac{13421756}{3} \, t^{3} - 6710878 \, t^{2} - 26843554 \, t - 13421773 \, \cos\left(t\right) + 26843546 \, \sin\left(t\right) + 13421777$
$-\frac{64}{1298054391195577640625} \, t^{27} + \frac{8}{9615217712559834375} \, t^{26} - \frac{8}{739632131735371875} \, t^{25} + \frac{4}{147926426347074375} \, t^{24} - \frac{16}{49308808782358125} \, t^{23} + \frac{8}{126109485376875} \, t^{22} - \frac{8}{11464498670625} \, t^{21} - \frac{188}{9280784638125} \, t^{20} + \frac{376}{1856156927625} \, t^{19} + \frac{76}{8881133625} \, t^{18} - \frac{76}{986792625} \, t^{17} - \frac{326}{127702575} \, t^{16} + \frac{2608}{127702575} \, t^{15} + \frac{26248}{42567525} \, t^{14} - \frac{26248}{6081075} \, t^{13} - \frac{52412}{467775} \, t^{12} + \frac{104824}{155925} \, t^{11} + \frac{209732}{14175} \, t^{10} - \frac{209732}{2835} \, t^{9} - \frac{419422}{315} \, t^{8} + \frac{1677688}{315} \, t^{7} + \frac{671092}{9} \, t^{6} - \frac{671092}{3} \, t^{5} - \frac{6710878}{3} \, t^{4} + \frac{13421756}{3} \, t^{3} + 26843554 \, t^{2} - 26843554 \, t + 53687091 \, \cos\left(t\right) + 26843546 \, \sin\left(t\right) - 53687087$
$\frac{32}{9086380738369043484375} \, t^{28} - \frac{16}{259610878239115528125} \, t^{27} + \frac{8}{9615217712559834375} \, t^{26} - \frac{8}{3698160658676859375} \, t^{25} + \frac{4}{147926426347074375} \, t^{24} - \frac{16}{2900518163668125} \, t^{23} + \frac{8}{126109485376875} \, t^{22} + \frac{376}{194896477400625} \, t^{21} - \frac{188}{9280784638125} \, t^{20} - \frac{8}{8881133625} \, t^{19} + \frac{76}{8881133625} \, t^{18} + \frac{652}{2170943775} \, t^{17} - \frac{326}{127702575} \, t^{16} - \frac{52496}{638512875} \, t^{15} + \frac{26248}{42567525} \, t^{14} + \frac{104824}{6081075} \, t^{13} - \frac{52412}{467775} \, t^{12} - \frac{419464}{155925} \, t^{11} + \frac{209732}{14175} \, t^{10} + \frac{838844}{2835} \, t^{9} - \frac{419422}{315} \, t^{8} - \frac{1342184}{63} \, t^{7} + \frac{671092}{9} \, t^{6} + \frac{13421756}{15} \, t^{5} - \frac{6710878}{3} \, t^{4} - \frac{53687108}{3} \, t^{3} + 26843554 \, t^{2} + 107374174 \, t + 53687091 \, \cos\left(t\right) - 107374182 \, \sin\left(t\right) - 53687087$
$-\frac{64}{263505041412702261046875} \, t^{29} + \frac{8}{1817276147673808696875} \, t^{28} - \frac{16}{259610878239115528125} \, t^{27} + \frac{8}{48076088562799171875} \, t^{26} - \frac{8}{3698160658676859375} \, t^{25} + \frac{4}{8701554491004375} \, t^{24} - \frac{16}{2900518163668125} \, t^{23} - \frac{376}{2143861251406875} \, t^{22} + \frac{376}{194896477400625} \, t^{21} + \frac{4}{44405668125} \, t^{20} - \frac{8}{8881133625} \, t^{19} - \frac{652}{19538493975} \, t^{18} + \frac{652}{2170943775} \, t^{17} + \frac{6562}{638512875} \, t^{16} - \frac{52496}{638512875} \, t^{15} - \frac{104824}{42567525} \, t^{14} + \frac{104824}{6081075} \, t^{13} + \frac{209732}{467775} \, t^{12} - \frac{419464}{155925} \, t^{11} - \frac{838844}{14175} \, t^{10} + \frac{838844}{2835} \, t^{9} + \frac{335546}{63} \, t^{8} - \frac{1342184}{63} \, t^{7} - \frac{13421756}{45} \, t^{6} + \frac{13421756}{15} \, t^{5} + \frac{26843554}{3} \, t^{4} - \frac{53687108}{3} \, t^{3} - 107374174 \, t^{2} + 107374174 \, t - 214748365 \, \cos\left(t\right) - 107374182 \, \sin\left(t\right) + 214748369$
WARNING: Some output was deleted.
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)

x = var('x')
y = function('y')(x)
show(desolve(diff(y,x) - exp(x+y), y))

$-{\left(e^{\left(x + y\left(x\right)\right)} + 1\right)} e^{\left(-y\left(x\right)\right)} = C$
x = var('x')
y = function('y')(x)
f = desolve(diff(y,x) -exp(x+y), y, ics=[0,1]); show(f)

$-{\left(e^{\left(x + y\left(x\right)\right)} + 1\right)} e^{\left(-y\left(x\right)\right)} = -{\left(e + 1\right)} e^{\left(-1\right)}$
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)

$-\frac{1}{5} \, {\left(\cos\left(t\right) e^{\left(2 \, t\right)} - 2 \, e^{\left(2 \, t\right)} \sin\left(t\right) - 21\right)} e^{\left(-2 \, t\right)}$
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)

$1$
$-e + e^{\left(t + 1\right)} + 1$
$e^{\left(-e + e^{\left(t + 1\right)}\right)}$