# Ticket #10983: sol_graphiques_1_fixed.py

File sol_graphiques_1_fixed.py, 3.0 KB (added by casamayou, 9 years ago)
Line
1r"""
2sage: t = var('t'); liste = [a + cos(t) for a in srange(0, 10, 0.1)]
3sage: g = polar_plot(liste, (t, 0, 2 * pi)); g.show(aspect_ratio=1)
4sage: f = lambda x: abs(x**2 - 1/4)
5sage: def liste_pts(u0, n):
6...     u = u0; liste = [[u0,0]]
7...     for k in range(n):
8...         v, u = u, f(u)
9...         liste.extend([[v,u], [u,u]])
10...     return(liste)
11sage: g = line(liste_pts(1.1, 8), rgbcolor = (.9,0,0))
12sage: g += line(liste_pts(-.4, 8), rgbcolor = (.01,0,0))
13sage: g += line(liste_pts(1.3, 3), rgbcolor = (.5,0,0))
14sage: g += plot(f, -1, 3, rgbcolor = 'blue')
15sage: g += plot(x, -1, 3, rgbcolor = 'green')
16sage: # g.show(aspect_ratio=1, ymin = -.2, ymax = 3)
17"""
18
19r"""
20sage: x = var('x'); y = function('y',x); DE = x^2 * diff(y, x) -  y == 0
21sage: desolve(DE, [y,x])
22c*e^(-1/x)
23sage: g = plot([c*e^(-1/x) for c in srange(-8, 8, 0.4)], (x, -3, 3))
24sage: y = var('y'); g += plot_vector_field((x^2, y), (x,-3,3), (y,-5,5))
25sage: # g.show(ymin = -5, ymax = 5)
26"""
27
28r"""
29sage: from sage.calculus.desolvers import desolve_system_rk4
30sage: f = lambda x, y:[a*x-b*x*y,-c*y+d*b*x*y]
31sage: x,y,t = var('x y t')
32sage: a, b, c, d = 1., 0.1, 1.5, 0.75
33sage: P = desolve_system_rk4(f(x,y),[x,y],
34...        ics=[0,10,5],ivar=t,end_points=15)
35sage: Ql = [ [i,j] for i,j,k in P]
36sage: p = line(Ql,color='red')
37sage: p += text("Lapins",(12,37),fontsize=10,color='red')
38sage: Qr = [ [i,k] for i,j,k in P]
39sage: p += line(Qr,color='blue')
40sage: p += text("Renards",(12,7),fontsize=10,color='blue')
41sage: p.axes_labels(["temps","population"])
42sage: # p.show(gridlines=True)
43sage: ### Deuxieme graphique
44sage: n = 10;  L = srange(6, 18, 12 / n); R = srange(3, 9, 6 / n)
45sage: def g(x,y):
46...     v = vector(f(x, y))
47...     return v/v.norm()
48...
49sage: q = plot_vector_field(g(x, y), (x, 0, 60), (y, 0, 36))
50sage: for j in range(n):
51...     P = desolve_system_rk4(f(x,y),[x,y],
52...            ics=[0,L[j],R[j]],ivar=t,end_points=15)
53...     Q = [ [j,k] for i,j,k in P]
54...     q += line(Q, color=hue(.8-j/(2*n)))
55...
56sage: q.axes_labels(["nombre de lapins","nombre de renards"])
57sage: # q.show()
58sage: import scipy; from scipy import integrate
59sage: def dX_dt(X, t=0):
60...     return [X[1], 0.5*X[1] - X[0] - X[1]**3]
61...
62sage: t = srange(0, 40, 0.01);  x0 = srange(-2, 2, 0.1)
63sage: y0 = 2.5
64sage: CI = [[i, y0] for i in x0] + [[i, -y0] for i in x0]
65sage: def g(x,y):
66...     v = vector(dX_dt([x, y]))
67...     return v/v.norm()
68...
69sage: x, y = var('x, y')
70sage: q = plot_vector_field(g(x, y), (x, -3, 3), (y, -y0, y0))
71sage: for j in xrange(len(CI)):                                       # long time
72...     X = integrate.odeint(dX_dt, CI[j], t)                         # long time
73...     q += line(X, color=(1.7*j/(4*n), 1.5*j/(4*n), 1-3*j/(8*n)))   # long time
74...                                                                   # long time
75sage: X = integrate.odeint(dX_dt, [0.01,0], t)                        # long time
76sage: q += line(X, color = 'red')                                     # long time
77sage: # q.show()
78"""