MATLAB: How to use non-linear regression with two input variables

nonlinearregression

I have three types of data and all three are matrices of same size. One of them can be expressed as a function of the other two as, Z = aX/ (1 + b * e^(-Y/c)), where X, Y and Z are matrices of same size, say nxm, and I have to find coefficient matrix with a,b & c which would fit the equation. For finding coefficients and fitting them to my data, the code I have written is as follows:
myfun = @(p,Y,X) p(1).*X./(1+p(2).*exp(-Y./p(3)));
param = [3000 0.2 10];
After this I am confused about fitting my data.
Thank you in advance.

Best Answer

You have to engage in a bit of deception to fit two independent variables, because the MATLAB curve fitting functions only allow one independent variable in the argument list.
Combine your ‘X’ and ‘Y’ matrices into one variable:
XY(:,:,1) = X;
XY(:,:,2) = Y;
then change ‘myfun’ to:
myfun = @(p,XY) p(1).*XY(:,:,1)./(1+p(2).*exp(-XY(:,:,2)./p(3)));
I tested this with matrices and it successfully returns a matrix result. I didn’t use it in an actual regression.
Related Question