% Power utility function u(c)=c^(1-gamma)/(1-gamma) if gamma ~= 1; 
% u(c)=ln(c) if gamma=1. 

% Utility is only defined if c>=0 for gamma<1 and c>0 if gamma>1

% The risk aversion coefficient is pass on to the function by
% define it as a global variable. 

% Also see invpoweru.m

function u=poweru(c)

global gamma;

[m,n]=size(c);
u=zeros(m,n);

if (gamma<0)
   u=-inf*abs(c);
%   display('invalid relative risk aversion coefficient');
else
   for i=1:m
      for j=1:n
   		if (gamma<1 & c(i,j) >= 0) | (gamma>1 & c(i,j)>0)
      		u(i,j)=c(i,j)^(1-gamma)/(1-gamma);
   		else if (gamma==1 & c(i,j)>0)
         	u(i,j)=log(c(i,j));
   	   	else
      	   	u(i,j)=-inf;
%        	 	display('consumption must be nonnegative if RRA coefficient is less than 1 and positive if greater than and equal to 1')
				end
			end
		end
   end
end


   
