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.Fri, 06 Sep 2013 11:49:23 +0200A very nonlinear system of three equationshttps://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/Here's a fun little problem: determine the exponential curve f(x) = c + ba^x defined by three points, (2,10), (4,6), and (5,5).
The system of three equations and three unknowns is
10 = c + ba^2
6 = c + ba^4
5 = c + ba^5
It's not that hard to solve numerically. With a little algebraic substitution and iteration, the answer turns out to be
a = 0.640388203
b = 16.53456516
c = 3.219223594
But is there a more elegant way to use Sage to arrive at this result? I'm stumped.Thu, 05 Sep 2013 04:15:08 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/Answer by kcrisman for <p>Here's a fun little problem: determine the exponential curve f(x) = c + ba^x defined by three points, (2,10), (4,6), and (5,5).</p>
<p>The system of three equations and three unknowns is</p>
<p>10 = c + ba^2</p>
<p>6 = c + ba^4</p>
<p>5 = c + ba^5</p>
<p>It's not that hard to solve numerically. With a little algebraic substitution and iteration, the answer turns out to be</p>
<p>a = 0.640388203</p>
<p>b = 16.53456516</p>
<p>c = 3.219223594</p>
<p>But is there a more elegant way to use Sage to arrive at this result? I'm stumped.</p>
https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?answer=15417#post-id-15417Have you tried `find_fit`? Presumably with an exact fit this would work very nicely. (Let me know if you need more details or if that doesn't work.)
That said, this isn't "elegant".Thu, 05 Sep 2013 09:23:45 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?answer=15417#post-id-15417Comment by OrionNav for <p>Have you tried <code>find_fit</code>? Presumably with an exact fit this would work very nicely. (Let me know if you need more details or if that doesn't work.)</p>
<p>That said, this isn't "elegant".</p>
https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17037#post-id-17037Thanks! That works well enough. My first attempt gave an unexpected answer:
model(x) = c+b*a^x
find_fit([(2,10), (4,6), (5,5)], model)
[a == -0.39038820320220763, b == 30.965434844830888, c == 5.2807764064044145]
which is correct as far as it goes, but raising -0.39 to non-integer values of x results in a complex number.
Giving it a non-default initial guess sussed out the expected answer:
model(x) = c+b*a^x
find_fit([(2,10), (4,6), (5,5)], model, initial_guess=(1,1,3))
[a == 0.6403882032022076, b == 16.534565155169098, c == 3.219223593595585]
and I'm pleased with the accuracy of the result.
Since this can be done with only two lines of code, I consider it more elegant than the fsolve approach (above).Fri, 06 Sep 2013 00:27:39 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17037#post-id-17037Comment by kcrisman for <p>Have you tried <code>find_fit</code>? Presumably with an exact fit this would work very nicely. (Let me know if you need more details or if that doesn't work.)</p>
<p>That said, this isn't "elegant".</p>
https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17031#post-id-17031Yes, sometimes you do need to give it a little help! That's typical of such numerical routines.Fri, 06 Sep 2013 11:49:23 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17031#post-id-17031Answer by Mark for <p>Here's a fun little problem: determine the exponential curve f(x) = c + ba^x defined by three points, (2,10), (4,6), and (5,5).</p>
<p>The system of three equations and three unknowns is</p>
<p>10 = c + ba^2</p>
<p>6 = c + ba^4</p>
<p>5 = c + ba^5</p>
<p>It's not that hard to solve numerically. With a little algebraic substitution and iteration, the answer turns out to be</p>
<p>a = 0.640388203</p>
<p>b = 16.53456516</p>
<p>c = 3.219223594</p>
<p>But is there a more elegant way to use Sage to arrive at this result? I'm stumped.</p>
https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?answer=15421#post-id-15421If you just want to have a numerical solution to a small set of (nonlinear) equations, why not simply 'fsolve' them?
from scipy.optimize import fsolve
def equations(p):
a, b, c = p
return (c + b*abs(a)**2 - 10, c + b*abs(a)**4 - 6, c + b*abs(a)**5 - 5)
a,b,c = fsolve(equations, (1, 1, 1))
print a,b,c
print equations((a,b,c))
0.640388203202 16.5345651552 3.2192235936
(-2.5757174171303632e-13, -2.3625545964023331e-13,-1.9895196601282805e-13)
The 'abs()' is there to provide at least some additional constraining on a,b,c
I don't know if this is elegant - but it is straightforward.
Thu, 05 Sep 2013 16:42:10 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?answer=15421#post-id-15421Comment by OrionNav for <p>If you just want to have a numerical solution to a small set of (nonlinear) equations, why not simply 'fsolve' them?</p>
<pre><code>from scipy.optimize import fsolve
def equations(p):
a, b, c = p
return (c + b*abs(a)**2 - 10, c + b*abs(a)**4 - 6, c + b*abs(a)**5 - 5)
a,b,c = fsolve(equations, (1, 1, 1))
print a,b,c
print equations((a,b,c))
0.640388203202 16.5345651552 3.2192235936
(-2.5757174171303632e-13, -2.3625545964023331e-13,-1.9895196601282805e-13)
</code></pre>
<p>The 'abs()' is there to provide at least some additional constraining on a,b,c</p>
<p>I don't know if this is elegant - but it is straightforward.</p>
https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17036#post-id-17036Thanks. I suspect that some or all of this is Python code, and I know absolutely nothing about Python. (I'm a decent Perl coder, though.)
The Sage Reference Manual has information about maple.fsolve and qfsolve, but nothing about plain old fsolve.Fri, 06 Sep 2013 00:34:02 +0200https://ask.sagemath.org/question/10506/a-very-nonlinear-system-of-three-equations/?comment=17036#post-id-17036