Jan 18, 2012

How to Graph Least Squares on MATLAB

,
A “least squares” line is used in scatter plots and regression analyses to indicate the “best fit” between two sets of data. It helps researchers to uncover trends in data visually in addition to the other analyses they may perform. In MATLAB, the least squares line can be added to any scatter plot by first defining the plot and then using the “lsline” command directly afterwards. To perform this operation you need a file containing data ready to plot on a scatter graph.


Instructions

    • 1
      Load up MATLAB and open the file containing your data.
    • 2
      Type the following line of code into MATLAB:
      scatter(X,Y,S,C)
      Replace “X” with the name of the vector containing your first group of data. Replace “Y” with the name of the vector containing your second group of data. Note that both groups of data must be equally sized – if they are not, remove any data points that do not have a corresponding figure in the other data set. Replace “S” with the size you want the markers in the graph to be, for example “5”. If you are not happy with how the graph looks after running the program, you can simply edit the figure you supplied for “S” and run the program again until you like the results. Replace “C” with the colors you want the markers to be. If you replace “C” with the name of a one-by-three matrix with integer values between 0 and 255, the color of the markers will be set to a red, green and blue combination specified by these values. Go to allprofitallfree.com/color-wheel2.html to find the color you want using the color wheel.
    • 3
      Add “lsline” (without quotations) to the program on a new line. This is the command that will graph the least squares line for you.
    • 4
      Run the program and wait a moment for MATLAB to draw your graph. It will appear as a scatter plot, with marker points of the size and color you specified, and a least squares line running through the points showing you the relationship between the two groups of data.

0 commentaires to “How to Graph Least Squares on MATLAB”

Post a Comment

 

HowToYo Copyright © 2011 | Template design by O Pregador | Powered by Blogger Templates