Multivariate root lookup in Python

Using the excel solver, it is easy to find a solution for this equation (optimal value for x and y):

(x * 14,80461) + (y * -4.9233) + (10 * 0.4803) β‰ˆ 0

However, I cannot figure out how to do this in Python. An existing scipy optimize library function like fsolve () or minimumsq () seems to work with only one variable .... (I just don't know how to use them) ...

Any suggestions?

Thanks!

+4
source share
1 answer
>>> def f(x): ... return x[0]*14.80461 + x[1]*(-4.9233) + x[2]*(10*0.4803) >>> def vf(x): ... return [f(x), 0, 0] >> xx = fsolve(vf, x0=[0,0,1]) >>> >>> f(xx) 8.8817841970012523e-16 

Since the solution is not unique, different initial values ​​for the unknown lead to different (valid) solutions.

EDIT: Why does this work. Well, this is a dirty hack. It is simply that fsolve and his relatives deal with systems of equations. What I did here, I defined a system of three equations ( f(x) returns a list of three elements) for three variables ( x has three elements). Now fsolve uses a Newton type algorithm to converge to a solution.

It’s clear that the system is underdetermined: you can specify arbitrary values ​​of two variables, for example x[1] and x[2] , and find x[0] to satisfy the only nontrivial equation that you have. You can clearly see this by specifying a couple of initial guesses for x0 and see different outputs, all of which satisfy f(x)=0 up to a certain tolerance.

+5
source

All Articles