如何在 matlab 中编写递归函数,它基本上是一个马尔可夫链!我尝试为它编写一个伪代码,并且是MATLAB的新手:
函数是这样的:
P= Probability
x= status(0,1)
Dij= probability to pick a site
P(Status of Site(i) being x at next time step)= Summation[P(Status of Site(i) being x at previous time step)*Dij]
我已经尝试过代码,任何人都可以告诉我它是否正确:
function [t,index]= CopyingInfluenceModel
%%Define constants
StatusP1=rand(1,0);
StatusP0=rand(1,0);
% Initializing the variables
t=[];
index=[];
i=[];
%assigining the initial conditions
t(1)=0;
%set index no i to 1(initial condition for i=1)
i=1;
%% If the probability is zero, terminate while loop
while p(i)>=0
%calculate time step for given index no
t(i+1)= t(i);
%calculate the status_probability at given time t=(i+1)
StatusP1(i+1)=StatusP1(i)+sum(StatusP1(i)*Dij);
%index i increases by 1 to calculate next probability
i=i+1;
end
end