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.Sun, 25 Oct 2020 12:53:05 +0100how do I find multiple inflection points?https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/I know how to find multiple zeros by repeating find_root over an interval. But how do I find multiple inflection points? Sin has an infinite number but like find_root I'd like to just find them over various intervals. However solve(diff(f)==0,x) doesn't have an option for choosing an interval.Sat, 24 Oct 2020 02:55:56 +0200https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/Answer by Emmanuel Charpentier for <p>I know how to find multiple zeros by repeating find_root over an interval. But how do I find multiple inflection points? Sin has an infinite number but like find_root I'd like to just find them over various intervals. However solve(diff(f)==0,x) doesn't have an option for choosing an interval.</p>
https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?answer=54018#post-id-54018I *suppose* this is not homework. If it is, you'd better try to solve it yourself, peeking this answer only for confirmation, unless you want to miss the point of the homework.
The difficulty isn't to find one solution, but to find them all. In some cases, `solve` might be able to give them an explicit form. Not here.
`find_root` will give you *one* real root in this interval if such one exists. To find the others, you might try searching them in the subintervals defined by this root (with a slight restriction to avoid finding the same root repetitively). However, such a recursive computation should be limited in avoid to (try to) return an infinite number of solutions. Hence the proposal :
def find_roots(f, a, b, depth=10, epsilon=1e-10):
def find1(f, a, b, **kwdargs):
try:
r =find_root(f, a, b)
return r
except RuntimeError:
return None
if a > b: a, b = b, a
r = find1(f, a, b)
if r is None: return set([])
if depth == 0 : return set([r])
a1 = a + (r - a) * epsilon
b1 = r - (r - a) * epsilon
a2 = r + (b - r) * epsilon
b2 = b - (b - r) * epsilon
R1 = find_roots(f, a1, b1, depth=depth-1, epsilon=epsilon)
R2 = find_roots(f, a2, b2, depth=depth-1, epsilon=epsilon)
return set([r]).union(R1).union(R2)
[ Handling and passing the optional arguments to `find_root` is left as an exercise to the reader...]
Sol=find_roots((x^2*cos(2*x)).diff(x,2),-10,10) ; Sol
{-8.75241852237047,
-7.205215809879128,
-5.66982003470027,
-4.156992355472504,
-2.692864827014006,
-1.3444837495903466,
-0.299870710513912,
0.299870710513912,
1.3444837495903472,
2.692864827014006,
4.156992355472463,
5.669820034700045,
7.205215809879129,
8.752418522370563}
This proposal of fourteen roots is consistent with the graphical representation :
sage: (x^2*cos(2*x)).diff(x,2).plot((-10,10), ymin=-5, ymax=5)
However, if this answer is numerically satisfying, *it is not a proof,* which should be searched by other (formal) means.
The `depth` parameter has effects varying as a function of the implementation of `find_root`, which does not explicitly documents its algorithm. FWIW, the default `depth=10` allows `find_roots` to return 63 of the (countable) infinity of roots of $\sin{\frac{1}{x}}$ between 0 and 1:
sage: len(find_roots(sin(1/x),0,1))
63
HTH,Sat, 24 Oct 2020 22:38:33 +0200https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?answer=54018#post-id-54018Comment by cybervigilante for <p>I <em>suppose</em> this is not homework. If it is, you'd better try to solve it yourself, peeking this answer only for confirmation, unless you want to miss the point of the homework.</p>
<p>The difficulty isn't to find one solution, but to find them all. In some cases, <code>solve</code> might be able to give them an explicit form. Not here.</p>
<p><code>find_root</code> will give you <em>one</em> real root in this interval if such one exists. To find the others, you might try searching them in the subintervals defined by this root (with a slight restriction to avoid finding the same root repetitively). However, such a recursive computation should be limited in avoid to (try to) return an infinite number of solutions. Hence the proposal :</p>
<pre><code> def find_roots(f, a, b, depth=10, epsilon=1e-10):
def find1(f, a, b, **kwdargs):
try:
r =find_root(f, a, b)
return r
except RuntimeError:
return None
if a > b: a, b = b, a
r = find1(f, a, b)
if r is None: return set([])
if depth == 0 : return set([r])
a1 = a + (r - a) * epsilon
b1 = r - (r - a) * epsilon
a2 = r + (b - r) * epsilon
b2 = b - (b - r) * epsilon
R1 = find_roots(f, a1, b1, depth=depth-1, epsilon=epsilon)
R2 = find_roots(f, a2, b2, depth=depth-1, epsilon=epsilon)
return set([r]).union(R1).union(R2)
</code></pre>
<p>[ Handling and passing the optional arguments to <code>find_root</code> is left as an exercise to the reader...]</p>
<pre><code>Sol=find_roots((x^2*cos(2*x)).diff(x,2),-10,10) ; Sol
{-8.75241852237047,
-7.205215809879128,
-5.66982003470027,
-4.156992355472504,
-2.692864827014006,
-1.3444837495903466,
-0.299870710513912,
0.299870710513912,
1.3444837495903472,
2.692864827014006,
4.156992355472463,
5.669820034700045,
7.205215809879129,
8.752418522370563}
</code></pre>
<p>This proposal of fourteen roots is consistent with the graphical representation :</p>
<pre><code>sage: (x^2*cos(2*x)).diff(x,2).plot((-10,10), ymin=-5, ymax=5)
</code></pre>
<p>However, if this answer is numerically satisfying, <em>it is not a proof,</em> which should be searched by other (formal) means.</p>
<p>The <code>depth</code> parameter has effects varying as a function of the implementation of <code>find_root</code>, which does not explicitly documents its algorithm. FWIW, the default <code>depth=10</code> allows <code>find_roots</code> to return 63 of the (countable) infinity of roots of $\sin{\frac{1}{x}}$ between 0 and 1:</p>
<pre><code>sage: len(find_roots(sin(1/x),0,1))
63
</code></pre>
<p>HTH,</p>
https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54021#post-id-54021I'm 71 so it's not homework unless I'm a real late starter ðŸ˜€ And I was unclear. I'm not interested in roots, but in the points where the derivative is zero, which I called "inflection points." Maybe they don't use that term anymore. Been fifty years since college.Sun, 25 Oct 2020 06:00:32 +0100https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54021#post-id-54021Comment by Emmanuel Charpentier for <p>I <em>suppose</em> this is not homework. If it is, you'd better try to solve it yourself, peeking this answer only for confirmation, unless you want to miss the point of the homework.</p>
<p>The difficulty isn't to find one solution, but to find them all. In some cases, <code>solve</code> might be able to give them an explicit form. Not here.</p>
<p><code>find_root</code> will give you <em>one</em> real root in this interval if such one exists. To find the others, you might try searching them in the subintervals defined by this root (with a slight restriction to avoid finding the same root repetitively). However, such a recursive computation should be limited in avoid to (try to) return an infinite number of solutions. Hence the proposal :</p>
<pre><code> def find_roots(f, a, b, depth=10, epsilon=1e-10):
def find1(f, a, b, **kwdargs):
try:
r =find_root(f, a, b)
return r
except RuntimeError:
return None
if a > b: a, b = b, a
r = find1(f, a, b)
if r is None: return set([])
if depth == 0 : return set([r])
a1 = a + (r - a) * epsilon
b1 = r - (r - a) * epsilon
a2 = r + (b - r) * epsilon
b2 = b - (b - r) * epsilon
R1 = find_roots(f, a1, b1, depth=depth-1, epsilon=epsilon)
R2 = find_roots(f, a2, b2, depth=depth-1, epsilon=epsilon)
return set([r]).union(R1).union(R2)
</code></pre>
<p>[ Handling and passing the optional arguments to <code>find_root</code> is left as an exercise to the reader...]</p>
<pre><code>Sol=find_roots((x^2*cos(2*x)).diff(x,2),-10,10) ; Sol
{-8.75241852237047,
-7.205215809879128,
-5.66982003470027,
-4.156992355472504,
-2.692864827014006,
-1.3444837495903466,
-0.299870710513912,
0.299870710513912,
1.3444837495903472,
2.692864827014006,
4.156992355472463,
5.669820034700045,
7.205215809879129,
8.752418522370563}
</code></pre>
<p>This proposal of fourteen roots is consistent with the graphical representation :</p>
<pre><code>sage: (x^2*cos(2*x)).diff(x,2).plot((-10,10), ymin=-5, ymax=5)
</code></pre>
<p>However, if this answer is numerically satisfying, <em>it is not a proof,</em> which should be searched by other (formal) means.</p>
<p>The <code>depth</code> parameter has effects varying as a function of the implementation of <code>find_root</code>, which does not explicitly documents its algorithm. FWIW, the default <code>depth=10</code> allows <code>find_roots</code> to return 63 of the (countable) infinity of roots of $\sin{\frac{1}{x}}$ between 0 and 1:</p>
<pre><code>sage: len(find_roots(sin(1/x),0,1))
63
</code></pre>
<p>HTH,</p>
https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54023#post-id-54023Please *read* my answer, instead of skimming it : I have searched the roots of `(x^2*cos(2*x)).diff(x,2)`, i. e. the points where the **second derivative** of $x^2\cos{2x}$ is null, i. e. the inflection points of $x^2\cos{2x}$.Sun, 25 Oct 2020 09:28:57 +0100https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54023#post-id-54023Answer by tmonteil for <p>I know how to find multiple zeros by repeating find_root over an interval. But how do I find multiple inflection points? Sin has an infinite number but like find_root I'd like to just find them over various intervals. However solve(diff(f)==0,x) doesn't have an option for choosing an interval.</p>
https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?answer=54017#post-id-54017Note that `diff(f)` is also a symbolic function, so you can use `find_root` on it as well, and specify an interval, e.g.
find_root(diff(f), 0, 1)Sat, 24 Oct 2020 14:57:01 +0200https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?answer=54017#post-id-54017Comment by cybervigilante for <p>Note that <code>diff(f)</code> is also a symbolic function, so you can use <code>find_root</code> on it as well, and specify an interval, e.g. </p>
<pre><code>find_root(diff(f), 0, 1)
</code></pre>
https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54024#post-id-54024Ah, that clarifies it. I just wanted to find the xval where a more complicated function changes direction in particular ranges that I can iterate over:
`find_root(diff((x^2)*cos(2*x)),-5,-2)` then results in -3.2891668663611693, which corresponds with its graph., that I put in above to clarify. And the other points are easy to find with a loop. Well, not all them, of course, being infinite ðŸ˜€ I was just used to thinking of find_root to find zeros on the graph, rather than zeros of derivatives.Sun, 25 Oct 2020 12:53:05 +0100https://ask.sagemath.org/question/54016/how-do-i-find-multiple-inflection-points/?comment=54024#post-id-54024