This is a static copy of a profile reportHome
polyfit (240 calls, 0.219 sec)
Generated 05-Aug-2011 13:03:51 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/polyfun/polyfit.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
72 | elseif warnIfLargeConditionNum... | 240 | 0.164 s | 75.0% |  |
83 | if nargout > 1 | 240 | 0.011 s | 5.0% |  |
68 | p = R\(Q'*y); % Same as p =... | 240 | 0.011 s | 5.0% |  |
67 | ws = warning('off','all'); | 240 | 0.011 s | 5.0% |  |
51 | x = x(:); | 240 | 0.011 s | 5.0% |  |
All other lines | | | 0.011 s | 5.0% |  |
Totals | | | 0.219 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
polyfit>warnIfLargeConditionNumber | subfunction | 240 | 0.164 s | 75.0% |  |
Self time (built-ins, overhead, etc.) | | | 0.055 s | 25.0% |  |
Totals | | | 0.219 s | 100% | |
Code Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 93 |
Non-code lines (comments, blank lines) | 60 |
Code lines (lines that can run) | 33 |
Code lines that did run | 14 |
Code lines that did not run | 19 |
Coverage (did run/can run) | 42.42 % |
Function listing
time calls line
1 function [p,S,mu] = polyfit(x,y,n)
2 %POLYFIT Fit polynomial to data.
3 % P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
4 % degree N that fits the data Y best in a least-squares sense. P is a
5 % row vector of length N+1 containing the polynomial coefficients in
6 % descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
7 %
8 % [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
9 % structure S for use with POLYVAL to obtain error estimates for
10 % predictions. S contains fields for the triangular factor (R) from a QR
11 % decomposition of the Vandermonde matrix of X, the degrees of freedom
12 % (df), and the norm of the residuals (normr). If the data Y are random,
13 % an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df,
14 % where Rinv is the inverse of R.
15 %
16 % [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in
17 % XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This
18 % centering and scaling transformation improves the numerical properties
19 % of both the polynomial and the fitting algorithm.
20 %
21 % Warning messages result if N is >= length(X), if X has repeated, or
22 % nearly repeated, points, or if X might need centering and scaling.
23 %
24 % Class support for inputs X,Y:
25 % float: double, single
26 %
27 % See also POLY, POLYVAL, ROOTS, LSCOV.
28
29 % Copyright 1984-2010 The MathWorks, Inc.
30 % $Revision: 5.17.4.13 $ $Date: 2010/11/17 11:29:32 $
31
32 % The regression problem is formulated in matrix format as:
33 %
34 % y = V*p or
35 %
36 % 3 2
37 % y = [x x x 1] [p3
38 % p2
39 % p1
40 % p0]
41 %
42 % where the vector p contains the coefficients to be found. For a
43 % 7th order polynomial, matrix V would be:
44 %
45 % V = [x.^7 x.^6 x.^5 x.^4 x.^3 x.^2 x ones(size(x))];
46
240 47 if ~isequal(size(x),size(y))
48 error(message('MATLAB:polyfit:XYSizeMismatch'))
49 end
50
0.01 240 51 x = x(:);
240 52 y = y(:);
53
240 54 if nargout > 2
55 mu = [mean(x); std(x)];
56 x = (x - mu(1))/mu(2);
57 end
58
59 % Construct Vandermonde matrix.
240 60 V(:,n+1) = ones(length(x),1,class(x));
240 61 for j = n:-1:1
62 V(:,j) = x.*V(:,j+1);
63 end
64
65 % Solve least squares problem.
240 66 [Q,R] = qr(V,0);
0.01 240 67 ws = warning('off','all');
0.01 240 68 p = R\(Q'*y); % Same as p = V\y;
240 69 warning(ws);
240 70 if size(R,2) > size(R,1)
71 warning(message('MATLAB:polyfit:PolyNotUnique'))
0.16 240 72 elseif warnIfLargeConditionNumber(R)
73 if nargout > 2
74 warning(message('MATLAB:polyfit:RepeatedPoints'));
75 else
76 warning('MATLAB:polyfit:RepeatedPointsOrRescale', ...
77 ['Polynomial is badly conditioned. Add points with distinct X\n' ...
78 ' values, reduce the degree of the polynomial, or try centering\n' ...
79 ' and scaling as described in HELP POLYFIT.']);
80 end
81 end
82
0.01 240 83 if nargout > 1
84 r = y - V*p;
85 % S is a structure containing three elements: the triangular factor from a
86 % QR decomposition of the Vandermonde matrix, the degrees of freedom and
87 % the norm of the residuals.
88 S.R = R;
89 S.df = max(0,length(y) - (n+1));
90 S.normr = norm(r);
91 end
92
240 93 p = p.'; % Polynomial coefficients are row vectors by convention.
Other subfunctions in this file are not included in this listing.