Find Roots Of A System Of Equations To An Arbitrary Decimal Precision
Given an initial guess for an array of values x, I am trying to find the root of a system that is closest to x. If you are familiar with finding roots of a system, you will unders
Solution 1:
You could try copying the code in and referenced by scipy.optimize.newton_krylov
then modifying it to use decimal
values rather than floating point values. This may be difficult and time-consuming, of course.
I have done the equivalent for other situations, but never quite this.
Solution 2:
I have found the mpmath
module, which contains mpmath.findroot
. mpmath
uses arbitrary decimal-point precision for all of its numbers. mpmath.findroot
will find the nearest root within tolerance. Here is an example of using mpmath
for the same problem, to a higher precision:
import scipy.optimize as sp
import mpmath
from mpmath import mpf
mpmath.mp.dps = 15defmp_f(x1, x2):
f1 = (x1**2) + (3*(x2**3)) - 2
f2 = x1 * (x2**2)
return f1, f2
deff(x):
f0 = (x[0]**2) + (3*(x[1]**3)) - 2
f1 = x[0] * (x[1]**2)
return [f0, f1]
tmp_solution = sp.newton_krylov(f, [2, .01], f_tol=Dc('1e-10'))
print tmp_solution
>>> [ 1.41421356e+004.87315249e-06]
for _ inrange(8):
tmp_solution = mpmath.findroot(mp_f, (tmp_solution[0], tmp_solution[1]))
print tmp_solution
mpmath.mp.dps += 10# Increase precision>>> [ 1.4142135623731]
[4.76620313173184e-9]
>>> [ 1.414213562373095048801689]
[4.654573673348783724565804e-12]
>>> [ 1.4142135623730950488016887242096981]
[4.5454827012374811707063801808968925e-15]
>>> [ 1.41421356237309504880168872420969807856967188]
[4.43894795688326535096068850443292395286770757e-18]
>>> [ 1.414213562373095048801688724209698078569671875376948073]
[4.334910114213471839327827177504976152074382061299675453e-21]
>>> [ 1.414213562373095048801688724209698078569671875376948073176679738]
[4.2333106584123451747941381835420647823192649980317402073699554127e-24]
>>> [ 1.41421356237309504880168872420969807856967187537694807317667973799073247846]
[4.1340924398558139440207202654766836515453497962889870471467483995909717197e-27]
>>> [ 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885]
[4.037199648296693366576484784520203892002447351324378380584214947262318103197216393589e-30]
The precision can be raised arbitrarily.
Post a Comment for "Find Roots Of A System Of Equations To An Arbitrary Decimal Precision"