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!