How can I perform nonlinear regression with two input variables and one dependent variable
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).
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
Ubong Abia
on 12 Aug 2016
Amos
on 12 Aug 2016
very elegant solution
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
Amos
on 12 Aug 2016
nice ;-)
Ubong Abia
on 12 Aug 2016
Star Strider
on 12 Aug 2016
@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
on 12 Aug 2016
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
0 votes
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.
Categories
Find more on Linear Regression 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!