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.Thu, 03 Nov 2016 14:19:16 +0100Numerical real solution of derivativehttps://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/I would like to know where a function attains its maximum, so I'm trying to solve some
>diff(y,x),x
where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...
Here is a more concrete example:
>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2
>find_local_maximum(diff(f,x), 0, 1)
What is mysterious for me, is that the above works if instead I have
>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2Thu, 03 Nov 2016 06:29:02 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/Comment by etb for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35378#post-id-35378It would make it easier to understand your problem, and to provide an answer, if you could provide a reproducible example, i.e. enough code to reproduce the equations, complex roots, and converting equations you mention. This would also increase the value of both question and answer for others who might face similar a challenge in the future.Thu, 03 Nov 2016 09:22:40 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35378#post-id-35378Comment by domotorp for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35380#post-id-35380I have added an example.Thu, 03 Nov 2016 09:52:09 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35380#post-id-35380Comment by Masacroso for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35384#post-id-35384Check [this](https://sagecell.sagemath.org/?z=eJxL06jQVLBVyMlP1zDSMzDSqlDQVjDUVNBXMAIyQKK6yGK8XGmZeSnxOfnJiTnxuYkVmbmluRopmWlpGmk6FZo6CgY6QIW8XACSmhXx&lang=sage). It seems to work fine. If you need precision it is possible you need to use octave or matlab instead of sage.Thu, 03 Nov 2016 10:45:38 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35384#post-id-35384Comment by domotorp for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35386#post-id-35386The same issue persists - changing the variable x to 2x shouldn't make a difference, but it does. This might not even be an issue with precision, very weird.Thu, 03 Nov 2016 10:56:03 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35386#post-id-35386Answer by etb for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?answer=35381#post-id-35381Would this give you what you are looking for? (assuming your interval is [-1;2]
f(y) = x^4-3*x+8
f.show()
find_local_maximum(f, -1, 2)
I am generally big on plotting unction like this, you can try to get a feel of your function
plot(f(y), -2,3)
![image description](/upfiles/1478163825185420.png)
I highly recommend the [SageMath Calculus Tutorial](https://www.sagemath.org/calctut/index.html) if you at all have the time.
### Update,
If I run your updated model with your coefficients, e.i.,
f = (1/4)*log(2.02000000000000*x + 1) + (1/2)*log(0.00999999999999979*x + 1) + (1/4)*log(-2*x + 1)
find_local_maximum(diff(f,x), 0, 1)
i get,
(95160969.23167175, 0.50000000262712752)
Could you explain what you expect and why? Did i possibly misinterpret your code? (I did added some parenthesis and some multiplication symbols).Thu, 03 Nov 2016 10:03:52 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?answer=35381#post-id-35381Comment by domotorp for <p>Would this give you what you are looking for? (assuming your interval is [-1;2]</p>
<pre><code>f(y) = x^4-3*x+8
f.show()
find_local_maximum(f, -1, 2)
</code></pre>
<p>I am generally big on plotting unction like this, you can try to get a feel of your function</p>
<pre><code>plot(f(y), -2,3)
</code></pre>
<p><img alt="image description" src="/upfiles/1478163825185420.png"></p>
<p>I highly recommend the <a href="https://www.sagemath.org/calctut/index.html">SageMath Calculus Tutorial</a> if you at all have the time.</p>
<h3>Update,</h3>
<p>If I run your updated model with your coefficients, e.i.,</p>
<pre><code>f = (1/4)*log(2.02000000000000*x + 1) + (1/2)*log(0.00999999999999979*x + 1) + (1/4)*log(-2*x + 1)
find_local_maximum(diff(f,x), 0, 1)
</code></pre>
<p>i get,</p>
<pre><code>(95160969.23167175, 0.50000000262712752)
</code></pre>
<p>Could you explain what you expect and why? Did i possibly misinterpret your code? (I did added some parenthesis and some multiplication symbols).</p>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35385#post-id-35385Thanks for your help again, I've again updated my answer. It seems that 2*x and x makes all the difference for Sage. The solution should be some x around 0.01.Thu, 03 Nov 2016 10:45:59 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35385#post-id-35385Answer by Masacroso for <p>I would like to know where a function attains its maximum, so I'm trying to solve some</p>
<blockquote>
<p>diff(y,x),x</p>
</blockquote>
<p>where y depends on y. I have difficulties with Sage returning equations, complex roots, converting equations, find_maximum_on_interval command etc, and instead of spending another hour trying to figure it out myself, I thought I would try asking here...</p>
<p>Here is a more concrete example:</p>
<blockquote>
<p>f = log(2.02 * x + 1) / 2 + log( -2 * x + 1) / 2</p>
<p>find_local_maximum(diff(f,x), 0, 1)</p>
</blockquote>
<p>What is mysterious for me, is that the above works if instead I have</p>
<blockquote>
<p>f = log(1.01 * x + 1) / 2 + log( -x + 1) / 2</p>
</blockquote>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?answer=35391#post-id-35391Ok, I see what is happening. The derivative of your function (the example that you put) is not bounded on the interval $(0,1)$, i.e. it is not defined in the points $-1/2$ and $1/2$ where it is going to infinity.
But sage works fine, it said that the maximum is in $1/2$ because it is an asymptote. In other words
$$\lim_{x\to\frac12^-}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=+\infty$$
and
$$\lim_{x\to\frac12^+}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=-\infty$$
Check this:
http://www.wolframalpha.com/input/?i=limit+diff(log(2.02+*+x+%2B+1)+%2F+2+%2B+log(+-2+*+x+%2B+1)+%2F+2,x)+when+x-%3E1%2F2
So, Sage is not doing wrong... it is doing the "correct" answer. Well, the correct answer will be to say that there is no maximum in the interval, because the function is not bounded.
But if you change your interval by $(0,1/2)$ then you get the answer of $\approx 0.01$, check [this](https://sagecell.sagemath.org/?z=eJxL06jQVLBVyMlP1zDSMzDSqlDQVjDUVNBXMAIyQKK6yGK8XGmZeSnxOfnJiTnxuYkVmbmluRopmWlpGmk6FZo6CgY6Cob6Rpq8XAC_rxZS&lang=sage). I tested in all the other major CAS with the same both results ;)
P.S.: the maximum in the interval $(0,1/2)$ is **exactly** $0.01$. It would be interesting to see how to get this answer directly. From mathematica I get something similar to sage, but maple and [wolframalpha](http://www.wolframalpha.com/input/?i=find+maximum+1.010000000%2F(2.02*x%2B1)-1%2F(-2*x%2B1)+in+0%3C%3Dx%3C%3D1%2F2) let me exactly the correct value. I will test geogebra now.
P.S2: geogebra found the exact value too.Thu, 03 Nov 2016 11:51:23 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?answer=35391#post-id-35391Comment by kcrisman for <p>Ok, I see what is happening. The derivative of your function (the example that you put) is not bounded on the interval $(0,1)$, i.e. it is not defined in the points $-1/2$ and $1/2$ where it is going to infinity.</p>
<p>But sage works fine, it said that the maximum is in $1/2$ because it is an asymptote. In other words</p>
<p>$$\lim_{x\to\frac12^-}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=+\infty$$</p>
<p>and</p>
<p>$$\lim_{x\to\frac12^+}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=-\infty$$</p>
<p>Check this:</p>
<pre><code>http://www.wolframalpha.com/input/?i=limit+diff(log(2.02+*+x+%2B+1)+%2F+2+%2B+log(+-2+*+x+%2B+1)+%2F+2,x)+when+x-%3E1%2F2
</code></pre>
<p>So, Sage is not doing wrong... it is doing the "correct" answer. Well, the correct answer will be to say that there is no maximum in the interval, because the function is not bounded.</p>
<p>But if you change your interval by $(0,1/2)$ then you get the answer of $\approx 0.01$, check <a href="https://sagecell.sagemath.org/?z=eJxL06jQVLBVyMlP1zDSMzDSqlDQVjDUVNBXMAIyQKK6yGK8XGmZeSnxOfnJiTnxuYkVmbmluRopmWlpGmk6FZo6CgY6Cob6Rpq8XAC_rxZS&lang=sage">this</a>. I tested in all the other major CAS with the same both results ;)</p>
<p>P.S.: the maximum in the interval $(0,1/2)$ is <strong>exactly</strong> $0.01$. It would be interesting to see how to get this answer directly. From mathematica I get something similar to sage, but maple and <a href="http://www.wolframalpha.com/input/?i=find+maximum+1.010000000%2F(2.02*x%2B1)-1%2F(-2*x%2B1)+in+0%3C%3Dx%3C%3D1%2F2">wolframalpha</a> let me exactly the correct value. I will test geogebra now.</p>
<p>P.S2: geogebra found the exact value too.</p>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35394#post-id-35394Note that `find_local_maximum` is explicitly a numerical computation, not an exact solution.Thu, 03 Nov 2016 13:47:30 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35394#post-id-35394Comment by etb for <p>Ok, I see what is happening. The derivative of your function (the example that you put) is not bounded on the interval $(0,1)$, i.e. it is not defined in the points $-1/2$ and $1/2$ where it is going to infinity.</p>
<p>But sage works fine, it said that the maximum is in $1/2$ because it is an asymptote. In other words</p>
<p>$$\lim_{x\to\frac12^-}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=+\infty$$</p>
<p>and</p>
<p>$$\lim_{x\to\frac12^+}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=-\infty$$</p>
<p>Check this:</p>
<pre><code>http://www.wolframalpha.com/input/?i=limit+diff(log(2.02+*+x+%2B+1)+%2F+2+%2B+log(+-2+*+x+%2B+1)+%2F+2,x)+when+x-%3E1%2F2
</code></pre>
<p>So, Sage is not doing wrong... it is doing the "correct" answer. Well, the correct answer will be to say that there is no maximum in the interval, because the function is not bounded.</p>
<p>But if you change your interval by $(0,1/2)$ then you get the answer of $\approx 0.01$, check <a href="https://sagecell.sagemath.org/?z=eJxL06jQVLBVyMlP1zDSMzDSqlDQVjDUVNBXMAIyQKK6yGK8XGmZeSnxOfnJiTnxuYkVmbmluRopmWlpGmk6FZo6CgY6Cob6Rpq8XAC_rxZS&lang=sage">this</a>. I tested in all the other major CAS with the same both results ;)</p>
<p>P.S.: the maximum in the interval $(0,1/2)$ is <strong>exactly</strong> $0.01$. It would be interesting to see how to get this answer directly. From mathematica I get something similar to sage, but maple and <a href="http://www.wolframalpha.com/input/?i=find+maximum+1.010000000%2F(2.02*x%2B1)-1%2F(-2*x%2B1)+in+0%3C%3Dx%3C%3D1%2F2">wolframalpha</a> let me exactly the correct value. I will test geogebra now.</p>
<p>P.S2: geogebra found the exact value too.</p>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35397#post-id-35397@Masacroso in this regard WolframAlpha might prove to be a bit like MS Word. It's easy at first, but down the road, what at first seemed to be a convenience is now a bug.Thu, 03 Nov 2016 14:19:16 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35397#post-id-35397Comment by Masacroso for <p>Ok, I see what is happening. The derivative of your function (the example that you put) is not bounded on the interval $(0,1)$, i.e. it is not defined in the points $-1/2$ and $1/2$ where it is going to infinity.</p>
<p>But sage works fine, it said that the maximum is in $1/2$ because it is an asymptote. In other words</p>
<p>$$\lim_{x\to\frac12^-}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=+\infty$$</p>
<p>and</p>
<p>$$\lim_{x\to\frac12^+}\frac{\mathrm d}{\mathrm dx}\left(\frac12(\log(2.02x + 1) + \log( -2x + 1) )\right)=-\infty$$</p>
<p>Check this:</p>
<pre><code>http://www.wolframalpha.com/input/?i=limit+diff(log(2.02+*+x+%2B+1)+%2F+2+%2B+log(+-2+*+x+%2B+1)+%2F+2,x)+when+x-%3E1%2F2
</code></pre>
<p>So, Sage is not doing wrong... it is doing the "correct" answer. Well, the correct answer will be to say that there is no maximum in the interval, because the function is not bounded.</p>
<p>But if you change your interval by $(0,1/2)$ then you get the answer of $\approx 0.01$, check <a href="https://sagecell.sagemath.org/?z=eJxL06jQVLBVyMlP1zDSMzDSqlDQVjDUVNBXMAIyQKK6yGK8XGmZeSnxOfnJiTnxuYkVmbmluRopmWlpGmk6FZo6CgY6Cob6Rpq8XAC_rxZS&lang=sage">this</a>. I tested in all the other major CAS with the same both results ;)</p>
<p>P.S.: the maximum in the interval $(0,1/2)$ is <strong>exactly</strong> $0.01$. It would be interesting to see how to get this answer directly. From mathematica I get something similar to sage, but maple and <a href="http://www.wolframalpha.com/input/?i=find+maximum+1.010000000%2F(2.02*x%2B1)-1%2F(-2*x%2B1)+in+0%3C%3Dx%3C%3D1%2F2">wolframalpha</a> let me exactly the correct value. I will test geogebra now.</p>
<p>P.S2: geogebra found the exact value too.</p>
https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35396#post-id-35396@kcrisman, yes, I noticed. What is fun is that mathematica do the same thing: a numerical computation. But a lazy search on wolframalpha does the exact solution, i.e. a symbolic solution... LOL.Thu, 03 Nov 2016 13:52:06 +0100https://ask.sagemath.org/question/35374/numerical-real-solution-of-derivative/?comment=35396#post-id-35396