In this lab, we will learn how to use SageMath to find derivatives of functions defined implicity.
Sections 3.7
Consider the Folium of Descartes $x^3 + y^3 = 3xy$. In this example, we will do the following:
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$.
y = function('y')(x)
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$.
diff(x^3 + y^3 == 3*x*y,x)
Recall that we can make SageMath display the output in a nicer and more readable way by using the $\textbf{show}(\dots)$ command.
show(diff(x^3 + y^3 == 3*x*y,x))
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.
show(solve(diff(x^3 + y^3 == 3*x*y,x), diff(y,x)))
Therefore, we see that $\dfrac{dy}{dx} = -\dfrac{x^2 - y}{y^2 - x}$.
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.
y = var('y')
def dydx(x,y):
return -(x^2 - y) / (y^2 - x)
dydx(3/2, 3/2)
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.$$
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.
p1 = implicit_plot(x^3 + y^3 == 3*x*y, (-5,5), (-5,5), color = "black")
p2 = point((3/2, 3/2), color="red", size = 50)
p3 = plot(-x+3, xmin=-5, xmax=5, color="orange", linestyle = "--")
show(p1 + p2 + p3)
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.$
solve([x^3 + y^3 == 3*x*y, dydx(x,y) == 0], x, y)
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)$.
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$.
solve([x^3 + y^3 == 3*x*y, 1/dydx(x,y) == 0], x, y)
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)$.
Repeat Example 1 with the function $2(x^2 + y^2)^2 = 25(x^2 - y^2)$ and the point $(3,1)$.
Repeat Example 1 with the function $x^2y - xy^2 = 2$ and the point $(-1,1)$.
Repeat Example 1 with the function $\left(x^{2}+y^{2}-1\right)^{3}-x^{2}y^{3}=0$ and the point (1,1).