This is a static copy of a profile reportHome
polyfit (202 calls, 0.431 sec)
Generated 05-Nov-2014 07:52:57 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/polyfun/polyfit.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Function Name | Function Type | Calls |
preprocess | function | 202 |
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
72 | elseif warnIfLargeConditionNum... | 202 | 0.310 s | 72.1% |  |
68 | p = R\(Q'*y); % Same as p =... | 202 | 0.030 s | 7.0% |  |
67 | ws = warning('off','all'); | 202 | 0.030 s | 7.0% |  |
69 | warning(ws); | 202 | 0.020 s | 4.7% |  |
66 | [Q,R] = qr(V,0); | 202 | 0.010 s | 2.3% |  |
All other lines | | | 0.030 s | 7.0% |  |
Totals | | | 0.431 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
polyfit>warnIfLargeConditionNumber | subfunction | 202 | 0.300 s | 69.8% |  |
Self time (built-ins, overhead, etc.) | | | 0.130 s | 30.2% |  |
Totals | | | 0.431 s | 100% | |
Code Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 90 |
Non-code lines (comments, blank lines) | 57 |
Code lines (lines that can run) | 33 |
Code lines that did run | 16 |
Code lines that did not run | 17 |
Coverage (did run/can run) | 48.48 % |
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-2011 The MathWorks, Inc.
30 % $Revision: 5.17.4.14 $ $Date: 2011/05/17 02:32:30 $
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
202 47 if ~isequal(size(x),size(y))
48 error(message('MATLAB:polyfit:XYSizeMismatch'))
49 end
50
202 51 x = x(:);
202 52 y = y(:);
53
0.01 202 54 if nargout > 2
55 mu = [mean(x); std(x)];
56 x = (x - mu(1))/mu(2);
57 end
58
59 % Construct Vandermonde matrix.
202 60 V(:,n+1) = ones(length(x),1,class(x));
202 61 for j = n:-1:1
0.01 202 62 V(:,j) = x.*V(:,j+1);
202 63 end
64
65 % Solve least squares problem.
0.01 202 66 [Q,R] = qr(V,0);
0.03 202 67 ws = warning('off','all');
0.03 202 68 p = R\(Q'*y); % Same as p = V\y;
0.02 202 69 warning(ws);
202 70 if size(R,2) > size(R,1)
71 warning(message('MATLAB:polyfit:PolyNotUnique'))
0.31 202 72 elseif warnIfLargeConditionNumber(R)
73 if nargout > 2
74 warning(message('MATLAB:polyfit:RepeatedPoints'));
75 else
76 warning(message('MATLAB:polyfit:RepeatedPointsOrRescale'));
77 end
78 end
79
202 80 if nargout > 1
81 r = y - V*p;
82 % S is a structure containing three elements: the triangular factor from a
83 % QR decomposition of the Vandermonde matrix, the degrees of freedom and
84 % the norm of the residuals.
85 S.R = R;
86 S.df = max(0,length(y) - (n+1));
87 S.normr = norm(r);
88 end
89
202 90 p = p.'; % Polynomial coefficients are row vectors by convention.
Other subfunctions in this file are not included in this listing.