Ticket #10983: sol_graphiques_1.py

File sol_graphiques_1.py, 3.0 KB (added by casamayou, 6 years ago)

updated file

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