\n",
" \n",
"## Overview\n",
"\n",
"In this lab, we will learn how to use SageMath to find derivatives of functions defined implicity. \n",
"\n",
"### Important SageMath Commands Introduced in this Lab\n",
"\n",
"\\begin{array}{|l|l|l|}\n",
"\\hline \n",
"\\hfill \\textbf{Command} \\hfill & \\hfill \\textbf{Description} \\hfill & \\hfill \\textbf{Example} \\hfill \\\\\n",
"\\hline\n",
"\\textbf{function('y')(x)} & \\text{Creates the function $y(x)$ without defining it} & y = \\textbf{function('y')(x)} \\\\\n",
"\\hline\n",
"\\textbf{implicit_plot}(\\textit{eqn}, (xmin, xmax), (ymin, ymax)) & \\text{Plots an implicitly defined function} & \\textbf{implicit_plot}(x^2 + y^2 == 1, (-2,2), (-2,2)) \\\\\n",
"\\hline\n",
"\\textbf{point}((x,y), \\dots) & \\text{Plots the point $(x,y)$} & \\textbf{point}((1,1), \\text{color = \"red\", size = 50}) \\\\\n",
"\\hline\n",
"\\end{array}\n",
"\n",
"### Related Course Material\n",
" Sections 3.7"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 1\n",
"\n",
"Consider the Folium of Descartes $x^3 + y^3 = 3xy$. In this example, we will do the following:\n",
"1. Use implicit differentiation to find $\\dfrac{dy}{dx}$\n",
"2. Find the equation of the tangent line to the Folium of Descartes at the point $\\left(\\frac{3}{2}, \\frac{3}{2}\\right)$\n",
"3. Graph the curve, the point, and the tangent line \n",
"4. Find all points on the Folium of Descartes which have a horizontal tangent line or a vertical tangent line\n",
"\n",
"First, will use SageMath to find $\\dfrac{dy}{dx}$ using implicit differentiation. In order for SageMath to calculate $\\dfrac{dy}{dx}$, we need to use the $\\textbf{function('y')(x)}$ command to let SageMath know that $y$ is a function which depends on $x$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = function('y')(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Next, we will take the derivate with respect to $x$ of both sides of our equation. Since we made $y$ a funciton of $x$, SageMath will automatically use the chain rule when it takes the derivative of $y$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"diff(x^3 + y^3 == 3*x*y,x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Recall that we can make SageMath display the output in a nicer and more readable way by using the $\\textbf{show}(\\dots)$ command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"show(diff(x^3 + y^3 == 3*x*y,x))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"The next thing we need to do is solve this equation for $\\dfrac{dy}{dx}$ which we can have SageMath do for us using the $\\textbf{solve}(\\textit{expression}, \\textit{variable})$ command from Lab 4."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"show(solve(diff(x^3 + y^3 == 3*x*y,x), diff(y,x)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Therefore, we see that $\\dfrac{dy}{dx} = -\\dfrac{x^2 - y}{y^2 - x}$. \n",
"\n",
"Now, we will find the equation of the tangent line of the Folium of Descartes at the point $\\left(\\frac{3}{2}, \\frac{3}{2}\\right)$. (Note: This is a point on the graph of the Folium of Descartes since if we replace both $x$ and $y$ with $\\frac{3}{2}$ in the equation, we get a true statement.) We already know the $x$-value and $y$-value of the point, so the only thing we have left to find is $\\dfrac{dy}{dx}\\biggr \\rvert_{\\left(\\frac{3}{2},\\frac{3}{2}\\right)}.$ We can do this in SageMath by first creating the function $\\dfrac{dy}{dx}$ which, in this example, depends on both $x$ and $y$. Note that when creating this function, we will need to use $y$ as a variable, however, earlier we made $y$ a function of $x$, so we have to overwrite that by using the $\\textbf{var}(`y`)$ command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"y = var('y')\n",
"def dydx(x,y):\n",
" return -(x^2 - y) / (y^2 - x)\n",
"dydx(3/2, 3/2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Thus, we see that $\\dfrac{dy}{dx} \\biggr \\rvert_{\\left(\\frac{3}{2}, \\frac{3}{2}\\right)} = -1.$ It follows that the equation of the tangent line of the Folium of Descartes at the point $\\left(\\frac{3}{2}, \\frac{3}{2}\\right)$ is $$y = -1\\left(x - \\frac{3}{2}\\right) + \\frac{3}{2} \\implies y = -x + 3.$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Now, let us plot the Folium of Descartes, the point $\\left(\\frac{3}{2},\\frac{3}{2}\\right)$, and the tangent line at $\\left(\\frac{3}{2}, \\frac{3}{2}\\right)$ all on the same graph. In order to plot them all on the same graph, we will assign each of these three objects to a variable and then use the $\\textbf{show}(\\dots)$ command. To plot the Folium of Descartes, we have to make use of the $\\textbf{implicit_plot}(\\textit{eqn}, (xmin,xmax), (ymin, ymax))$ command since it is defined implicitly. To graph the point, we need to make use of the $\\textbf{point}((x,y))$ command. Both of these commands can use some of the options we use in plot, such as color."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p1 = implicit_plot(x^3 + y^3 == 3*x*y, (-5,5), (-5,5), color = \"black\")\n",
"p2 = point((3/2, 3/2), color=\"red\", size = 50)\n",
"p3 = plot(-x+3, xmin=-5, xmax=5, color=\"orange\", linestyle = \"--\")\n",
"show(p1 + p2 + p3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Lastly, we will find all of the points on the Folium of Descartes which have either a horizontal or vertical tangent line. Recall that horizontal tangent lines occur at points where $\\dfrac{dy}{dx} = 0$. We will use the $\\textbf{solve}([\\textit{equations}], \\textit{variables})$ command to determine what points both lie on the Folium of Descartes and satisfy $\\dfrac{dy}{dx} = 0.$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"solve([x^3 + y^3 == 3*x*y, dydx(x,y) == 0], x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Note that some of the solutions SageMath gave are imaginary; that is, they involve the imaginary constant $I = \\sqrt{-1}$, so we will disregard these. Therefore, the only two points which are on the Folium of Descartes and have a horizontal tangent line are $(0,0)$ and rougly $(1.2599, 1.5874)$. \n",
"\n",
"Vertical tangent lines occur at points where $\\dfrac{dx}{dy} = 0$. Note that $\\dfrac{dx}{dy}$ is simply the reciprocal of $\\dfrac{dy}{dx}$. Again, we use the $\\textbf{solve}(\\dots)$ command to determine which points lie on the Folium of Descartes and make $\\dfrac{dx}{dy} = 0$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"solve([x^3 + y^3 == 3*x*y, 1/dydx(x,y) == 0], x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Again, SageMath gives some imaginary solutions which we will disregard. It follows that the only two points which are on the Folium of Descartes and have a vertical tangent line are $(0,0)$ and rougly $(1.5874, 1.2599)$."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 2\n",
"\n",
"Repeat Example 1 with the function $2(x^2 + y^2)^2 = 25(x^2 - y^2)$ and the point $(3,1)$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 3 \n",
"\n",
"Repeat Example 1 with the function $x^2y - xy^2 = 2$ and the point $(-1,1)$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 4\n",
"\n",
"Repeat Example 1 with the function $\\left(x^{2}+y^{2}-1\\right)^{3}-x^{2}y^{3}=0$ and the point (1,1)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.2",
"language": "sage",
"name": "sagemath"
},
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}