ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 13 Apr 2020 15:20:14 +0200Sage returning wrong derivativehttps://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/I am trying to calculate the derivative of `y = e^(x*y)`
Hand calculation give me the result of `dy/dx = ( y*e^(x*y) ) / ( 1 - x*e^(x*y) )`
But the sage is giving me the wrong output of `-y/x`. Here is my code:
sage:
sage: y=function('y')(x)
sage: y
y(x)
sage:
sage: expr = exp(1)**(x*y)
sage:
sage: diff(y)
diff(y(x), x)
sage:
sage: diff(expr)
(x*diff(y(x), x) + y(x))*e^(x*y(x))
sage:
sage: solve(diff(expr), diff(y))
[diff(y(x), x) == -y(x)/x]
sage:
sage:
Sun, 12 Apr 2020 19:25:28 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/Answer by Juanjo for <p>I am trying to calculate the derivative of <code>y = e^(x*y)</code></p>
<p>Hand calculation give me the result of <code>dy/dx = ( y*e^(x*y) ) / ( 1 - x*e^(x*y) )</code></p>
<p>But the sage is giving me the wrong output of <code>-y/x</code>. Here is my code:</p>
<pre><code>sage:
sage: y=function('y')(x)
sage: y
y(x)
sage:
sage: expr = exp(1)**(x*y)
sage:
sage: diff(y)
diff(y(x), x)
sage:
sage: diff(expr)
(x*diff(y(x), x) + y(x))*e^(x*y(x))
sage:
sage: solve(diff(expr), diff(y))
[diff(y(x), x) == -y(x)/x]
sage:
sage:
</code></pre>
https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?answer=50705#post-id-50705You don't consider the right equation to solve:
y=function('y')(x)
dy = diff(y)
derivative = solve(diff(y==exp(x*y)), dy)
derivative[0].subs(exp(x*y)==y)
This yields
diff(y(x), x) == -y(x)^2/(x*y(x) - 1)
that is,
$$y'=\frac{y^2}{1-xy},$$
in agreement with your hand calculation.Sun, 12 Apr 2020 20:22:12 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?answer=50705#post-id-50705Comment by gg for <p>You don't consider the right equation to solve:</p>
<pre><code>y=function('y')(x)
dy = diff(y)
derivative = solve(diff(y==exp(x*y)), dy)
derivative[0].subs(exp(x*y)==y)
</code></pre>
<p>This yields</p>
<pre><code>diff(y(x), x) == -y(x)^2/(x*y(x) - 1)
</code></pre>
<p>that is,
$$y'=\frac{y^2}{1-xy},$$
in agreement with your hand calculation.</p>
https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50715#post-id-50715`exp(1)**(x*y)` and ` exp(x*y)` both yields `e^(x*y(x))`. So, I think my equation is correct. Also, why I need to call the `subs` method. I used the same procedure to find the derivative of many equations.Mon, 13 Apr 2020 07:05:48 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50715#post-id-50715Comment by Juanjo for <p>You don't consider the right equation to solve:</p>
<pre><code>y=function('y')(x)
dy = diff(y)
derivative = solve(diff(y==exp(x*y)), dy)
derivative[0].subs(exp(x*y)==y)
</code></pre>
<p>This yields</p>
<pre><code>diff(y(x), x) == -y(x)^2/(x*y(x) - 1)
</code></pre>
<p>that is,
$$y'=\frac{y^2}{1-xy},$$
in agreement with your hand calculation.</p>
https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50717#post-id-50717Concerning the `subs` method, it is just a matter of taste. Don't use it if you don't like it, but I find simpler the expression I gave for $y'$. And yes, `exp(1)**(x*y)` and `exp(x*y)` both yields `e^(x*y(x))`. That is not the point. See the next comment.Mon, 13 Apr 2020 11:48:02 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50717#post-id-50717Comment by Juanjo for <p>You don't consider the right equation to solve:</p>
<pre><code>y=function('y')(x)
dy = diff(y)
derivative = solve(diff(y==exp(x*y)), dy)
derivative[0].subs(exp(x*y)==y)
</code></pre>
<p>This yields</p>
<pre><code>diff(y(x), x) == -y(x)^2/(x*y(x) - 1)
</code></pre>
<p>that is,
$$y'=\frac{y^2}{1-xy},$$
in agreement with your hand calculation.</p>
https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50718#post-id-50718Let us get $y'$ by hand. We start with the relation $y=e^{xy}$, right? Considering $y$ as a function of $x$, we derive **in both sides** of this equation to get
$$y'=e^{xy}(y+xy')=y(y+xy')=y^2+xyy'.$$
Hence
$$y'=\frac{y^2}{1-xy},$$
as stated above. However, your Sage commands lead to get the expression of $y'$ from
$$e^{xy}(y+xy')=0,$$
which is wrong.Mon, 13 Apr 2020 11:49:53 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50718#post-id-50718Comment by gg for <p>You don't consider the right equation to solve:</p>
<pre><code>y=function('y')(x)
dy = diff(y)
derivative = solve(diff(y==exp(x*y)), dy)
derivative[0].subs(exp(x*y)==y)
</code></pre>
<p>This yields</p>
<pre><code>diff(y(x), x) == -y(x)^2/(x*y(x) - 1)
</code></pre>
<p>that is,
$$y'=\frac{y^2}{1-xy},$$
in agreement with your hand calculation.</p>
https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50721#post-id-50721Got it thanks :)Mon, 13 Apr 2020 15:20:14 +0200https://ask.sagemath.org/question/50704/sage-returning-wrong-derivative/?comment=50721#post-id-50721