0001 function [a, b, c, err, E, A, C] = vb_exponential_estimate(y,x,mode)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if nargin < 3, mode = 0; end;
0021
0022 Niter = 5000;
0023 errmin = 1e-10;
0024 ddmin = 1e-10;
0025
0026
0027 [a, b, c] = exponent_init(y,x);
0028 ex = exp(c*x);
0029
0030 yy = mean(y.^2);
0031
0032 ddmin = ddmin * yy;
0033
0034
0035 A = zeros(Niter,1);
0036 C = zeros(Niter,1);
0037 E = zeros(Niter,1);
0038
0039
0040 for n=1:Niter
0041
0042 dy = (a + b*ex - y);
0043
0044
0045 da = b*mean( dy.*ex.*(x.^2) );
0046 db = (b^2)*mean( (ex.*x).^2 );
0047 ddE = da + db;
0048
0049 if ddE < ddmin,
0050 ddE = db;
0051 fprintf('-')
0052 end;
0053
0054
0055 dE = b * mean( dy.*x.*ex );
0056
0057 c = c - dE/ddE;
0058
0059 ex = exp(c*x);
0060
0061
0062 [b,a] = slope_estimate(y,ex,mode);
0063
0064 A(n) = a;
0065 C(n) = c;
0066 E(n) = mean((y - a - b*ex).^2)/yy;
0067
0068 if n > 1 && abs(E(n-1) - E(n)) < errmin,
0069 break;
0070 end;
0071
0072 end
0073
0074 err = sum(abs(y - a - b*ex))/sum(abs(y));
0075
0076 if nargout < 5, return; end;
0077
0078 A = A(1:n);
0079 C = C(1:n);
0080 E = E(1:n);
0081
0082
0083
0084 return
0085
0086
0087
0088
0089
0090 function [a,b,c] = exponent_init(y,x)
0091
0092 ix = find(y > 0);
0093
0094
0095 xx = x(ix);
0096 ly = log(y(ix));
0097
0098 c = slope_estimate(ly, xx);
0099
0100 ex = exp(c*x);
0101
0102
0103 [b,a] = slope_estimate(y, ex, 1);
0104
0105 return
0106
0107
0108
0109 function [c, b] = slope_estimate(y,x,mode)
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 if nargin==2 || mode~=1
0124
0125 mx = mean(x);
0126 my = mean(y);
0127
0128
0129 dx = x - mx;
0130 dy = y - my;
0131 c = sum(dy.*dx)/sum(dx.^2);
0132 b = my - c * mx;
0133 else
0134 c = sum(y.*x)/sum(x.^2);
0135 b = 0;
0136 end
0137
0138 return