Reputation: 20045
I have the function:
f(x) = 1 / (x + a)^t + b
And I would like to solve for a given t
for a
and b
the equation system {f(0)=1 and f(1)=0}
.
For t=1 a solution is successfully calculated:
import sympy as sp
a,b = sp.symbols("a b")
res = sp.solve([1/(a+1)**1 +b, 1/a**1+b-1], [a,b])
res
# [(-1/2 + sqrt(5)/2, -sqrt(5)/2 + 1/2), (-sqrt(5)/2 - 1/2, 1/2 + sqrt(5)/2)]
But for any t other than 1 (and most of the time also 2) no solution is found:
import sympy as sp
a,b = sp.symbols("a b")
res = sp.solve([1/(a+1)**1.5 +b, 1/a**1.5+b-1], [a,b])
res
gives:
NotImplementedError: could not solve
b*(-(1 + sqrt(3)*I)*(1/(b**2 - 2*b + 1))**(1/3)/2 + 1)**(3/2) + 1
Is it possible to approach this problem in SymPy from a more efficient angle?
Suggestions for Python packages useful for tackling this are also most welcome.
Upvotes: 3
Views: 1323
Reputation: 929
What assumptions are you making about t
?
You can of course solve the system of non-linear equations numerically using e.g. scipy.optimize.root
.
I have written an experimental package pyneqsys to help with this when you start from symbolic expressions. In your case I would use it as follows:
>>> import sympy as sp
>>> from pyneqsys.symbolic import SymbolicSys
>>> a, b, t = sp.symbols('a b t')
>>> f = lambda x: 1/(x+a)**t + b
>>> neqsys = SymbolicSys([a, b], [f(0) - 1, f(1) - 0], [t])
>>> ab, sol = neqsys.solve_scipy([0.5, -0.5], 1)
>>> ab, sol.success
(array([ 0.61803399, -0.61803399]), True)
You could also plot the result as you vary t
from say 0.5 to 3:
>>> def solve(tval, guess=(.5, -.5)):
... vals, sol = neqsys.solve_scipy(guess, tval)
... assert sol.success
... return vals
...
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> trange = np.linspace(.5, 3)
>>> plt.plot(trange, np.array([solve(t_) for t_ in trange]))
Upvotes: 1