I am trying to fit some data to a non-linear model with two independent variables, but the length of vectors for the two independent variables are, that is xdat
is smaller than ydat
.
This is closely related to this question: Python curve_fit with multiple independent variables, but the requirement that xdat
and ydat
are different sizes seems to break things.
Let's take the example solution of xnx, but change the length of one of the arrays:
import numpy as np
from scipy.optimize import curve_fit
def func(X, a, b, c):
x,y = X
return np.log(a) + b*np.log(x) + c*np.log(y)
# some artificially noisy data to fit
x = np.linspace(0.1,1.1,101)
y = np.linspace(1.,2., 90) #I have changed the length of one of these arrays
a, b, c = 10., 4., 6.
z = func((x,y), a, b, c) * 1 + np.random.random(101) / 100
# initial guesses for a,b,c:
p0 = 8., 2., 7.
print curve_fit(func, (x,y), z, p0)
if you do this, then you end up with the error:
ValueError: operands could not be broadcast together with shapes (101,) (90,)
Is there a way to force curve fit to take arrays of different lengths?