3.0

3.0 | 2 ratings Rate this file 221 downloads (last 30 days) File Size: 3.43 KB File ID: #22865

General Least Squares Regression

by Husam Aldahiyat

 

02 Feb 2009 (Updated 09 Sep 2009)

Code covered by BSD License  

Multi Dimensional Multivariable Least Squares Regression

Download Now | Watch this File

File Information
Description

The code manually derives the expression fm(x1,x2,x3,...,xk) for
the equally spaced points in (k+1) dimensions, where m is the order of the approximation f.

Example:
  
x1=[6 7 8 5 3 2 1 2 5 8];
x2=[9 7 0 8 5 6 4 3 2 10];
y=[9 7 6 4 3 2 5 6 8 1];
  
mreg([x1;x2],y,1)
ans =
  5.542073-.2829251*x2+.2310048*x1
   
mreg([x1;x2],y,2)
ans =
  3.797949-.6754073*x2-.2393447e-1*x2^2
+2.256645*x1+.8248780e-1*x1*x2
-.2478672*x1^2
  
mreg([x1;x2],y,3)
ans =
-24.62814+33.15517*x2-9.837215*x2^2+.8316257*x2^3
-17.31119*x1+7.450905*x1*x2-.6438069*x1*x2^2
+.3614982*x1^2-.4148167*x1^2*x2+.2851207*x1^3
  
Example:
  
x1=[6 7 8 5 3 2 1 2 5 8 5 4];
x2=[9 7 0 8 5 6 4 3 2 10 5 7];
x3=[8 9 7 6 5 4 3 5 6 7 5 3];
y=[9 7 6 4 3 2 5 6 8 1 8 0];
  
mreg([x1;x2;x3],y,2)
ans =
32.48905-7.049319*x3+.1384845*x3^2-6.246954*x2
+1.259510*x2*x3-.4498889e-1*x2^2+5.668323*x1
-.4038119*x1*x3-.3257156*x1*x2-.1031725*x1^2
 

Required Products Symbolic Math Toolbox
MATLAB release MATLAB 7.4 (R2007a)
Other requirements A MATLAB version with the Maple Symbolic Math Toolbox, i.e. MATLAB versions EARLIER than 7.6 (R2008a).
Zip File Content  
Other Files license.txt,
mreg.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
19 Feb 2009 Mehmet Demirel  
13 Mar 2009 Husam Aldahiyat

If you liked this submission please rate it! :)

13 Mar 2009 John D'Errico

This looks like it would be nice, though I do not have the symbolic TB to put it through its paces, so I will not offer a rating. It is an alternative to my own polyfitn, with output directly into a sym form.

A couple of comments. (Surely you would not expect me to have nothing to say.) Good help. I like to see verbose help, even when some others have been known to say my help is too verbose. I want all of the questions answered in my help, and this help made an effort. I like the examples. Error checks, and an H1 line. All good.

The code itself looks like it was carefully written, but as I said, I could not test it. I had a couple of style points though. I'd rarely recommend the use of variable names like "kkksdgsdg". When you need to debug the code, reading it will be more difficult in a year or so because of a randomly chosen string of characters. I know, sometimes it is difficult to come up with a mnemonic name, but you can try harder than that.

Also, use internal comments. This code has virtually none, except for a credit to Roger Stafford. (Well done there. It is always good to give credit where it is due.) Those internal comments make your code readable. they allow a reader to follow your code and your thoughts as you wrote it. Those comments take less time than you think to write, but they are worth their weight in gold when you need to read even your own code next year.

Finally, I'm not sure I like this style of error message:

error('MSG:ID3','lengths of martices x and y need to be the same')

If you will use a message identifier, why not provide a descriptive name instead of MSG:ID3? This reminds me of the old days writing IBM Fortran code, where each programmer learned the meaning of cryptic error messages, identified only by a few characters and a 3 digit number. We then had a manual that offered an even more cryptic explanation of that error code, if you wanted to dig for it.

09 Jun 2009 Michal Kvasnicka

at STRB2 function is probably infinite while loop problem

22 Jun 2009 Husam Aldahiyat

You're right, this is due to the new MUPAD engine. On MATLAB versions earlier than the MuPad era the code should work fine. I will try to fix this problem as soon as possible.

Please login to add a comment or rating.
Updates
09 Sep 2009

Updated requirements.

Tag Activity for this File
Tag Applied By Date/Time
least Husam Aldahiyat 02 Feb 2009 16:12:36
squares Husam Aldahiyat 02 Feb 2009 16:12:36
regression Husam Aldahiyat 02 Feb 2009 16:12:36
generalized Husam Aldahiyat 02 Feb 2009 16:12:36
polynomial Husam Aldahiyat 03 Feb 2009 01:54:53
multidimensional Husam Aldahiyat 03 Feb 2009 08:44:38
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com