main content

evaluate a curve fit -凯发k8网页登录

this example shows how to work with a curve fit.

load data and fit a polynomial curve

load census
curvefit = fit(cdate,pop,'poly3','normalize','on')
curvefit = 
     linear model poly3:
     curvefit(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.921  (-0.9743, 2.816)
       p2 =       25.18  (23.57, 26.79)
       p3 =       73.86  (70.33, 77.39)
       p4 =       61.74  (59.69, 63.8)

the output displays the fitted model equation, the fitted coefficients, and the confidence bounds for the fitted coefficients.

plot the fit, data, residuals, and prediction bounds

plot(curvefit,cdate,pop)

figure contains an axes object. the axes object with xlabel x, ylabel y contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent data, fitted curve.

plot the residuals fit.

plot(curvefit,cdate,pop,'residuals')

figure contains an axes object. the axes object with xlabel x contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent data, zero line.

plot the prediction bounds on the fit.

plot(curvefit,cdate,pop,'predfunc')

figure contains an axes object. the axes object with xlabel x, ylabel y contains 4 objects of type line. one or more of the lines displays its values using only markers these objects represent data, fitted curve, prediction bounds.

evaluate the fit at a specified point

evaluate the fit at a specific point by specifying a value for x , using this form: y = fittedmodel(x).

curvefit(1991)
ans = 252.6690

evaluate the fit values at many points

evaluate the model at a vector of values to extrapolate to the year 2050.

xi = (2000:10:2050).';
curvefit(xi)
ans = 6×1
  276.9632
  305.4420
  335.5066
  367.1802
  400.4859
  435.4468

get prediction bounds on those values.

ci = predint(curvefit,xi)
ci = 6×2
  267.8589  286.0674
  294.3070  316.5770
  321.5924  349.4208
  349.7275  384.6329
  378.7255  422.2462
  408.5919  462.3017

plot the fit and prediction intervals across the extrapolated fit range. by default, the fit is plotted over the range of the data. to see values extrapolated from the fit, set the upper x-limit of the axes to 2050 before plotting the fit. to plot prediction intervals, use predobs or predfun as the plot type.

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

figure contains an axes object. the axes object with xlabel x, ylabel y contains 4 objects of type line. one or more of the lines displays its values using only markers these objects represent fitted curve, prediction bounds.

get the model equation

enter the fit name to display the model equation, the fitted coefficients, and the confidence bounds for the fitted coefficients.

curvefit
curvefit = 
     linear model poly3:
     curvefit(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.921  (-0.9743, 2.816)
       p2 =       25.18  (23.57, 26.79)
       p3 =       73.86  (70.33, 77.39)
       p4 =       61.74  (59.69, 63.8)

to get only the model equation, use formula.

formula(curvefit)
ans = 
'p1*x^3   p2*x^2   p3*x   p4'

get coefficient names and values

specify a coefficient by name.

p1 = curvefit.p1
p1 = 0.9210
p2 = curvefit.p2
p2 = 25.1834

get all the coefficient names. look at the fit equation (for example, f(x) = p1*x^3 ... ) to see the model terms for each coefficient.

coeffnames(curvefit)
ans = 4x1 cell
    {'p1'}
    {'p2'}
    {'p3'}
    {'p4'}

get all the coefficient values.

coeffvalues(curvefit)
ans = 1×4
    0.9210   25.1834   73.8598   61.7444

get confidence bounds on the coefficients

use confidence bounds on coefficients to help you evaluate and compare fits. the confidence bounds on the coefficients determine their accuracy. bounds that are far apart indicate uncertainty. if the bounds cross zero for linear coefficients, this means you cannot be sure that these coefficients differ from zero. if some model terms have coefficients of zero, then they are not helping with the fit.

confint(curvefit)
ans = 2×4
   -0.9743   23.5736   70.3308   59.6907
    2.8163   26.7931   77.3888   63.7981

examine goodness-of-fit statistics

to get goodness-of-fit statistics at the command line, you can either:

  • open the curve fitter app. on the curve fitter tab, in the export section, click export and select export to workspace to export your fit and goodness of fit to the workspace.

  • specify the gof output argument using the fit function.

recreate the fit specifying the gof and output arguments to get goodness-of-fit statistics and fitting algorithm information.

[curvefit,gof,output] = fit(cdate,pop,'poly3','normalize','on')
curvefit = 
     linear model poly3:
     curvefit(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.921  (-0.9743, 2.816)
       p2 =       25.18  (23.57, 26.79)
       p3 =       73.86  (70.33, 77.39)
       p4 =       61.74  (59.69, 63.8)
gof = struct with fields:
           sse: 149.7687
       rsquare: 0.9988
           dfe: 17
    adjrsquare: 0.9986
          rmse: 2.9682
output = struct with fields:
        numobs: 21
      numparam: 4
     residuals: [21x1 double]
      jacobian: [21x4 double]
      exitflag: 1
     algorithm: 'qr factorization and solve'
    iterations: 1

plot a histogram of the residuals to look for a roughly normal distribution.

histogram(output.residuals,10)

figure contains an axes object. the axes object contains an object of type histogram.

plot the fit, data, and residuals

plot(curvefit,cdate,pop,'fit','residuals')
legend location southwest
subplot(2,1,1)
legend location northwest

figure contains 2 axes objects. axes object 1 with xlabel x, ylabel y contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent data, fitted curve. axes object 2 with xlabel x contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent data, zero line.

find methods

list every method that you can use with the fit.

methods(curvefit)
methods for class cfit:
argnames       category       cfit           coeffnames     coeffvalues    confint        dependnames    differentiate  feval          fitoptions     formula        indepnames     integrate      islinear       numargs        numcoeffs      plot           predint        probnames      probvalues     setoptions     type           

use the help command to find out how to use a fit method.

help cfit/differentiate
 differentiate  differentiate a fit result object.
    deriv1 = differentiate(fitobj,x) differentiates the model fitobj at the
    points specified by x and returns the result in deriv1. fitobj is a fit
    object generated by the fit or cfit function. x is a vector. deriv1 is
    a vector with the same size as x. mathematically speaking, deriv1 =
    d(fitobj)/d(x).
 
    [deriv1,deriv2] = differentiate(fitobj, x) computes the first and
    second derivatives, deriv1 and deriv2 respectively, of the model
    fitobj.
 
    see also cfit/integrate, fit, cfit.
网站地图