# How to transform the script into a function that simulates the customers' shopping model for the case with n types of customers.

1 view (last 30 days)
Rafaela Stylianou on 7 Nov 2016
Edited: Rafaela Stylianou on 7 Nov 2016
Functions should be called convcustomers. The function should do the following few things: take as inputs: the matrix C that characterises our system of dynamic equations the length of the simulation t 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) 3. The function also produces a graph of the simulated series, and the graph has a legend, with types numbered from 1 to n.
i have the following script :
%% define our customer transition matrix C = [0.4 0.1; 0.6 0.95];
%% set our maximum time frame t = 50;
%% we'll store the populations as the columns of the matrix X X = zeros(2,t+1);
%% set the initial populations % remember X(:,1) means 'column 1, all rows of matrix X' X(:,1) = [200;100];
%% iterate over time
for j = 1:t
X(:,j+1) = C * X(:,j);
end
%% plot the results
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');
And my solution so far is :
function[x]=convcustomers(C,t,initdistr)
initdistr=(X:,1)
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')