Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
| Download
Views: 226
Image: ubuntu2004-dev
Kernel: SageMath 9.3

Find a root of a vector function via SciPY in SageMath

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

from scipy.optimize import root as scipy_root
x, y, z, t = var('x y z t')
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
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." !!!

sol = scipy_root(fun, [0, 0, 0, 0], method='hybr') sol
fjac: array([[ 0.00840245, -0.25803071, 0.96610018, 0. ], [ 0.11882016, -0.95903187, -0.25717629, 0. ], [-0.99288024, -0.11695309, -0.02260104, 0. ], [ 0. , 0. , 0. , 1. ]]) fun: array([ 2.28483898e-12, -2.22044605e-16, 1.65201186e-11, 0.00000000e+00]) message: 'The solution converged.' nfev: 68 qtf: array([ 1.60864901e-08, -4.00371453e-09, -2.66020064e-09, 0.00000000e+00]) r: array([-3.87550761, -0.44493695, 5.21493561, -1.17178494, 0.11971177, -2.59724491, 0.43299946, 1.24175494, -0.96537661, 0. ]) status: 1 success: True x: array([-1.46356043, 8.96996905, -0.53643957, -3.28776741])
xs, yx, zs, ts = sol.x
f(xs, yx, zs, ts)
2.284838984678572e-12
g(xs, yx, zs, ts)
-2.220446049250313e-16
h(xs, yx, zs, ts)
1.652011860642233e-11