How do I do a Polynomial Fit to my data in Xess?
There are 2 Xess functions you will want to use to do this, polyfit and polycoef.
Take, for example, the following data:
A | B | C | D | E |
---|---|---|---|---|
1 | 1 | 1 | ||
2 | 2 | 2.1 | ||
3 | 3 | 2.9 | ||
4 | 4 | 4.1 | ||
5 | 5 | 4.9 | ||
6 | 6 | 6.2 | ||
7 | 7 | 6.9 |
To do a least squares fit, you will want to move the cursor to an adjacent vacant column (e.g. C1) and enter the following command,
@polyfit(A1..A7,B1..B7,1)
The first range is the "x" data and the second range is the "y" data. The last number indicates the degree of the polynomial (1 for linear, 2 for quadratic, etc.). The polyfit command will then generate the y values corresponding to the type of polynomial fit you desire.
To get the coefficients of the fit, go to another unoccupied cell (e.g. D1) and enter the following,
@polycoef(A1..A7,B1..B7,1)
Your spreadsheet now will look like,
A | B | C | D | E |
---|---|---|---|---|
1 | 1 | 1 | 1.025 | 0.99642 |
2 | 2 | 2.1 | 2.02142 | 0.02857 |
3 | 3 | 2.9 | 3.01785 | |
4 | 4 | 4.1 | 4.01428 | |
5 | 5 | 4.9 | 5.01071 | |
6 | 6 | 6.2 | 6.00714 | |
7 | 7 | 6.9 | 7.00357 |
Column C contains the y values of the fitted data points, and column D contains the coefficients of the fitted polynomial in descending order; in a linear fit, these correspond to the slope and intercept of the line.
To plot this, you would select "graph", then "New Graph", then the type of graph you want (in this case, "Line Graph"). Now you need to select your data ranges. Go to the graphics window and select "Edit" then "Data Sets".
Click in the X Range and enter the data range for the x data (you can also just highlight the data range and click on the middle mouse in the appropriate box and the range will appear), then click in the Y Range and enter the range for your y data. Then you want to select "Line Style" = "No
Line" so only data points are shown. Now, to display the fitted curve, go to "Edit", "Data Sets", then drag the drag bar to go to data set 2. Enter new X-range (same as the old X-range), and the Y-range corresponding to the fitted data (Column C). Select "Line Style" = "Solid", then "Apply". You should have a graph with data points and a fitted line through them. If you want to get a little more sophisticated and display the coefficients on the graph, goto "Options" in the Graph Editor, select "General" and click in the "Legend" box to enable legends. Then, "Edit", "Data Sets" and enter in the "Legend" text box,
0.996x+0.029
and "Apply".
Experiment to get the graph looking the way you want.