MATLAB Examples

# Fit Polynomials Using the Fit Function

This example shows how to use the fit function to fit polynomials to data. The steps fit and plot polynomial curves and a surface, specify fit options, return goodness of fit statistics, calculate predictions, and show confidence intervals.

The polynomial library model is an input argument to the fit and fittype functions. Specify the model type poly followed by the degree in x (up to 9), or x and y (up to 5). For example, you specify a quadratic curve with 'poly2' , or a cubic surface with 'poly33' .

## Create and Plot a Quadratic Polynomial Curve

Load some data and fit a quadratic polynomial. Specify a quadratic, or second-degree polynomial, with the string 'poly2' .

```load census; fitpoly2=fit(cdate,pop,'poly2') % Plot the fit with the plot method. plot(fitpoly2,cdate,pop) % Move the legend to the top left corner. legend('Location','NorthWest' ); ```
```fitpoly2 = Linear model Poly2: fitpoly2(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93) p3 = 2.113e+04 (1.964e+04, 2.262e+04) ```

## Create a Cubic Curve

Fit a cubic polynomial 'poly3'.

```fitpoly3=fit(cdate,pop,'poly3') plot(fitpoly3,cdate,pop) ```
```Warning: Equation is badly conditioned. Remove repeated data points or try centering and scaling. fitpoly3 = Linear model Poly3: fitpoly3(x) = p1*x^3 + p2*x^2 + p3*x + p4 Coefficients (with 95% confidence bounds): p1 = 3.855e-06 (-4.078e-06, 1.179e-05) p2 = -0.01532 (-0.06031, 0.02967) p3 = 17.78 (-67.2, 102.8) p4 = -4852 (-5.834e+04, 4.863e+04) ```

## Specify Fit Options

The cubic fit warns that the equation is badly conditioned, so you should try centering and scaling by specifying the 'Normalize' option. Fit the cubic polynomial with both center and scale and robust fitting options. Robust 'on' is a shortcut equivalent to 'Bisquare' , the default method for robust linear least-squares fitting method.

```fit3=fit(cdate, pop,'poly3','Normalize','on','Robust','on') plot(fit3,cdate,pop) ```
```fit3 = Linear model Poly3: fit3(x) = p1*x^3 + p2*x^2 + p3*x + p4 where x is normalized by mean 1890 and std 62.05 Coefficients (with 95% confidence bounds): p1 = -0.4619 (-1.895, 0.9707) p2 = 25.01 (23.79, 26.22) p3 = 77.03 (74.37, 79.7) p4 = 62.81 (61.26, 64.37) ```

To find out what parameters you can set for the library model 'poly3' , use the fitoptions function.

```fitoptions poly3 ```
```ans = Normalize: 'off' Exclude: [] Weights: [] Method: 'LinearLeastSquares' Robust: 'Off' Lower: [1x0 double] Upper: [1x0 double] ```

## Get Goodness of Fit Statistics

Specify the 'gof' output argument to get the goodness-of-fit statistics for the cubic polynomial fit.

```[fit4, gof]=fit(cdate, pop,'poly3','Normalize','on'); gof ```
```gof = struct with fields: sse: 149.7687 rsquare: 0.9988 dfe: 17 adjrsquare: 0.9986 rmse: 2.9682 ```

## Plot the Residuals to Evaluate the Fit

To plot residuals, specify 'residuals' as the plot type in the plot method.

```plot(fit4,cdate, pop,'residuals'); ```

## Examine a Fit Beyond the Data Range

By default, the fit is plotted over the range of the data. To plot a fit over a different range, set the x-limits of the axes before plotting the fit. For example, to see values extrapolated from the fit, set the upper x-limit to 2050.

```plot( cdate, pop, 'o' ); xlim( [1900, 2050] ); hold on plot( fit4 ); hold off ```

## Plot Prediction Bounds

To plot prediction bounds, use 'predobs' or 'predfun' as the plot type.

```plot(fit4,cdate,pop,'predobs') ```

Plot prediction bounds for the cubic polynomial up to year 2050.

```plot( cdate, pop, 'o' ); xlim( [1900, 2050] ) hold on plot( fit4, 'predobs' ); hold off ```

## Get Confidence Bounds at New Query Points

Evaluate the fit for some new query points.

```cdateFuture = (2000:10:2020).'; popFuture = fit4( cdateFuture ) ```
```popFuture = 276.9632 305.4420 335.5066 ```

Compute 95% confidence bounds on the prediction for the population in the future, using the predint method.

```ci = predint( fit4, cdateFuture, 0.95, 'observation' ) ```
```ci = 267.8589 286.0674 294.3070 316.5770 321.5924 349.4208 ```

Plot the predicted future population, with confidence intervals, against the fit and data.

```plot(cdate, pop, 'o'); xlim([1900, 2040]) hold on plot(fit4) h = errorbar(cdateFuture,popFuture,popFuture-ci(:,1),ci(:,2)-popFuture,'.'); hold off legend('cdate v pop','poly3','prediction','Location','NorthWest') ```

## Fit and Plot a Polynomial Surface

Load some surface data and fit a fourth-degree polynomial in x and y.

```load franke; fitsurface=fit([x,y],z, 'poly44','Normalize','on') plot(fitsurface, [x,y],z) ```
``` Linear model Poly44: fitsurface(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2 + p13*x*y^3 + p04*y^4 where x is normalized by mean 1982 and std 868.6 and where y is normalized by mean 0.4972 and std 0.2897 Coefficients (with 95% confidence bounds): p00 = 0.3471 (0.3033, 0.3909) p10 = -0.1502 (-0.1935, -0.107) p01 = -0.4203 (-0.4637, -0.377) p20 = 0.2165 (0.1514, 0.2815) p11 = 0.1717 (0.1175, 0.2259) p02 = 0.03189 (-0.03351, 0.09729) p30 = 0.02778 (0.00749, 0.04806) p21 = 0.01501 (-0.002807, 0.03283) p12 = -0.03659 (-0.05439, -0.01879) p03 = 0.1184 (0.09812, 0.1387) p40 = -0.07661 (-0.09984, -0.05338) p31 = -0.02487 (-0.04512, -0.004624) p22 = 0.0007464 (-0.01948, 0.02098) p13 = -0.02962 (-0.04987, -0.009366) p04 = -0.02399 (-0.0474, -0.0005797) ```