I am trying to fit a function with multiple variables, my fit_function returns two values, and I need to find best parameters that fit for both values.
Here is the sample code
import numpy as np from scipy.optimize import curve_fit # Fit function returns two values def func(X, a, b, c): x,y = X val1 = np.log(a) + b*np.log(x) + c*np.log(y) val2 = np.log(a)-4*val1/3 return (val1,val2) # some artificially noisy data to fit x = np.linspace(0.1,1.1,101) y = np.linspace(1.,2., 101) a, b, c = 10., 4., 6. z ,v = func((x,y), a, b, c) * 1 + np.random.random(101) / 100 # initial guesses for a,b,c: p0 = 8., 2., 7. curve_fit(func, (x,y), (z,v), p0) It works fine with fitfunction of one return value, but it is not working with two. It gives : N=3 must not exceed M=2 error.
if n > m: raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m)) Improper input: N=3 must not exceed M=2 I need to find parameters that minimize the residual between val1 - z and val2- v at the same time.
What I am missing here ?
This is how my input data looks like.

I need parameters that fits both z/x and v/x.

xvalue, a function should return oneyvalue. You seem to be returning twoyvalues, I'm not sure what you are solving. If you were to write out the equation to the fit function by hand, what would it look like?