%happytoy.m
clear all
%- - set up parameters and variables
time=50;
happiness(1)=0;
experience=zeros(1,time);
comparitor=zeros(1,time);
% - - define 'experience'
%experience is a bunch of random good events
experience=rand(1,100);
%- - this loop calculates the happiness at each time point
for t=2:time
%calculate the comparitor, based on previous experiences
if t>10
comparitor(t)=sum(experience(t-10:t-1).*(1:10))/sum(1:10);
else
comparitor(t)=sum(experience(1:t-1).*(1:t-1))/sum(1:t-1);
end
%calculate happiness
happiness(t)=experience(t)-comparitor(t);
end
%display the total aggregate happiness to the screen
say=['Total happiness = ' num2str(sum(happiness))];disp(say)
%draw a graph of the time series
figure(4);hold off
plot(happiness,'b-')
hold on
plot(experience,'k.')
plot(comparitor,'r:')
xlabel('time')
axis([0 time+1 min(happiness)*1.1 max(happiness)*1.1])