필터 지우기
필터 지우기

Problem With Graph In Function

조회 수: 1 (최근 30일)
Karan Kular
Karan Kular 2016년 11월 1일
편집: James Tursa 2016년 11월 1일
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

채택된 답변

James Tursa
James Tursa 2016년 11월 1일
편집: James Tursa 2016년 11월 1일
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)')]);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by