scipy - using python to do 3-D surface fitting -
i can use module(scipy.optimize.least_squares) 1-d curve fitting(of course,i can use curve_fit module directly) , this
def f(par,data,obs): return par[0]*data+par[1]-obs def get_f(x,a,b): return x*a+b data = np.linspace(0, 50, 100) obs = get_f(data,3.2,2.3) par = np.array([1.0, 1.0]) res_lsq = least_squares(f, par, args=(data, obs)) print res_lsq.x
i can right fitting parameter (3.2,2.3),but when generalize method multi-dimension,like this
def f(par,data,obs): return par[0]*data[0,:]+par[1]*data[1,:]-obs def get_f(x,a,b): return x[0]*a+b*x[1] data = np.asarray((np.linspace(0, 50, 100),(np.linspace(0, 50, 100)) ) ) obs = get_f(data,1.,1.) par = np.array([3.0, 5.0]) res_lsq = least_squares(f, par, args=(data, obs)) print res_lsq.x
i find can not right answer, i.e (1.,1.),i have no idea whether have made mistake.
the way generate data , observations in "multi-dimensional" case results in get_f
returning (a+b)*x[0]
(input values x[0]
, x[1]
same) and, similarly, f
returning (par[0]+par[1])*data[0]-obs
. of course, a=1
, b=1
, exact same obs
generated other values a
, b
such a+b=1
. scipy correctly returns 1 of (infinite) possible values satisfying constraint, depending on initial estimate.
Comments
Post a Comment