1
votes

I have a function of this form in MATLAB,

C=S*e^(L*t)*inv(S)*C_0

where my

     S=[-2 -3;3 -2] 
     L=[0.5 0; 0 1.5]
     C_0=[1; 1]

I need to plot this function with respect to time. My output C is a 2-by-1 matrix.

What I have done is computed e^L separately using b=expm(L) and then I inserted mpower(b,t) into the function. So my resulting function in the script looks like

  b=expm(L);
  C=S*mpower(b,t)*inv(S)*C_0;

Now, how should I go about plotting this w.r.t time. I tried defining the time vector and then using it, but quite obviously I get the error message which says matrix dimensions do not agree. Can someone give me a suggestion?

1

1 Answers

0
votes

You can probably do this in a vectorised manner but if you're not worried about speed or succinct code, why not just write a for loop?

ts = 1 : 100;
Cs = zeros(2, length(ts) );

S = [-2 -3;3 -2];
L = [0.5 0; 0 1.5];
C_0 = [1; 1];

for ii = 1 : length(ts)
  b = expm(L);
  Cs(:,ii) = S*mpower(b,ts(ii))*inv(S)*C_0;
end

ts contains the time values, Cs contains the values of C at each time.