How can I perform nonlinear regression with two input variables and one dependent variable
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (3개)
John D'Errico
2016년 8월 12일
편집: John D'Errico
2016년 8월 12일
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
John D'Errico
2016년 8월 13일
(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
2016년 8월 12일
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
Star Strider
2016년 8월 13일
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
2016년 8월 12일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!