Recursive Function in matlab

喜你入骨 提交于 2020-01-14 14:01:26

问题


How do I write a recursive Function in matlab, it basically being a Markov chain! I tried writing a pseudo code for it and new to MATLAB:

The function goes this way:

   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]

I have tried code, can anyone tel me whether its right:

 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

回答1:


First the recursive "Hello World" factorial function in matlab:

function result=f(x)
if (x<=0)
    result=1;
else
    result=x*f(x-1);
end

end

It can be called like this:

>> f(4)
ans =
    24

Not knowing much about Markov chains, I am making a guess here at what your function is supposed to do. First the code:

function yourmainscript
    % state 1 -> state 1: 50%
    % state 1 -> state 2: 50%
    % state 2 -> state 1: 25%
    % state 2 -> state 2: 75%
    D=[0.5 0.5; 0.25 0.75];
    p0=[1; 0];

    % Get probability that the system is in state number 1 at time step number 4
    % given the transition matrix D and the initial probabilities p0
    P(1,4,D,p0)
end

function result=P(state, t, D, p0)
if t==0
    result=p0(state);
else
    possible_states=(1:length(D))';
    result=D(state,:)*arrayfun(@(x) P(x, t-1, D, p0), possible_states);
end
end

The parameters D and p0 describe the system and are just passed through unmodified. Using global variables or playing with function nesting will work for them, too, as long as they are accessible.

state is an integer between 1 and the total number of states that you deal with, t is an integer that represents the time step.

At t==0 we can use state as an index into p0 to get the probability. For t>0 I have rewritten the sum as a matrix multiplication:

We need the row of Dij (which is D(state,:) given by the current state and multiply it with the vector of the probabilities of all possible states at the last time step.

The line

possible_states=(1:length(D))';

is a column vector containing 1,2,3,...,last state and is needed in the next line. Arrayfun calls a function (first argument) for each element of an array (second argument) and stuffs the result into a vector. The first argument is a shorthand for defining the following function:

function result=wrapperfunction(x)
    % Assume t, D and p0 are in scope and the function P is known
    result=P(x, t-1, D, p0);
end

Please note that matlab is case sensitive, so if you define a function "Markov" then matlab still doesn't now about "markov".

Edit: Sorry, you have updated your code while I was composing this answer, so it may or may not apply to the updated version.



来源:https://stackoverflow.com/questions/13311289/recursive-function-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!