Café du jeudi: exemple2

132 days ago by thierry.chappuis

Mathématique symboliques avec Sage et un peu de Latex

Sage peut travailler de manière symbolique avec des objets mathématiques

Calcul d'une dérivée

On désire caluler la dérivée de la fonction suivante:

f(x) = \frac{x^{3} - 1}{x^{4} + 1}
 
var('x') f(x) = (x**3 - 1) / (x**4 + 1) f_prime = derivative(f, x) show(f) show(f_prime) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{x^{3} - 1}{x^{4} + 1}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ -\frac{4 \, {\left(x^{3} - 1\right)} x^{3}}{{\left(x^{4} + 1\right)}^{2}} + \frac{3 \, x^{2}}{x^{4} + 1}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{x^{3} - 1}{x^{4} + 1}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ -\frac{4 \, {\left(x^{3} - 1\right)} x^{3}}{{\left(x^{4} + 1\right)}^{2}} + \frac{3 \, x^{2}}{x^{4} + 1}

Il est également possible de générer automatiquement du code LaTeX pour coller les formules ci-dessus directement dans un rapport

latex(f) 
       
x \ {\mapsto}\ \frac{x^{3} - 1}{x^{4} + 1}
x \ {\mapsto}\ \frac{x^{3} - 1}{x^{4} + 1}

Je peux également directement interprétez du LaTeX est y inclure des expression provenant directement de Sage

%latex Voici un essai qui démontre l'interaction entre $LaTeX$ et Sage: La dérivée de \(\sage{latex(f)}\) est \(\sage{latex(f_prime)}\) 
       

Calcul d'une intégrale

Nous allons calculer l'intégrale de

\int e^{x} \cos\left(x\right)dx

f(x) = e^x * cos(x) f_int(x) = integrate(f, x) show(f) show(f_int) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ e^{x} \cos\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{2} \, {\left(\sin\left(x\right) + \cos\left(x\right)\right)} e^{x}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ e^{x} \cos\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{2} \, {\left(\sin\left(x\right) + \cos\left(x\right)\right)} e^{x}
print "f: %s\n\nf_int: %s\n" % (latex(f), latex(f_int)) 
       
f: x \ {\mapsto}\ e^{x} \cos\left(x\right)

f_int: x \ {\mapsto}\ \frac{1}{2} \, {\left(\sin\left(x\right) +
\cos\left(x\right)\right)} e^{x}
f: x \ {\mapsto}\ e^{x} \cos\left(x\right)

f_int: x \ {\mapsto}\ \frac{1}{2} \, {\left(\sin\left(x\right) + \cos\left(x\right)\right)} e^{x}

Nous avons donc:

\int e^{x} \cos\left(x\right)dx = 

\frac{1}{2} \, {\left(\sin\left(x\right) +
\cos\left(x\right)\right)} e^{x}

Pour mieux comprendre une fonction mathématique, rien de tel que de dessiner un graphe:

plot(f, (x, -2, 8)) 
       

Avec Sage, vous pouvez également calculer une intégrale définie de manière symbolique. Par exemple:

\int_0^{-1} 

\sqrt{-x^{2} + 1} dx

f(x)= sqrt(1 - x^2) f_integral = integrate(f, (x, 0, 1)) show(f_integral) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{4} \, \pi
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{4} \, \pi

Décomposer une fraction

Dans le code ci-dessous, je voudrais décomposer une fraction en une somme de fractions partielles. La fraction à décomposer est la suivante:

f(x) = \frac{3 \, x^{4} + 4 \, x^{3} + 16 \, x^{2} + 20 \, x +
9}{{\left(x + 2\right)} {\left(x^{2} + 3\right)}^{2}}
f(x) = (3 * x^4 + 4 * x^3 + 16 * x^2 + 20 * x + 9) / ((x + 2) * (x^2 + 3)^2) g(x) = f.partial_fraction(x) show(g) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{2 \, x}{x^{2} + 3} + \frac{1}{x + 2} + \frac{4 \, x}{{\left(x^{2} + 3\right)}^{2}}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{2 \, x}{x^{2} + 3} + \frac{1}{x + 2} + \frac{4 \, x}{{\left(x^{2} + 3\right)}^{2}}

Un peu d'algèbre linéaire

On désire résoudre le système d'équations linéaires suivant:

\left(\begin{array}{rrrr}
0 & -1 & -1 & 1 \\
1 & 1 & 1 & 1 \\
2 & 4 & 1 & -2 \\
3 & 1 & -2 & 2
\end{array}\right)
\cdot
\left(\begin{array}{r}
x \\
y \\
z \\
u
\end{array}\right)
=
\left(\begin{array}{r}
0 \\
6 \\
-1 \\
3
\end{array}\right)
 

A = Matrix(QQ, [[0, -1, -1, 1], [1, 1, 1, 1], [2, 4, 1, -2], [3, 1, -2, 2]]) B = vector([0, 6, -1, 3]) A.solve_right(B) 
       
(2, -1, 3, 2)
(2, -1, 3, 2)