{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](img/logocurso.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbpresent": {
     "id": "d9e822f9-0a52-43f2-90ef-e8fd6860aacc"
    }
   },
   "source": [
    "![](img/sesion8.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Al igual que en la mayoría de entornos de programación matemática, Sympy incorpora instrucciones para obtener la representación gráfica de funciones, funciones paramétricas o curvas y dominios determinados por ecuaciones implícitas, tanto en dos como en tres dimensiones. \n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "<p>Para utilizar estas instrucciones hemos de importarlas del submodulo <b>plotting</b> de sympy mediante</p>\n",
    "    <p style=\"text-align:center;\">from sympy.plotting import *</p>\n",
    "</div>\n",
    "\n",
    "Las instrucciones básicas son las siguientes.\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "<ul>\n",
    "    <li><b style=\"font-size: larger\">Gráficas en dos dimensiones:</b>\n",
    "        <ul>\n",
    "            <li><b>plot()</b>: representa funciones de una variable,</li>\n",
    "            <li><b>plot_parametric()</b>: representa curvas paramétricas del plano,</li>\n",
    "            <li><b>plot_implicit()</b>: representa curvas y dominios dados mediante ecuaciones implícitas.</li>\n",
    "        </ul>\n",
    "    </li>\n",
    "    <li><b style=\"font-size: larger\">Gráficas en tres dimensiones:</b>\n",
    "        <ul>\n",
    "            <li><b>plot3d()</b>: representa funciones de dos variables,</li>\n",
    "            <li><b>plot3d_parametric_line()</b>: representa curvas paramétricas del espacio,</li>\n",
    "            <li><b>plot3d_implicit_surface()</b>: representa superficies paramétricas.</li>\n",
    "        </ul>\n",
    "    </li>\n",
    "</ul>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# plot\n",
    "\n",
    "La instrucción plot admite los siguientes formatos:\n",
    "<div class=\"alert alert-info\">\n",
    "    <ul>\n",
    "        <li><b>plot(expr,(x,x0,xf),opciones)</b>: Representa la función dada por la expresión expr para la variable x moviéndose entre x0 y xf (si no se indica nada se toma como dominio por defecto x0=-10, xf=10)</li>\n",
    "        <li><b>plot(expr1,expr2,...,exprn,(x,x0,xf),opciones)</b>:Representa las funciones dadas por expr1, expr2,..., exprn en el dominio indicado </li>\n",
    "        <li><b>plot((expr1,dominio1),...,(exprn,dominion),opciones)</b>:Representa las funciones dadas por expr1, expr2,..., exprn cada una en el dominio indicado</li>\n",
    "    </ul>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "from sympy.plotting import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Representamos la función $f(x)=x^2$ para $x\\in[-10,10]$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=symbols(\"x\")\n",
    "plot(x**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Representamos las funciones $f_1(x)=x^2$ y $f_2(x)=20\\cos(x)$ en $x\\in[-5,5]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(x**2,20*cos(x),(x,-5,5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Representamos las funciones $f_1(x)=x^2$ en $x\\in[-3,3]$ y $f_2(x)=20\\cos(x)$ en $x\\in[-5,5]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot((x**2,(x,-3,3)),(20*cos(x),(x,-5,5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-info\">\n",
    "Algunas de las opciones que podemos utilizar son:\n",
    "    <ul>\n",
    "        <li><b>title</b>: A nombre o título para la gráfica</li>\n",
    "        <li><b>line_color</b>: Color de la curva del gráfico</li>\n",
    "        <li><b>xlabel</b>: Etiqueta para el eje horizontal</li>\n",
    "        <li><b>ylabel</b>: Etiqueta para el vertical </li>\n",
    "        <li><b>size</b>: (tamaño horizontal, tamaño vertical) en pulgadas</li>\n",
    "        <li><b>xlim</b>: límites (xi,xf) para el gráfico en el eje horizontal</li>\n",
    "        <li><b>ylim</b>: límites (yi,yf) para el gráfico en el eje vertical</li>\n",
    "        <li><b>axis_center</b>: posicón del centro de ejes</li>\n",
    "        <li><b>aspect_ratio</b>: auto o un par (escx,escy) que indica qué escala se usará para los ejes horizontal y vertical</li>\n",
    "        <li><b>show</b>: True/False permite establecer si la gráfica se mostrará en el momento de generarla o no. Después, mediante el método <b>show()</b> podemos forzar la representación de la gráfica</li>\n",
    "    </ul>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(cos(x**2),(x,-5,5),title=\"Gráfica de $f(x)=cos(x^2)$\",\n",
    "     line_color=\"green\",xlabel=\"x\",ylabel=\"f(x)\",size=(5,3),ylim=(0,1),\n",
    "    axis_center=(2,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Mezcla de gráficos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generamos dos gráficos pero establecemos show=false para que no\n",
    "# se muestren\n",
    "grafico1 = plot(x*x, show=False)\n",
    "grafico2 = plot(x, show=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Añadimos el contenido de grafico2 (objeto de la clase Series) \n",
    "# a grafico1\n",
    "grafico1.append(grafico2[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Si imprimimos grafico1 obtenemos una descripción de las gráficas\n",
    "print(grafico1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mediante el método show() forzamos la representación de las gráficas\n",
    "# contenidas en grafico1\n",
    "grafico1.title=\"Gráfica conjunta\"\n",
    "grafico1.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# plot_parametric"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Representamos la curva $r(t)=(1+\\cos(4t))\\cdot(\\cos(t),\\sin(t))$ para $t\\in[0,2\\pi]$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t=symbols(\"t\")\n",
    "plot_parametric(((1+cos(4*t))*cos(t),(1+cos(4*t))*sin(t)),(t,0,2*pi),\n",
    "                aspect_ratio=(1,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_parametric(\n",
    "    ((1+cos(4*t))*cos(t),(1+cos(4*t))*sin(t)),\n",
    "    (2*cos(t),2*sin(t)),\n",
    "    (t,0,2*pi),\n",
    "    aspect_ratio=(1,1)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_parametric(\n",
    "    ((1+cos(4*t))*cos(t),(1+cos(4*t))*sin(t),(t,0,2*pi)),\n",
    "    (cos(3*t),t,(t,-pi/2,pi/2))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# plot_implicit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consideramos la curva, dada en ecuaciones implícitas, $C\\equiv x^4+y^4=1$. Para utilizar plot_implicit necesitamos indicar el area en que debe ser representada (de lo contrario sympy realizará la elección por su cuenta."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y=symbols(\"y\")\n",
    "curva1=plot_implicit(Eq(x**4+y**4,1),(x,-1,1),(y,-1,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Podemos utilizar tambien extend para cambinar dos gráficos."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curva2=plot_implicit(Eq(x**2+y**2,1),(x,-1,1),(y,-1,1),show=False)\n",
    "curva1.extend(curva2)\n",
    "curva1.aspect_ratio=(1,1)\n",
    "curva1.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Representemos ahora el disco $D\\equiv x^2+y^2\\leq 1$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curva1=plot_implicit(x**2+y**2<=1,(x,-1,1),(y,-1,1),aspect_ratio=(1,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# plot3d, plot3d_parametric_line/surface\n",
    "\n",
    "Representamos la función de dos variables $\\begin{array}{c}f:[-1,1]\\times[-1,1]\\rightarrow \\mathbb{R}\\\\f(x,y)=x^2+y^2\\end{array}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot3d(x**2+y**2,(x,-1,1),(y,-1,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Podemos combinar la representación de dos funciones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot3d(\n",
    "    (sqrt(1-x**2-y**2),(x,-1,1),(y,-1,1)),\n",
    "    (x+y,(x,-1,1),(y,-1,1)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tomemos ahora la curva del espacio $r(t)=(\\cos(t),\\sin(t),t)$ para $t\\in[0,2\\pi]$ (hélice circular)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot3d_parametric_line(cos(t),sin(t),t,(t,0,4*pi))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "El toro es una superficie del espacio dada por la parametrización \n",
    "$$\n",
    "    \\begin{array}{c}\n",
    "        X:[0,2\\pi]\\times[0,2\\pi]\\rightarrow \\mathbb{R}\\\\\n",
    "        X(u,v)=((2+\\cos(u))\\cos(v),(2+\\cos(u))\\sin(v),\\sin(u))\n",
    "    \\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "u,v=symbols(\"u v\")\n",
    "plot3d_parametric_surface(\n",
    "    (2+cos(u))*cos(v),(2+cos(u))*sin(v),sin(u),\n",
    "    (u,0,2*pi),(v,0,2*pi))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ejercicio 1\n",
    "Representa la cicloide dada por las ecuaciones paramétricas\n",
    "$$\n",
    "    \\begin{array}{c}\n",
    "        c:[0,4\\pi]\\rightarrow\\mathbb{R}^2\\\\\n",
    "        c(t)=(t-\\sin(t),1-\\cos(t))\n",
    "     \\end{array}\n",
    "$$\n",
    "Para $t=1,3,5,7,9,11$, representa la circunferencia de radio 1 centrada en el punto (t,1) así como la línea que une el centro de esa circunferencia con el punto c(t) correspondiente de la cicloide.\n",
    "\n",
    "Muestra, combinadas en una misma imagen, todas estas gráficas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t,s=symbols(\"t,s\")\n",
    "cicloide=plot_parametric((t-sin(t),1-cos(t)),(t,0,4*pi),\n",
    "                aspect_ratio=(1,1),show=False)\n",
    "\n",
    "def circulo(t):\n",
    "    return plot_parametric((t+cos(s),1+sin(s)),(s,0,2*pi),aspect_ratio=(1,1),show=False)\n",
    "\n",
    "def linea(t):\n",
    "    return plot_parametric((t-s*sin(t),1-s*cos(t)),(s,0,1),aspect_ratio=(1,1),show=False)\n",
    "\n",
    "for t in range(1,12,2):\n",
    "    cicloide.extend(circulo(t))\n",
    "    cicloide.extend(linea(t))\n",
    "\n",
    "cicloide.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ejercicio 2\n",
    "Representa la función $\\begin{array}{c} f:[-5,5]\\times[-5,5]\\rightarrow \\mathbb{R}\\\\f(x,y)=\\cos(x)\\cos(y)e^{-\\frac{x^2+y^2}{100}}\\end{array}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
