How can I perform nonlinear regression with two input variables and one dependent variable
古いコメントを表示
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).
回答 (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 件のコメント
Ubong Abia
2016 年 8 月 12 日
Amos
2016 年 8 月 12 日
very elegant solution
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 件のコメント
Amos
2016 年 8 月 12 日
nice ;-)
Ubong Abia
2016 年 8 月 12 日
Star Strider
2016 年 8 月 12 日
@Amos — Thank you!
-----------------
@Ubong Abia — My pleasure.
First read Getting Started to understand MATLAB, how it works, and how to make it work for you. Please take your time reading it, run the examples, and get comfortable with it. Be sure you understand how to create, save, and run script files (using the .m extension). Don’t worry about the strange looking code I wrote (it uses anonymous functions), since there will be time for you to learn about Function Basics later when you’re more comfortable with MATLAB. (I linked to it here to make it easier for you to find and read.) MATLAB is a very large environment, so make extensive use of the documentation. I refer to it frequently. To bring that up, type doc in the Command Window.
Second, to read an Excel file, use the xlsread function. See the documentation on xlsread to understand how it works.
Third, with respect to the full MATLAB code, I don’t have your Excel file, so I can’t write specific code. Assuming the first two columns are ‘X1’ and ‘X2’ and the third is ‘Y’, the actual code would go something like this:
[d,s,r] = xlsread('Your_Data_File.xlsx');
XY = d(:,1:2);
Y = d(:,3);
% % % 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
I had xlsread return all three outputs here because you may need that other information, and having it in your workspace makes it easier for you to access it.
I’m not certain what you want to do, so I didn’t go further than importing the data and estimating the parameters of your function.
This should get you started.
Ubong Abia
2016 年 8 月 12 日
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 日
0 投票
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.
カテゴリ
ヘルプ センター および File Exchange で Linear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!