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, 30 Jan 2020 23:24:15 +0100how to find a local maximum?https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/Hello. I'm fairly new to Sage, so lets see if someone more experienced can help!
Say I have a function f(x) continuous in [a,b] and derivable in (a,b).
How can I implement in Sage a function 'maximum(f,a,b)' that returns the maximum of f(x) in [a,b] (as a numerical approximation, not as an expression)?
Thanks you a lot!Sun, 15 Apr 2012 15:37:12 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/Answer by achrzesz for <p>Hello. I'm fairly new to Sage, so lets see if someone more experienced can help!</p>
<p>Say I have a function f(x) continuous in [a,b] and derivable in (a,b).
How can I implement in Sage a function 'maximum(f,a,b)' that returns the maximum of f(x) in [a,b] (as a numerical approximation, not as an expression)?</p>
<p>Thanks you a lot!</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=13465#post-id-13465 sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)Sun, 15 Apr 2012 17:19:02 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=13465#post-id-13465Comment by srobbert for <pre><code>sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)
</code></pre>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=35080#post-id-35080In Sage 7.3, the command is now find_local_maximum.Wed, 05 Oct 2016 20:45:25 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=35080#post-id-35080Comment by Nil for <pre><code>sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)
</code></pre>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19926#post-id-19926Can you explain why, or how, does your solution work? Thanks youSun, 15 Apr 2012 18:16:53 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19926#post-id-19926Comment by achrzesz for <pre><code>sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)
</code></pre>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19910#post-id-19910http://www.diveintopython.net/power_of_introspection/lambda_functions.html
Mon, 16 Apr 2012 18:45:07 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19910#post-id-19910Comment by Nil for <pre><code>sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)
</code></pre>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19911#post-id-19911More concretely, I was asking about the keyword lambda, because 'lambda?' yields no results.Mon, 16 Apr 2012 17:53:40 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19911#post-id-19911Comment by achrzesz for <pre><code>sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)
</code></pre>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19920#post-id-19920Read the documentation:
sage: find_maximum_on_interval??
sage: find_minimum_on_interval??
and you will see that this procedure gives you an access to scipy.optimize.fminbound
which uses Brent's methodMon, 16 Apr 2012 02:46:33 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19920#post-id-19920Answer by Dan-K for <p>Hello. I'm fairly new to Sage, so lets see if someone more experienced can help!</p>
<p>Say I have a function f(x) continuous in [a,b] and derivable in (a,b).
How can I implement in Sage a function 'maximum(f,a,b)' that returns the maximum of f(x) in [a,b] (as a numerical approximation, not as an expression)?</p>
<p>Thanks you a lot!</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=49708#post-id-49708Not sure when the change occurred, but in SageMath 9.0, `find_maximum_on_interval ` has been replaced with [`find_local_maximum`](http://doc.sagemath.org/html/en/reference/numerical/sage/numerical/optimize.html#sage.numerical.optimize.find_local_maximum).
Bear in mind this is a numerical method.Wed, 29 Jan 2020 16:04:14 +0100https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=49708#post-id-49708Comment by Emmanuel Charpentier for <p>Not sure when the change occurred, but in SageMath 9.0, <code>find_maximum_on_interval</code> has been replaced with <a href="http://doc.sagemath.org/html/en/reference/numerical/sage/numerical/optimize.html#sage.numerical.optimize.find_local_maximum"><code>find_local_maximum</code></a>.</p>
<p>Bear in mind this is a numerical method.</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=49715#post-id-49715What would be wrong with `f.diff(x).solve(x)` (or `diff(f(x),x).solve(x)`) ?Thu, 30 Jan 2020 11:48:11 +0100https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=49715#post-id-49715Comment by Dan-K for <p>Not sure when the change occurred, but in SageMath 9.0, <code>find_maximum_on_interval</code> has been replaced with <a href="http://doc.sagemath.org/html/en/reference/numerical/sage/numerical/optimize.html#sage.numerical.optimize.find_local_maximum"><code>find_local_maximum</code></a>.</p>
<p>Bear in mind this is a numerical method.</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=49718#post-id-49718The thing that's wrong with what you propose @Emmanuel Charpentier is that it doesn't find the local maximum.
For example take `y = 2*x + 3`; `find_local_maximum(y, 1, 4)` will return `(10.999999837732908, 3.9999999188664543)` while `y.diff(x).solve(x)` returns `[]`Thu, 30 Jan 2020 23:24:15 +0100https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=49718#post-id-49718Answer by achrzesz for <p>Hello. I'm fairly new to Sage, so lets see if someone more experienced can help!</p>
<p>Say I have a function f(x) continuous in [a,b] and derivable in (a,b).
How can I implement in Sage a function 'maximum(f,a,b)' that returns the maximum of f(x) in [a,b] (as a numerical approximation, not as an expression)?</p>
<p>Thanks you a lot!</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=13464#post-id-13464find_maximum_on_interval?
Sun, 15 Apr 2012 16:12:29 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?answer=13464#post-id-13464Comment by Nil for <p>find_maximum_on_interval?</p>
https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19928#post-id-19928However, this bit piece of code gives me a runtime error:
f(x) = -4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
find_maximum_on_interval(f,0,2)
This is the error:
RuntimeError: ECL says: THROW: The catch MACSYMA-QUIT is undefined.
Thanks youSun, 15 Apr 2012 16:57:04 +0200https://ask.sagemath.org/question/8890/how-to-find-a-local-maximum/?comment=19928#post-id-19928