简体   繁体   中英

Plotting time graph in MATLAB

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 wrt 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM