{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bbed05c6",
   "metadata": {},
   "source": [
    "# Feuille d'exercices 9\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "```{admonition} Objectifs\n",
    "* Expressions symboliques\n",
    "* Fonctions : dérivation, intégration, limites\n",
    "* Sommes, produits et séries\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "## Exercice 1 : quelques limites\n",
    "\n",
    "**Question 1 :** Soit $f(x) = \\frac{x^3+x}{x-1}$.\n",
    "1. Définir l'expression symbolique associée à $f$.\n",
    "2. Évaluer cette expression en $x=1$, que se passe-t-il ?\n",
    "3. Calculer la limite de $f(x)$ en $x=1$, à gauche et à droite."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a95cee90",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "f = (x**3 + x)/(x-1)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2096f5ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb13b8f6",
   "metadata": {},
   "source": [
    "On observe une erreur d'évaluation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1ec4567",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(limit(f, x=1, dir=\"+\"))\n",
    "print(limit(f, x=1, dir=\"-\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e8db3f",
   "metadata": {},
   "source": [
    "**Question 2 :** Soit $n \\ge 1$ et $h_n$ la fonction à valeurs réelles définie par $h_n(x) := (1 + \\frac{x}{n})^n$ pour tout réel $x$. \n",
    "1. Définir l'expression symbolique représentant $h_n(x)$.\n",
    "1. Tracer sur un même graphique, entre $x=-1$ et $x=3$, les fonctions $h_n$ pour $n=1$, $n=2$ et $n= 10$ (utiliser différentes couleurs). Vers quelle fonction semble converger la suite $(h_n)_{n \\ge 0}$ ?\n",
    "1. Pour $x$ fixé, calculer la limite de $h_n(x) = (1 + \\frac{x}{n})^n$ lorsque $n \\to \\infty$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7a09279",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "var('n')\n",
    "h = (1+x/n)**n\n",
    "\n",
    "plot(h(n=1), color=\"blue\", xmin=-1, xmax=3) + plot(h(n=2), color=\"green\", xmin=-1, xmax=3) + plot(h(n=10), color=\"red\", xmin=-1, xmax=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "413aaf35",
   "metadata": {},
   "outputs": [],
   "source": [
    "h.limit(n=oo)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9442667",
   "metadata": {},
   "source": [
    "**Question 3 :** Pour $x$ fixé, calculer la série\n",
    "\n",
    "$$\n",
    "\\sum_{n=0}^{+\\infty} s_n(x) \n",
    "\\quad \\text{ où } \\quad s_n(x) = (-1)^n \\frac{x^{2n+1}}{(2n+1)!}\\,.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4291f990",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = (-1)**n * x**(2*n+1)/factorial(2*n+1)\n",
    "sum(s, n, 0, oo)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74bb987e",
   "metadata": {},
   "source": [
    "## Exercice 2 : Dérivées, primitives et intégrales\n",
    "\n",
    "\n",
    "**Question 1 :** Calculer des dérivées de :\n",
    "- $x \\mapsto \\ln(x-1)$\n",
    "- $x \\mapsto \\sin(x^2)$\n",
    "- $x \\mapsto e^{\\frac{1}{x^2}}$ (dérivée à l'ordre 2)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45c41090",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "print(diff(log(x-1), x))\n",
    "print(diff(sin(x**2), x))\n",
    "print(diff(exp(1/(x**2)), x, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "353b7f0a",
   "metadata": {},
   "source": [
    "**Question 2 :** Calculer des primitives de :\n",
    "- $x \\mapsto \\frac{1}{1+x^2}$\n",
    "- $x \\mapsto \\sin(x)^2$\n",
    "- $x \\mapsto \\frac{1}{x\\ln(x)}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72d16c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "f = 1/(1+x**2)\n",
    "print(integral(f, x))\n",
    "print(integral(sin(x)**2, x))\n",
    "print(integral(1/(x*ln(x)), x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd520dd",
   "metadata": {},
   "source": [
    "**Question 3 :** Calculer les intégrales suivantes :\n",
    "- $\\int_{1}^{2} \\frac{1}{t} dt$\n",
    "- $\\int_{-\\infty}^{+\\infty} e^{-t^2} dt$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "439059b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "var('t')\n",
    "show(integral(1/t, t, 1, 2))\n",
    "show(integral(e**(-t**2), t, -oo, +oo))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5b4c23c",
   "metadata": {},
   "source": [
    "## Exercice 3 : Un calcul de déterminant à paramètre\n",
    "\n",
    "Pour $k \\in \\mathbb{R}$, on considère la matrice \n",
    "\n",
    "$$\n",
    "M_k =\n",
    "\\begin{pmatrix}\n",
    "1 & 2 & 3 \\\\\n",
    "k & 1 & 1 \\\\\n",
    "1 & 4k & 9 \\\\\n",
    "\\end{pmatrix}\n",
    "$$\n",
    "\n",
    "\n",
    "**Question 1.** Construire la matrice $M_k$ (sur l'anneau `SR` des éléments symboliques), en prenant le soin d'avoir défini préalablement la variable `k`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53637ee1",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('k')\n",
    "Mk = matrix(SR, 3, 3, [1, 2, 3, k, 1, 1, 1, 4*k, 9])\n",
    "print(Mk)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92a218c6",
   "metadata": {},
   "source": [
    "**Question 2.** Déterminer les valeurs de $k$ pour lesquelles la matrice $M_k$ n'est pas inversible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f09735e",
   "metadata": {},
   "outputs": [],
   "source": [
    "Dk = Mk.det()\n",
    "print(Dk)\n",
    "\n",
    "solutions = Dk.solve(k)\n",
    "print(solutions)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0425d02d",
   "metadata": {},
   "source": [
    "## Exercice 4 : Des intégrales multiples\n",
    "\n",
    "\n",
    "**Question 1.** Soit $x$ une variable réelle. Calculer l'intégrale à paramètre :\n",
    "\n",
    "$$\n",
    "c(x) = \\int_{y=-\\sqrt{1-x^2}}^{\\sqrt{1-x^2}} 1 dy\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "125441cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x y')\n",
    "f = 1\n",
    "\n",
    "A = integrate(f, y, -sqrt(1-x**2), sqrt(1-x**2))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c10dc9aa",
   "metadata": {},
   "source": [
    "**Question 2.** En utilisant la fonction précédente, calculer l'intégrale double\n",
    "\n",
    "\n",
    "$$\n",
    "\\int_{x=-1}^1 \\left( \\int_{y=-\\sqrt{1-x^2}}^{\\sqrt{1-x^2}} 1 dy \\right) dx\n",
    "$$\n",
    "\n",
    "Qu'a t-on calculé ici (géométriquement) ? On pourra s'aider d'un affichage des graphes de $x \\mapsto \\pm \\sqrt{1-x^2}$ en fonction de $x$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "566eb7b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = integrate(A, x, -1, 1)\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e497095",
   "metadata": {},
   "source": [
    "On a calculé l'aire d'un cercle. En effet, les graphes des fonctions $x \\mapsto \\pm \\sqrt{1-x^2}$ sont :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35a67687",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(sqrt(1-x**2), xmin=-1, xmax=1, color=\"blue\", aspect_ratio=1) + plot(-sqrt(1-x**2), xmin=-1, xmax=1, color=\"red\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "834c50e0",
   "metadata": {},
   "source": [
    "**Question 3.** Calculer l'intégrale triple :\n",
    "\n",
    "$$\n",
    "\\int_{r=0}^R \\int_{\\theta=0}^{2\\pi} \\int_{\\phi=-\\pi/2}^{\\pi/2} r^2 cos(\\phi) dr d\\theta d\\phi\n",
    "$$\n",
    "\n",
    "Qu'a t'on calculé ici (géométriquement)  ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "911dcdb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('r theta phi R')\n",
    "f = r**2 * cos(phi)\n",
    "A = integrate(f, r, 0, R)\n",
    "B = integrate(A, theta, 0, 2*pi)\n",
    "C = integrate(B, phi, -pi/2, pi/2)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9d0a8ef",
   "metadata": {},
   "source": [
    "## Exercice 5 : D'autres limites\n",
    "\n",
    "**Question 1 :** Soit $g(x) = \\frac{\\sin(x)}{x}$.\n",
    "1. Tracer $g$ entre $-3\\pi$ et $3\\pi$. \n",
    "2. La fonction $g$ peut-elle être évaluée en $0$ ?\n",
    "3. Calculer la limite de $g$ en $0$ et en $+\\infty$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6d01109",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('x')\n",
    "g = sin(x)/x\n",
    "plot(g, [-3*pi, 3*pi])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9393ddd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "g(x=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6679ecfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(g.limit(x=0))\n",
    "print(g.limit(x=oo))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc3b5d84",
   "metadata": {},
   "source": [
    "**Question 2 :** Calculer $\\sum_{n=1}^\\infty \\frac{1}{n^k}$ pour les valeurs de $k=2, 4, 6, 8, 10$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23f41530",
   "metadata": {},
   "outputs": [],
   "source": [
    "var('n')\n",
    "L = []\n",
    "for k in range(2, 12, 2):\n",
    "    L.append(sum(1/(n**k), n, 1, oo))\n",
    "print(L)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Sagemath",
   "language": "python",
   "name": "sagemath"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
