Fsolve - mismatch between input and output

I am trying to solve an overdetermined system of equations with three unknowns. I can get a solution with fsolve and lsqnonlin in MATLAB by calling the system of equations through a for loop.

But in python using scipy I get the following error message:

fsolve: there is a mismatch between the input and output shape of the 'func' argument 'fnz'

      

The code is below:

from xlrd import open_workbook
import numpy as np
from scipy import optimize
g = [0.5,1,1.5]
wb = open_workbook('EThetaValuesA.xlsx')
sheet=wb.sheet_by_index(0)
y=sheet.col_values(0,1)
t1=sheet.col_values(1,1)
t2=sheet.col_values(2,1)
t3=sheet.col_values(3,1)

def fnz(g):
    i=0
    sol=[0 for i in range(len(t1))]
    x1 = g[0]
    x2 = g[1]
    x3 = g[2]
    print len(t1)
    for i in range(len(t1)):
        # various set of t1,t2 and t3 gives the various eqns
        print i
        sol[i]=x1+t1[i]/(x2*t2[i]+x3*t3[i])-y[i]    
    return sol

Anz = optimize.fsolve(fnz,g)

print Anz

      

Could you please suggest where I am going wrong? Thank you in advance.

0


source to share


1 answer


An exception means that the result of the function call fnz()

does not have the same size as the input g

, which is a list of three elements or can be thought of as array

forms (3,)

.

To illustrate the problem, if we define:

def fnz(g):
    return [2,3,5]
Anz = optimize.fsolve(fnz,g)

      



There will be no such exception. But it will:

def fnz(g):
    return [2,3,4,5]
Anz = optimize.fsolve(fnz,g)

      

The result from fnz()

should be the same length as t1

, which I'm sure is longer than three elements.

0


source







All Articles