Curve Fitting
Curve Fitting matches mathematical models of data to make accurate predictions.
Plotting a line of best fit can be performed with the least square fit or a robust fitting method.
- Robust methods detect outliers.
Polyfit
A least squares linear fit minimizes the square of the distance between every data point and the line of best fit.
polyfit(X,Y,N) finds the coefficients of the polynomial P(X) of degree N that fits the data. It uses the least-squares minimization N = 1 (linear fit).
[P] = polyfit(X,Y,N) returns P, a matrix containing the slope and the x-intercept for a linear fit.
[Y] = polyval(P,X) calculates the Y values for every X point on the line of best fit.
Example:
We have the data in var1 (x values) and var2 (y values). We find the fit with P = polyfit(var1, var2, 1). We plot it with Y = polyval(P,var1) and plot(var1, Y).
Higher order fitting can be done with polyfit. It uses the form:
it will return the value of a polynomial of n degree evaluated at x. The input argument p is a vector of length n+1. x can be a matrix or a vector. polyval evaluates p at each element of x.
Other fits
regress and robustfit are other fitting methods.
Errors
If our fit is a straight line: ,
Then the best line has the minimum error between the line and data points. This is called the least squares approach.
Take the derivative of error with respect to a and b and set each of them to zero and solve for a and b as a system of linear equations.
Machine Learning Overview
- you have data
- choose a assumption of model and make it fit the data that mimimizes approximate output and real output.
- solve for the data.
Toolbox: fitting interface
There is an interface that can quickly perform basic curve fitting tasks.
Linked from Computational Modeling