#!/usr/bin/env python # coding: utf-8 # 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 # In[3]: from sympy import Rational Rational(5,2) # In[4]: from sympy import S get_ipython().run_line_magic('pinfo', 'S') # In[8]: type(S(5)) # In[9]: S(5)/2 # In[14]: S('13/2') + S(5)/7 # ### 3.2 Nombres complexes # In[15]: from sympy import arg,re,im,I # In[16]: a = 3 + 5*I # In[18]: re(a) # In[19]: im(a) # In[22]: arg(a).n() # In[23]: abs(a) # ### 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 # # 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) # In[40]: solve(eq2, y) # Résoudre l'équation $x^2+2x=4$ # In[44]: solve(Eq(x**2+2*x, 4)) # 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) # 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) # 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]) # ### 5.5 Trouver les racines d'une fonction # $$3x^2 + 52x - 265$$ # In[50]: roots(3*x**2+52*x-265) # $$3x^3 + 52x - 265$$ # In[52]: r1,r2,r3 = roots(3*x**3+52*x-265) # In[54]: r1.n(), r2.n(), r3.n() # ## 6 Tracer une fonction # In[55]: get_ipython().run_line_magic('matplotlib', 'inline') # ### 6.1 Tracer une fonction R -> R # $$f(x) = x^2$$ # In[71]: f = x**2 plot(x**2, (x,-20,20)) # $$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)) # 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)) # In[70]: roots(x**3+2*x**2-1) # ### 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) # ### 6.3 Tracer une fonction R^2 -> R # In[73]: from sympy.plotting import plot3d # $$x^2+y^2$$ # In[74]: j # $$x^2-y^2$$ # In[75]: plot3d(x**2-y**2) # $$(x^2-y^2)\sin(x)$$ # In[76]: plot3d((x**2-y**2)*sin(x)) # ### 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)) # 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) # 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) # 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) # \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) # ### 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)) # $$\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='+') # In[103]: oo # In[104]: limit(1/x, x, oo) # ### 7.2 Sommes et séries # In[ ]: # In[ ]: # ### 7.3 Produit # In[ ]: # In[ ]: # ### 7.4 Développement en séries # In[ ]: # In[ ]: # In[ ]: