function z=modexptr(y)

%MODEXPTR Computes parameters a,b,c of modified exponential trend of
%	time series y. Modified exponencial trend is trend of form
%	y = c + a*b^t, where b>0, t = 1,...,n. Function returns vector
%	z, which is [a,b,c].

[m,n]=size(y);
if m == 1
	k = floor(n/3);
	y1 = sum(y(1:k));
	y2 = sum(y(k+1:2*k));
	y3 = sum(y(2*k+1:3*k));
	b = ((y3-y2)/(y2-y1))^(1/k);
	a = (b-1)*(y2-y1)/b/(b^k-1)^2;
	c = (y1-a*b*(b^k-1)/(b-1))/k;
	z = [a,b,c];
else
	k = floor(m/3);
	z = zeros(3,n);
	for l = 1:n
		y1 = sum(y(1:k,l));
		y2 = sum(y(k+1:2*k,l));
		y3 = sum(y(2*k+1:3*k,l));
		b = ((y3-y2)/(y2-y1))^(1/k);
		a = (b-1)*(y2-y1)/b/(b^k-1)^2;
		c = (y1-a*b*(b^k-1)/(b-1))/k;
		z(1,l) = a;
		z(2,l) = b;
		z(3,l) = c;
	end
end
