How can I perform nonlinear regression with two input variables and one dependent variable
2 views (last 30 days)
Show older comments
I have a data set with three columns, say, Y, X1 and X2 of which Y is the dependent variable (on X1 and X2). I need to know how to use MATLAB to perform nonlinear regression with two input variables for a particular model
y=f(X1,X2)=(1+aX1+bX2)/(1+cX1+dX2).
0 Comments
Answers (3)
John D'Errico
on 12 Aug 2016
Edited: John D'Errico
on 12 Aug 2016
Simpler than using nonlinear regression is to use LINEAR regression.
If you have
y=f(X1,X2)=(1+aX1+bX2)/(1+cX1+dX2)
then multiply by (1+cX1+dX2).
y*(1+cX1+dX2) = 1+aX1+aX2
or
y - 1 = a*X1 + a*X2 -c*y*x1 -d*y*x2
You can do it using no extra functions, than just some basic MATLAB.
So assume that X1, X2, and Y are all COLUMN vectors. We implement it in MATLAB as:
coefs = [X1, X2, -Y.*X1, -Y.*X2]\(Y-1);
a = coefs(1);
b = coefs(2);
c = coefs(3);
d = coefs(4);
Basically one line of code. No iterative routine needed. No starting values. Or, you can use these estimates as very good estimators of the coefficients for a nonlinear estimation.
3 Comments
John D'Errico
on 13 Aug 2016
(TY)
As others have said, you need to learn to use MATLAB then. I cannot teach you a complete course in basic MATLAB skills, especially when that is done so much more ably in other places. Read the getting started tutorials.
Star Strider
on 12 Aug 2016
This will work:
X1 = randi(9, 1, 10); % Create Data
X2 = randi(9, 1, 10); % Create Data
Y = randi(99, 10, 1); % Create Data
XY = [X1(:) X2(:)]; % Create Independent Variable Matrix
Y = Y(:); % Dependent Variable
% % % MAPPING: p(1) = a, p(2) = b, p(3) = c, p(4) = d,
f = @(p,x) (1+p(1).*x(:,1)+p(2).*x(:,2))./(1+p(3).*x(:,1)+p(4)*x(:,2));
SSECF = @(p) sum((Y-f(p,XY)).^2); % Sum-Squared-Error Cost Function
P = fminsearch(SSECF, [1; 1; 1; 1]); % Estimate Parameters
5 Comments
Star Strider
on 13 Aug 2016
I do not have the Curve Fitting Toolbox. (I have the Optimization and Statistics Toolboxes, so don’t need it for what I do.) I prefer to use fminsearch in my Answers because everyone has it.
My code will give you accurate coefficients for your model. It may be more robust that code using the gradient-descent algorithms.
Amos
on 12 Aug 2016
You could use lsqcurvefit for that, where prbably X1 and X2 need to be put together into a single array, and a,b,c,d also need to be put together into a parameter array p.
0 Comments
See Also
Categories
Find more on Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!