CoCalc Public Filessage-scipy-root.ipynb
Author: Harald Schilly
Views : 51
Compute Environment: Ubuntu 20.04 (Experimental)

# Find a root of a vector function via SciPY in SageMath

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root.html

In [1]:
from scipy.optimize import root as scipy_root

In [2]:
x, y, z, t = var('x y z t')

In [3]:
f(x, y, z, t) = 3 + t + z^2
g(x, y, z, t) = 2 + x + z
h(x, y, z, t) = x * t + y * z

In [4]:
def fun(x):
xp, yp, zp, tp = x
return [
f(x=xp, y=yp, z=zp, t=tp).n(),
g(x=xp, y=yp, z=zp, t=tp).n(),
h(x=xp, y=yp, z=zp, t=tp).n(),
0
]


## running scipy's root function

make sure to check for "The solution converged." !!!

In [5]:
sol = scipy_root(fun, [0, 0, 0, 0], method='hybr')
sol

fjac: array([[ 0.00840132, -0.25803323, 0.96609951, 0. ], [ 0.11881741, -0.95903156, -0.25717872, 0. ], [-0.99288058, -0.11695009, -0.02260171, 0. ], [ 0. , -0. , -0. , 1. ]]) fun: array([ 2.28572716e-12, -2.22044605e-16, 1.65245595e-11, 0.00000000e+00]) message: 'The solution converged.' nfev: 68 qtf: array([ 1.60467670e-08, -3.99387982e-09, -2.65363115e-09, 0.00000000e+00]) r: array([-3.87546977, -0.44493312, 5.21488509, -1.17178519, 0.11971194, -2.59724276, 0.43299966, 1.24176148, -0.96537612, 0. ]) status: 1 success: True x: array([-1.463562 , 8.97000015, -0.536438 , -3.28776573])
In [6]:
xs, yx, zs, ts = sol.x

In [7]:
f(xs, yx, zs, ts)

2.2857271630982723e-12
In [8]:
g(xs, yx, zs, ts)

-2.220446049250313e-16
In [9]:
h(xs, yx, zs, ts)

1.652455949852083e-11
In [ ]:


In [ ]: