create a function containing a system of eqs - fsolve

This is the place for queries that don't fit in any of the other categories.

create a function containing a system of eqs - fsolve

Postby poumaipoumai » Mon Apr 01, 2013 4:25 pm

Lets say that I want to solve a system of nonlinear equations that are created from an iterative expression, i.e. x[i]-2*x[i-2]...=1/x[i]**2.

In order to achieve that I want to use fsolve from scipy package. So I have first to create a function f(x), i.e.

Code: Select all
def func f(x, imax):

    for i in range(imax):

      y = [ x[i]-2*x[i-2]...-1/x[i]**2, ] #this should be a matrix containing the 

                                          #equations in where all the equations are

                                          # seperated with ','
return y

After define the function you use fsolve like this: scipy.optimize.fsolve( func, inGuess, args = ( imax ). So the argument to fsolve is a function containing the system of equations. My problem is how to create this function having the number of equations depended from imax. e.g. I want to write fsolve(func,args(10)), or fsolve(func,args(20)) etc

This is an example that works:
Code: Select all
>>> def func2(x): y = [x[0]*cos(x[1]) - 4, x[1]*x[0] - x[1] - 5] return y
>>> x0 = scipy.optimize.fsolve(func2, [1, 1])
>>> print x0
[ 6.5041, 0.9084]
Posts: 1
Joined: Mon Apr 01, 2013 4:18 pm

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests