Clear Filters
Clear Filters

Problem With Graph In Function

1 view (last 30 days)
Karan Kular
Karan Kular on 1 Nov 2016
Edited: James Tursa on 1 Nov 2016
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
%%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');
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 )
%CONVCUSTOMERS This function calculates the ratio
% of one time to repeat customers in a n time period.
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

Accepted Answer

James Tursa
James Tursa on 1 Nov 2016
Edited: James Tursa on 1 Nov 2016
E.g., something like this to get you started
T = 1:1:t+1;
m = size(X,1);
figure(1);
G = plot(T',X');
legend([repmat('type',m,1) num2str((1:m)')]);

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!