Pergunta

I have a five-dimensional rootfinding problem I'd like to solve from within a Sage notebook, but the functions I wish to solve depend on other parameters that shouldn't be varied during the rootfinding. Figuring out how to set up a call to, say, scipy.optimize.newton_krylov has got me stumped. So let's say I have (with a,b,c,d,e the parameters I want to vary, F1,F2,F3,F4,F5 the five expressions I which to solve to be equal to F1Val,F2Val,F3Val,F4Val,F5Val, values I already know, and posVal another known parameter)

def func(a, b, c, d, e, F1Val, F2Val, F3Val, F4Val, F5Val, posVal):
    F1.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
    F2.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
    F3.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
    F4.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
    F5.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
    return (F1-F1Val, F2-F2Val, F3-F3Val, F4-F4Val, F5-F5Val)

and now I want to pass this to a rootfinding function to yield func = (0,0,0,0,0). I want to pass an initial guess (a0, b0, c0, d0, e0) vector and a set of arguments (F1Val, F2Val, F3Val, F4Val, F5Val, posVal) for the evaluation, but I can't figure out how to do this. Is there a standard technique for this sort of thing? The multidimensional rootfinders in scipy seem to be lacking the args=() variable that the 1D rootfinders offer.

Best,

-user2275987

Foi útil?

Solução

Well, I'm still not sure how to actually employ the Newton-Raphson method here, but using fsolve works, for functions that accept a vector of variables and a vector of constant arguments. I'm reproducing my proof of concept here

def tstfunc(xIn, constIn):
    x = xIn[0]
    y = xIn[1]
    a = constIn[0]
    b = constIn[1]
    out = [x+2*y+a]
    out.append(a*x*y+b)
    return out

from scipy.optimize import fsolve
ans = fsolve(tstfunc, x0=[1,1], args=[0.3, 2.1])
print ans
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top