In [1]:
from __future__ import division

Chapitres 1 à 4

Quelques ajouts dans les sections déjà vues...

3.1 Nombres rationels

Une autre façon de créer un nombre rationnel


In [2]:
5 / 2


Out[2]:
2.5

In [3]:
from sympy import Rational
Rational(5,2)


Out[3]:
5/2

In [4]:
from sympy import S
S?

In [8]:
type(S(5))


Out[8]:
sympy.core.numbers.Integer

In [9]:
S(5)/2


Out[9]:
5/2

In [14]:
S('13/2') + S(5)/7


Out[14]:
101/14

3.2 Nombres complexes


In [15]:
from sympy import arg,re,im,I

In [16]:
a = 3 + 5*I

In [18]:
re(a)


Out[18]:
3

In [19]:
im(a)


Out[19]:
5

In [22]:
arg(a).n()


Out[22]:
1.03037682652431

In [23]:
abs(a)


Out[23]:
sqrt(34)

4.2 Définir les variables symboliques x_1, x_2, ... x_n


In [24]:
from sympy import symbols

In [28]:
x3,x4,x5,x6,x7 = symbols('x3:8')

In [29]:
x3 + x4+ 6 *x7


Out[29]:
x3 + x4 + 6*x7

Initialisation


In [ ]:

Importer toutes les fonctions de Sympy:


In [30]:
from sympy import *
init_printing(pretty_print=True, use_latex='mathjax')

Importer quelques variables du module abc:


In [31]:
#from sympy.abc import *   # à éviter
from sympy.abc import a,b,c,n,t,u,v,w,x,y,z

5 Résolution d'équations

5.1 Définir une équation

Définir l'équation $x=3$


In [35]:
eq1 = Eq(x, 3)

Définir l'équation $x^2+y^2=16$


In [37]:
eq2 = Eq(x**2+y**2, 16)

5.2 Résoudre une équation


In [43]:
solve(eq1, x)


Out[43]:
$$\left [ 3\right ]$$

In [40]:
solve(eq2, y)


Out[40]:
$$\left [ - \sqrt{- x^{2} + 16}, \quad \sqrt{- x^{2} + 16}\right ]$$

Résoudre l'équation $x^2+2x=4$


In [44]:
solve(Eq(x**2+2*x, 4))


Out[44]:
$$\left [ -1 + \sqrt{5}, \quad - \sqrt{5} - 1\right ]$$

In [ ]:

5.3 Résoudre un système d'équations

Résoudre le système $x+y=34$, $xy=34$


In [47]:
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y, 34)
systeme = [eq1, eq2]
solve(systeme)


Out[47]:
$$\left [ \left \{ x : - \sqrt{255} + 17, \quad y : \sqrt{255} + 17\right \}, \quad \left \{ x : \sqrt{255} + 17, \quad y : - \sqrt{255} + 17\right \}\right ]$$

Résoudre le système $x+y=34$, $xy^2=34$


In [48]:
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y**2, 34)
systeme = [eq1, eq2]
solve(systeme)


Out[48]:
$$\left [ \left \{ x : \frac{17^{\frac{2}{3}}}{\left(1 - \sqrt{3} i\right) \sqrt[3]{2285 + 3 \sqrt{13791} i}} \left(\frac{136}{3} + \frac{1}{102} \left(1 - \sqrt{3} i\right) \left(136 + \left(1 - \sqrt{3} i\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right), \quad y : \frac{17^{\frac{2}{3}}}{\left(1 - \sqrt{3} i\right) \sqrt[3]{2285 + 3 \sqrt{13791} i}} \left(- \frac{136}{3} + \frac{1}{102} \left(1 - \sqrt{3} i\right) \left(68 + \left(-1 + \sqrt{3} i\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right)\right \}, \quad \left \{ x : \frac{17^{\frac{2}{3}}}{\left(1 + \sqrt{3} i\right) \sqrt[3]{2285 + 3 \sqrt{13791} i}} \left(\frac{136}{3} + \frac{1}{102} \left(1 + \sqrt{3} i\right) \left(136 + \left(1 + \sqrt{3} i\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right), \quad y : \frac{17^{\frac{2}{3}}}{\left(1 + \sqrt{3} i\right) \sqrt[3]{2285 + 3 \sqrt{13791} i}} \left(- \frac{136}{3} + \frac{1}{102} \left(1 + \sqrt{3} i\right) \left(68 + \left(-1 - \sqrt{3} i\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right) \sqrt[3]{38845 + 51 \sqrt{13791} i}\right)\right \}, \quad \left \{ x : \frac{68}{3} - \frac{\sqrt[3]{17}}{3} \sqrt[3]{2285 + 3 \sqrt{13791} i} - \frac{68 \cdot 17^{\frac{2}{3}}}{3 \sqrt[3]{2285 + 3 \sqrt{13791} i}}, \quad y : \frac{34}{3} + \frac{68 \cdot 17^{\frac{2}{3}}}{3 \sqrt[3]{2285 + 3 \sqrt{13791} i}} + \frac{\sqrt[3]{17}}{3} \sqrt[3]{2285 + 3 \sqrt{13791} i}\right \}\right ]$$

In [ ]:

5.4 Syntaxe abrégée

Résoudre les systèmes ci-haut avec la syntaxe abrégée


In [49]:
solve([x+y-34, x*y-34])


Out[49]:
$$\left [ \left \{ x : - \sqrt{255} + 17, \quad y : \sqrt{255} + 17\right \}, \quad \left \{ x : \sqrt{255} + 17, \quad y : - \sqrt{255} + 17\right \}\right ]$$

5.5 Trouver les racines d'une fonction

$$3x^2 + 52x - 265$$

In [50]:
roots(3*x**2+52*x-265)


Out[50]:
$$\left \{ - \frac{26}{3} + \frac{\sqrt{1471}}{3} : 1, \quad - \frac{\sqrt{1471}}{3} - \frac{26}{3} : 1\right \}$$
$$3x^3 + 52x - 265$$

In [52]:
r1,r2,r3 = roots(3*x**3+52*x-265)

In [54]:
r1.n(), r2.n(), r3.n()


Out[54]:
$$\left ( 3.20205243239924, \quad -1.60102621619962 - 5.00231827937895 i, \quad -1.60102621619962 + 5.00231827937895 i\right )$$

6 Tracer une fonction


In [55]:
%matplotlib inline

6.1 Tracer une fonction R -> R

$$f(x) = x^2$$

In [71]:
f = x**2
plot(x**2, (x,-20,20))


Out[71]:
<sympy.plotting.plot.Plot at 0x1140a8090>
$$g(x) = {3x^2 + 52x - 265 \over (x - 7)(x - 1)(x + 34)}$$

In [63]:
g = (3*x**2+52*x-265) / ((x-7)*(x-1)*(x+34))
plot(g, (x,-40,10), xlim=(-40, 10), ylim=(-2,2))


Out[63]:
<sympy.plotting.plot.Plot at 0x11393ee90>

Approximer les racines de $$x^3 + 2x^2 - 1$$ avec un dessin


In [69]:
plot(x**3+2*x**2-1, (x,-2,1), ylim=(-1,1))


Out[69]:
<sympy.plotting.plot.Plot at 0x113d2e110>

In [70]:
roots(x**3+2*x**2-1)


Out[70]:
$$\left \{ -1 : 1, \quad - \frac{1}{2} + \frac{\sqrt{5}}{2} : 1, \quad - \frac{\sqrt{5}}{2} - \frac{1}{2} : 1\right \}$$

6.2 Tracer plusieurs fonctions R -> R

Tracer $f(x)$ en vert et $g(x)$ en jaune dans le même dessin


In [72]:
plot(f, g)


Out[72]:
<sympy.plotting.plot.Plot at 0x1142bbe50>

6.3 Tracer une fonction R^2 -> R


In [73]:
from sympy.plotting import plot3d
$$x^2+y^2$$

In [74]:
j


Out[74]:
<sympy.plotting.plot.Plot at 0x11435b990>
$$x^2-y^2$$

In [75]:
plot3d(x**2-y**2)


Out[75]:
<sympy.plotting.plot.Plot at 0x114497d50>
$$(x^2-y^2)\sin(x)$$

In [76]:
plot3d((x**2-y**2)*sin(x))


Out[76]:
<sympy.plotting.plot.Plot at 0x114599f90>

6.4 Dessiner une fonction R -> R^2


In [77]:
from sympy.plotting import plot_parametric
$$x = \sin(t)$$$$y = \cos(t)$$

In [78]:
plot_parametric(sin(t), cos(t))


Out[78]:
<sympy.plotting.plot.Plot at 0x114a65810>

Butterfly curve (transcendental)

$$x = \sin(t) \left(e^{\cos(t)} - 2\cos(4t) - \sin^5\left({t \over 12}\right)\right)$$$$y = \cos(t) \left(e^{\cos(t)} - 2\cos(4t) - \sin^5\left({t \over 12}\right)\right)$$

Voir: https://en.wikipedia.org/wiki/Butterfly_curve_%28transcendental%29


In [80]:
x = sin(t)*(E**cos(t)-2*cos(4*t)-sin(t/12)**5)
y = cos(t)*(E**cos(t)-2*cos(4*t)-sin(t/12)**5)
plot_parametric(x, y)


Out[80]:
<sympy.plotting.plot.Plot at 0x114b6c610>

In [81]:
from IPython.display import Image
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/Param_02.jpg/800px-Param_02.jpg'
Image(url=url, width=500)


Out[81]:

In [ ]:

6.5 Dessiner une fonction R -> R^3


In [82]:
from sympy.plotting import plot3d_parametric_line
\begin{align} x(u)&= \cos(u)\\ y(u)&=\sin(u)\\ z(u)&=u\end{align}

In [83]:
x = cos(u)
y = sin(u)
z = u
plot3d_parametric_line(x,y,z)


Out[83]:
<sympy.plotting.plot.Plot at 0x11202a850>
\begin{align} x(u)&= u \cos(u)\\ y(u)&=\sin(u)\\ z(u)&=u\end{align}

In [85]:
x = u*cos(u)
y = u*sin(u)
z = u
plot3d_parametric_line(x,y,z)


Out[85]:
<sympy.plotting.plot.Plot at 0x1155a5f10>

6.6 Dessiner une fonction R^2 -> R^3


In [86]:
from sympy.plotting import plot3d_parametric_surface
\begin{align} X &= \cos(u)(R+r\cos(v))\\ Y &= \sin(u)(R+r\cos(v))\\ Z &= r\sin(v) \end{align}

In [87]:
from sympy.abc import u,v
R = 5
r = 2
X = cos(u)*(R+r*cos(v))
Y = sin(u)*(R+r*cos(v))
Z = r*sin(v)
plot3d_parametric_surface(X, Y, Z, (u, -.5, 4), (v, -5, 5))


Out[87]:
<sympy.plotting.plot.Plot at 0x115568a90>
$$\vec r(\theta,\phi) = (\cos\theta \sin\phi, \sin\theta \sin \phi, \cos\phi), \quad 0 \leq \theta < 2\pi, 0 \leq \phi \leq \pi.$$

In [ ]:
from sympy.abc import theta,phi
from sympy import pi,cos,sin
x = cos(theta)*sin(phi)
y = sin(theta)*sin(phi)
z = cos(theta)
plot3d_parametric_surface(x,y,z,(theta,0,pi), (phi,0,pi))

6.7 Dessiner les solutions d'une équation implicite

Tracer les solutions de l'équation $$x^2+y^2+xy-2x =5$$


In [ ]:
from sympy import plot_implicit
eq = Eq(x**2+y**2+x*y-2*x, 5)
plot_implicit(eq)

6.8 Tracer une région de R^2

Tracer la région délimitée par $$y>2x+1, y<5x, x+y<5$$


In [ ]:

6.9 Dessiner une fonction complexe avec mpmath


In [90]:
#from sympy import mpmath    # Sympy (installation normale)
import mpmath               # SageMath

Tracer la fonction complexe identité $$f(z)=z$$


In [92]:
from mpmath import cplot

In [93]:
cplot(lambda z:z)


Tracer la fonction complexe $$f(z)=z^5-1$$


In [94]:
cplot(lambda z:z**5-1)


7 Limites et séries

7.1 Limites


In [97]:
from sympy.abc import x

In [101]:
limit(1/x, x, 0, dir='+')


Out[101]:
$$\infty$$

In [103]:
oo


Out[103]:
$$\infty$$

In [104]:
limit(1/x, x, oo)


Out[104]:
$$0$$

7.2 Sommes et séries


In [ ]:


In [ ]:

7.3 Produit


In [ ]:


In [ ]:

7.4 Développement en séries


In [ ]:


In [ ]:


In [ ]: