Curve Fitting

Created

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: y=p1xn+p2xn−1+...+Pnx+Pn+1y = p_{1}x^{n}+p_2x^{n-1} + ...+ P_{n}x + P_{n+1} 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: f(x)=ax+bf(x) = ax + b, Error=∑i=1N=[yi−f(xi)]2Error = \sum_{i=1}^{N}=[y_i - f(x_i)]^2 Error=∑i=1N=[yi−(axi+b)]2Error = \sum_{i=1}^{N}=[y_i - (ax_i + b)]^2

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

  1. you have data
  2. choose a assumption of model and make it fit the data that mimimizes approximate output and real output.
  3. solve for the data.

Toolbox: fitting interface

There is an interface that can quickly perform basic curve fitting tasks.