본문 바로가기

Enginius/Matlab

fitting (polyfit, fit)

1. polyfit -Polynomial curve fitting

Syntax

p = polyfit(x,y,n)
[p,S] = polyfit(x,y,n)
[p,S,mu] = polyfit(x,y,n)

Description

p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data, p(x(i)) to y(i), in a least squares sense. The result p is a row vector of length n+1 containing the polynomial coefficients in descending powers:

[p,S] = polyfit(x,y,n) returns the polynomial coefficients p and a structure S for use with polyval to obtain error estimates or predictions. Structure S contains fields Rdf, and normr, for the triangular factor from a QR decomposition of the Vandermonde matrix of x, the degrees of freedom, and the norm of the residuals, respectively. If the data y are random, an estimate of the covariance matrix of p is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R. If the errors in the data y are independent normal with constant variance, polyval produces error bounds that contain at least 50% of the predictions.

[p,S,mu] = polyfit(x,y,n) finds the coefficients of a polynomial in

where  and mu is the two-element vector [μ12]. This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.

Examples

This example involves fitting the error function, erf(x), by a polynomial in x. This is a risky project because erf(x) is a bounded function, while polynomials are unbounded, so the fit might not be very good.

First generate a vector of x points, equally spaced in the interval [0, 2.5]; then evaluate erf(x) at those points.

x = (0: 0.1: 2.5)';
y = erf(x); 

The coefficients in the approximating polynomial of degree 6 are

p = polyfit(x,y,6)

p =

 0.0084  -0.0983   0.4217   -0.7435  0.1471   1.1064  0.0004

There are seven coefficients and the polynomial is . To see how good the fit is, evaluate the polynomial at the data points with:

f = polyval(p,x);

A table showing the data, fit, and error is

 table = [x y f y-f]
 
 table =

    0          0          0.0004    -0.0004
    0.1000     0.1125     0.1119     0.0006
    0.2000     0.2227     0.2223     0.0004
    0.3000     0.3286     0.3287    -0.0001
    0.4000     0.4284     0.4288    -0.0004
    ...
    2.1000     0.9970     0.9969     0.0001
    2.2000     0.9981     0.9982    -0.0001
    2.3000     0.9989     0.9991    -0.0003
    2.4000     0.9993     0.9995    -0.0002
    2.5000     0.9996     0.9994     0.0002

So, on this interval, the fit is good to between three and four digits. Beyond this interval the graph shows that the polynomial behavior takes over and the approximation quickly deteriorates.

x = (0: 0.1: 5)';
y = erf(x);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
axis([0  5  0  2])

Algorithms

The polyfit MATLAB file forms the Vandermonde matrix, V, whose elements are powers of x

It then uses the backslash operator, \, to solve the least squares problem Vpy.

You can modify the MATLAB file to use other functions of x as the basis functions.



2. fit -Fit curve or surface to data

Syntax

fitobject = fit(x,y,fitType)
fitobject = fit([x,y],z, fitType)
fitobject = fit(..., Name, Value,...)
fitobject = fit(x,y,libname,options)
fitobject = fit(...,'problem',vals)
fitobject = fit(x,y,fitType,...,'Weight', Weights)
[fitobject,gof] = fit(...)
[cfun,gof,output] = fit(...)

Description

fitobject = fit(x,y,fitType) fits the data in x and y with the library model, anonymous function or fittype object specified by fitType.

  • x must be a matrix with either one (curve fitting) or two (surface fitting) columns. For surface fitting, if your data is in separate vectors, then you can use the syntax: fitobject = fit([x,y],z, fitType).

  • y must be a column vector with the same number of rows as x.

  • x and y cannot contain Inf or NaN. Only the real parts of complex data are used in the fit.

  • fitType can be a string, anonymous function, or a fittype object specifying the model to fit. If a string, you can specify library model names. String choices include:

    LIBNAME           DESCRIPTION
    'poly1'           Linear polynomial curve
    'poly11'          Linear polynomial surface
    'poly2'           Quadratic polynomial curve
    'linearinterp'    Piecewise linear interpolation
    'cubicinterp'     Piecewise cubic interpolation
    'smoothingspline' Smoothing spline (curve)
    'lowess'          Local linear regression (surface)
    

    or any of the names of library models described in cflibhelp. Type cflibhelp to display names and descriptions of all library models.

    To fit custom models, use an anonymous function or create a fittype with the fittype function and use this as the fitTypeargument.

  • fitobject is the fit result, a cfit (for curves) or sfit (for surfaces) object. See Fit Postprocessing for functions for plotting, evaluating, calculating confidence intervals, integrating, differentiating, or modifying your fit object.

fitobject = fit(..., Name, Value,...) fits the data using the problem and algorithm options specified in the name-value pair arguments. You can display the supported property names and default values for specific library models with the fitoptionsfunction. For example:

fitoptions( 'cubicinterp' )
fitoptions( 'poly1' )

fitobject = fit(x,y,libname,options) fits the data using the algorithm options specified by the fitoptions object options. This is an alternative syntax to specifying the property-value pairs. For help on constructing options, see the fitoptionsfunction.

fitobject = fit(...,'problem',vals) assigns vals to the problem-dependent constants. vals is a cell array with one element per problem dependent constant. See fittype for more information on problem dependent constants.

fitobject = fit(x,y,fitType,...,'Weight', Weights) creates a weighted fit using the given WeightsWeights must be a vector the same size as y.

[fitobject,gof] = fit(...) returns goodness-of-fit statistics to the structure gof. The gof structure includes the fields shown in the table below.

Field

Value

sse

Sum of squares due to error

R2

Coefficient of determination

adjustedR2

Degree-of-freedom adjusted coefficient of determination

stdError

Root mean squared error (standard error)

[cfun,gof,output] = fit(...) returns the structure output, which contains information associated with the fitting algorithm. Fields depend on the algorithm. For example, the output structure for nonlinear least-squares algorithms has the fields shown in the table below.

Field

Value

numobs

Number of observations (response values)

numparam

Number of unknown parameters (coefficients) to fit

residuals

Vector of residuals

Jacobian

Jacobian matrix

exitflag

Describes the exit condition of the algorithm. Positive flags indicate convergence, within tolerances. Zero flags indicate that the maximum number of function evaluations or iterations was exceeded. Negative flags indicate that the algorithm did not converge to a solution.

iterations

Number of iterations

funcCount

Number of function evaluations

firstorderopt

Measure of first-order optimality (absolute maximum of gradient components)

algorithm

Fitting algorithm employed

Remarks on Starting Points

For rational and Weibull models, and all custom nonlinear models, the toolbox selects default initial values for coefficients uniformly at random from the interval (0,1).

As a result, multiple fits using the same data and model may lead to different fitted coefficients. To avoid this, specify initial values for coefficients with fitoptions structure or a vector value for the StartPoint property.

Examples

Fit a cubic interpolating spline through x and y:

[curve, goodness] = fit( x, y, 'pchipinterp' );

 

Fit a polynomial surface of degree 2 in x and degree 3 in y using the least absolute residual robust (LAR) method:

sf = fit( [x, y], z, 'poly23', 'Robust', 'LAR' );

 

Fit the 1st equation in the curve fitting library of exponential models (a single-term exponential), overriding the starting point to be p0:

curve = fit( x, y, 'exp1', 'StartPoint', p0 );
 

Load data and fit using an anonymous function:

  1. Load data and set Emax to 1 before defining your anonymous function:

    data = importdata( 'OpioidHypnoticSynergy.txt' );
    Propofol      = data.data(:,1);
    Remifentanil  = data.data(:,2);
    Algometry     = data.data(:,3);
    Emax = 1;
  2. Define the model equation as an anonymous function:

    Effect = @(IC50A, IC50B, alpha, n, x, y) ...
        Emax*( x/IC50A + y/IC50B + alpha*( x/IC50A )...
        .* ( y/IC50B ) ).^n ./(( x/IC50A + y/IC50B + ...
        alpha*( x/IC50A ) .* ( y/IC50B ) ).^n  + 1);
  3. Use the anonymous function Effect as an input to the fit function, and plot the results:

    AlgometryEffect = fit( [Propofol, Remifentanil], Algometry, Effect, ...
        'StartPoint', [2, 10, 1, 0.8], ...
        'Lower', [-Inf, -Inf, -5, -Inf], ...
        'Robust', 'LAR' )
    plot( AlgometryEffect, [Propofol, Remifentanil], Algometry )

    See Custom Nonlinear Surface Fitting Examples for more information on this example.

 

Load and plot data, create fit options and fit type, then create and plot fit:

  1. Load and plot the data in census.mat:

    load census
    plot(cdate,pop,'o')
    hold on
    

  2. Create a fit options structure and a fittype object for the custom nonlinear model y = a(xb)n, where a and b are coefficients and n is a problem-dependent parameter:

    s = fitoptions('Method','NonlinearLeastSquares',...
                   'Lower',[0,0],...
                   'Upper',[Inf,max(cdate)],...
                   'Startpoint',[1 1]);
    f = fittype('a*(x-b)^n','problem','n','options',s);
  3. Fit the data using the fit options and a value of n = 2:

    [c2,gof2] = fit(cdate,pop,f,'problem',2)
    c2 =
         General model:
           c2(x) = a*(x-b)^n
         Coefficients (with 95% confidence bounds):
           a =    0.006092  (0.005743, 0.006441)
           b =        1789  (1784, 1793)
         Problem parameters:
           n =           2
    gof2 = 
               sse: 246.1543
           rsquare: 0.9980
               dfe: 19
        adjrsquare: 0.9979
              rmse: 3.5994
  4. Fit the data using the fit options and a value of n = 3:

    [c3,gof3] = fit(cdate,pop,f,'problem',3)
    c3 =
         General model:
           c3(x) = a*(x-b)^n
         Coefficients (with 95% confidence bounds):
           a =  1.359e-005  (1.245e-005, 1.474e-005)
           b =        1725  (1718, 1731)
         Problem parameters:
           n =           3
    gof3 = 
               sse: 232.0058
           rsquare: 0.9981
               dfe: 19
        adjrsquare: 0.9980
              rmse: 3.4944
  5. Plot the fit results with the data:

    plot(c2,'m')
    plot(c3,'c')

See Also

cflibhelp | confint | feval | fitoptions | fittype | plot

 

'Enginius > Matlab' 카테고리의 다른 글

mldivide \, mrdivide / (Matrix division)  (0) 2011.10.26
hist (Histogram)  (0) 2011.10.26
subplot  (0) 2011.10.25
clear all / close all / clc / clf  (2) 2011.10.25
for loop  (0) 2011.10.25