Hi , I have a problem. The question is as follows:
Below you will find a Matlab script that simulates the customers' shopping model . In this model it is assumed there are two types of customers. We now want this model to be more general. Transform this script into a function that simulates this model for the case with n types of customers. Call this function convcustomers
C = [0.4 0.1; 0.6 0.95];
t = 50;
X = zeros(2,t+1);
X(:,1) = [200;100];
for j = 1:t
X(:,j+1) = C * X(:,j);
end
figure(1);
plot(1:1:t+1, X(1,:),'r-',1:1:t+1,X(2,:),'b-');
xlabel('Time');
ylabel('Customers');
title('Customer populations over time');
legend('One-time','Repeat');
This function should do the following few things:
It takes as inputs:
1.) the matrix C that characterises our system of dynamic equations (notice: the size of C implicitly determines n),
the length of the simulation t (i.e. the total number of periods for which we simulate the model), and
a vector of initial conditions for the system (call it initdistr)
2. It spits out the following output:
an n x (t+1) matrix containing the simulated values of the different types (it's called X in the script I provide for guidance)
The function also produces a graph of the simulated series, and the graph has a legend, with types numbered from 1 to n.
This is my code so far :
function [X, G ] = convcustomers( C, t, initdistr )
X(:,1) = initdistr;
for j = 1:t
X(:,j+1) = C * X(:,j);
end
figure(1);
G = plot(1:1:t+1, X(1,:) ,'r-',1:1:t+1, X(2,:) ,'b-');
xlabel('Time');
ylabel('Customers');
title('Customer populations over time');
legend('One-time','Repeat');
end
I am unsure how to produce a graph for this with a legend with types numbered 1 to n.
Thanks