
How to use polyfit command to find the linear approximation of the data
25 views (last 30 days)
Show older comments
Measurment course
0 Comments
Answers (2)
Star Strider
on 30 Sep 2017
A linear approximation is a first-degree polynomial, so specify it as:
p = polyfit(x, y, 1);
3 Comments
Star Strider
on 30 Sep 2017
You will need to choose the vectors in your data that correspond to the independent, x, and dependent, y variables.
To fit them to your data, use the code I provided to get the coefficient vector, p, of the linear fit. To create a line to plot, use the p vector in polyval with your independent variable:
y_fit = polyval(p, x);
then:
plot(x, y, '+')
hold on
plot(x, y_fit, '-r')
hold off
grid
I am not certain if you are referring to ‘table’ as a matrix or as a table data type. That is an important distinction. See the documentation on Matrix Indexing (link) and table (link) for addressing, and a discussion of the different data types.
Image Analyst
on 30 Sep 2017
Let's say your table is called t and you want the first column to be x and the second column to be y. So you'd do
x = t{:, 1}; % Use braces
y = t{:, 2};
If t were a regular numerical matrix, you'd use parentheses instead of braces:
x = t(:, 1); % Use parentheses.
y = t(:, 2);
See Also
Categories
Find more on Tables 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!