Search code examples
matlabmatrixsymbolic-math

Each element in my 3x3 matrix is an equation with respect to t. How do I plot each one with respect to time?


I have a 3x3 matrix along the lines of:

C = [sin(t) cos(t)*4t^2 cos(t); cos(t) 5t^3 4*cos(t); t*tan(t) 4t^2*cos(t) 5sin(2*t)]

And a specified array t, representing time from 0 to 50 seconds with a sampling rate of 0.1s.

t = 0:0.1:50;

In the C matrix, t is symbolic right now. It started out as a function of many variables, but I used subs() to get it to just a function of t. I'd like a graph of each of the 9 elements with respect to t. How do I do this? I thought it would be very simple, something like subs(C), or subs(C(1,1)) once I specified t, but it turns out that's not the case.


Solution

  • The best approach is to convert to a function handle and to replace all of the multiplication operations with their elementwise analogs. For instance, if you want to compute the value of

    cos(t) * t^2 
    

    for a vector t = 1:10, the above will not work because it tries to do matrix multiplication. To do elementwise multiplications and powers, put a dot in front of the operator like

    cos(t) .* t.^2 
    

    So to do what you want, write C as a vector (not a matrix) and make sure you add in the necessary elementwise operations...

    C = [ sin(t) ; cos(t).*4*t.^2 ; cos(t) ; cos(t) ; 5*t.^3 ; 4*cos(t) ; t.*tan(t) ; 4*t.^2.*cos(t) ; 5 *sin(2*t) ]
    

    Alternatively, if you want to evaluate C at many different row vectors t, then you can write C as a function handle...

    C = @(t) [ sin(t) ; cos(t)*4.*t.^2 ; cos(t) ; cos(t) ; 5.*t.^3 ; 4*cos(t) ; t.*tan(t) ; 4*t.^2.*cos(t) ; 5 *sin(2*t) ]
    

    You can now try evaluating C like this

    C(1:10)
    
    C(.1:.22:50)
    

    and each column of the output will represent C at a different value of t. So now you can do something like

    plot(C(t))
    

    or

    plot(t,C(t))