Hi,
I'm trying to use cvxpy to fit experimental data (they 're obeying a parametrized model). I tried lmfit and other stuff included in scipy, but it didn't work because it is impossible to define complex constraints with these modules.
Here is my model, defined in the scipy way:
def model(t, y0, h, A1, x1, s1, A2, x2, s2, A3, x3, s3, A4, x4, s4):
y = y0 + h * (
A1 * (0.5 + 0.5 * erf((t - x1) / (s1 * 2**0.5))) +
A2 * (0.5 + 0.5 * erf((t - x2) / (s2 * 2**0.5))) +
A3 * (0.5 + 0.5 * erf((t - x3) / (s3 * 2**0.5))) +
A4 * (0.5 + 0.5 * erf((t - x4) / (s4 * 2**0.5)))
)
return y
Basically, "t" and "y" are array of values obtained experimentally. And y = f(t), very basic.
I'm trying to fit the experimental data, and get values for y0, h, A1, A2, A3, A4, x1, x2, x3, x4, s1, s2, s3 and s4. I would like to try a basic approach, a least square fitting.
For now I'm just getting started. I tried this:
t = cvx.Variable(self.x)
y = cvx.Variable(self.y)
y0 = cvx.Variable()
h = cvx.Variable()
A1 = cvx.Variable()
A2 = cvx.Variable()
A3 = cvx.Variable()
A4 = cvx.Variable()
x1 = cvx.Variable()
x2 = cvx.Variable()
x3 = cvx.Variable()
x4 = cvx.Variable()
s1 = cvx.Variable()
s2 = cvx.Variable()
s3 = cvx.Variable()
s4 = cvx.Variable()
obj = cvx.Minimize(y0 + h * (
A1 * (0.5 + 0.5 * erf((t - x1) / (s1 * 2**0.5))) +
A2 * (0.5 + 0.5 * erf((t - x2) / (s2 * 2**0.5))) +
A3 * (0.5 + 0.5 * erf((t - x3) / (s3 * 2**0.5))) +
A4 * (0.5 + 0.5 * erf((t - x4) / (s4 * 2**0.5)))
))
prob = cvx.Problem(obj)
prob.solve()
I'm well aware I didn't define any constraint for now, I'm just starting. I'm trying to undesrtand how the module works for now.
The previous code gives:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Could you give me a hand please, and help me to start ?