MATLAB: How to do parameter fitting for a system of equations with 3 independent variables and a complex function expression

a complex function expressionfit

I have a system of 3 equations with 3 independent variables( x1,x2,x3) and correspondingly 1 dependent variables y as follows: y=k1(1-exp(k2*x1))*x3^2+k3*x3*(x1*x2)^2+k4*x3*(x1*x2)+k5*x3*(x1*x2)^0.5+k6*x3+k7-K7exp(k8*x1) x and y data are available.
I need to fit 8 parameters( k1,k2,k3,k4,k5,k6,k7,k8) in this system.
My code as follow: xdata = horzcat(x1,x2,x3,x4,x5); ydata = horzcat(y1,y2,y3); fun=@(b,xdata)horzcat(b(1)*x5-b(1)*exp(b(2)*x1)*x3.*x3+b(3)*x3.*(x1.*x2)+b(4)*x3.*x2+b(5)*x3.*x2^0.5+b(6)*x3+b(7)-b(7)*exp(b(8)*x1),0,0); b0=[0;0;0;0;0;0;0;0]; b = lsqcurvefit(fun,b0,xdata,ydata)
the operation result is :Matrix dimensions must be consistent.
mistake: @(b,xdata)horzcat(b(1)*x5-b(1)*exp(b(2)*x1)*x3.*x3+b(3)*x3.*(x1.*x2)+b(4)*x3.*x2+b(5)*x3.*x2^0.5+b(6)*x3+b(7)-b(7)*exp(b(8)*x1),0,0)
Failure:lsqcurvefit (line 198) initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
I tried using lsqcurvefit but couldn't make it work for this case. Suggest me how to solve this problem. Thank you.

Best Answer

You didn’t show your code, so I’m not certain what you’re doing.
You have to combine your independent variables in a matrix (I prefer column vectors for the independent and dependent variables), present them as one variable, then address the individual columns as the individual variables in your objective function.
For example:
x1x2x3 = [x1(:) x2(:) x3(:)];
y = y(:);
y = (k,x) k(1).*(1-exp(k(2).*x1x2x3(:,1))).*x1x2x3(:,3).^2 + ...
I’m not certain I parsed the parentheses correctly, so I’ll let you sort that.
Here, you would call nlinfit as:
k_est = nlinfit(x1x2x3, y, modelfun, k0);