0

I have this 3x3 system of equations for 2-D markov chain. I want to map the coefficients of this matrix to look like this 9x9 matrix having those zeros coefficients as well but i don't know how to proceed with it. Any help ?

[ 5*P11 - 2*P12 - 2*P21, 7*P12 - 5*P11 - 4*P13 - 2*P22,          9*P13 - 5*P12 - 2*P23]
[ 7*P21 - 2*P22 - 4*P31, 9*P22 - 5*P21 - 4*P23 - 4*P32, 11*P23 - 5*P22 - 5*P13 - 4*P33]
[         9*P31 - 2*P32,        11*P32 - 5*P31 - 4*P33,          8*P33 - 5*P32 - 5*P23]

Coefficient Matrix

Daniel
  • 36,610
  • 3
  • 36
  • 69
A Ahmed
  • 7
  • 6
  • Pxx are symbolic variables? – Daniel Feb 22 '16 at 14:44
  • @Daniel It says undefined function 1P11'. I created my symbolic variables like this. n=2; jmax=n+1; P = sym(zeros((n+1),(n+1))); for j1=1:jmax for j2=1:jmax P(j1,j2) = sym(sprintf('P%d%d', j1,j2)); end end – A Ahmed Feb 22 '16 at 15:33
  • I noticed my comment was incomplete and deleted it before you answered. Currently writing an answer. – Daniel Feb 22 '16 at 15:35

1 Answers1

1

You can use coeffs to get the coefficients.

syms P11 P12 P13 P21 P22 P23 P31 P32 P33
%your data
T=[ 5*P11 - 2*P12 - 2*P21, 7*P12 - 5*P11 - 4*P13 - 2*P22,          9*P13 - 5*P12 - 2*P23;...
    7*P21 - 2*P22 - 4*P31, 9*P22 - 5*P21 - 4*P23 - 4*P32, 11*P23 - 5*P22 - 5*P13 - 4*P33;...
    9*P31 - 2*P32,        11*P32 - 5*P31 - 4*P33,          8*P33 - 5*P32 - 5*P23];
%get a list of all variables. Optional, sort here if you expect another ordering.
allvars=symvar(T);

%initialize empty matix
C=zeros(numel(T),numel(allvars));
%build up coefficient matrix
for ix=1:numel(T)
    [a,b]=coeffs(T(ix));
    C(ix,ismember(allvars,b))=a;
end

which returns

>> C

C =

     5    -2     0    -2     0     0     0     0     0
     0     0     0     7    -2     0    -4     0     0
     0     0     0     0     0     0     9    -2     0
    -5     7    -4     0    -2     0     0     0     0
     0     0     0    -5     9    -4     0    -4     0
     0     0     0     0     0     0    -5    11    -4
     0    -5     9     0     0    -2     0     0     0
     0     0    -5     0    -5    11     0     0    -4
     0     0     0     0     0    -5     0    -5     8
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Just a quick query. I am trying to enter my code in this comment section but my code s not showing like yours. – A Ahmed Feb 22 '16 at 15:41
  • `P=sym('P',[3,3])` might be a way to simplify it. – Daniel Feb 22 '16 at 15:44
  • Much better. If you notice, one of the entry (-4) in the last column is misplaced. It should be in column 5. Also, i want it be a diagonal dominant matrix. – A Ahmed Feb 22 '16 at 16:04
  • @user3729921: I don't understand your comment. I did not use your image of the coefficient matrix as a template for ordering because it clearly does not match your data (e.g. only two coefficients for P33). It might be a case of row-major vs. column major ordering. – Daniel Feb 22 '16 at 16:22