12.5: What Could Go Wrong?

Physical Modeling in MATLAB · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    12.5: What Could Go Wrong?

    What could go wrong? Well, vertcat for one. To explain what that means, I’ll start with concatenation, which is the operation of joining two matrices into a larger matrix. Vertical concatenation joins the matrices by stacking them on top of each other; horizontal concatenation lays them side by side.

    Here’s an example of horizontal concatenation with row vectors:

    >> x = 1:3
    x = 1     2     3
    
    >> y = 4:5
    y = 4     5
    
    >> z = [x, y]
    z = 1     2     3     4     5

    Inside brackets, the comma operator performs horizontal concatenation. The vertical concatenation operator is the semicolon. Here’s an example with matrices:

    >> X = zeros(2, 3)
    
    X =  0     0     0
         0     0     0
    
    >> Y = ones(2, 3)
    
    Y =  1     1     1
         1     1     1
    
    >> Z = [X; Y]
    
    Z =  0     0     0
         0     0     0
         1     1     1
         1     1     1

    These operations only work if the matrices are the same size along the dimension where they are glued together. If not, you get

    >> a = 1:3
    
    a = 1     2     3
    
    >> b = a'
    
    b =  1
         2
         3
    
    >> c = [a, b]
    Error using horzcat
    Dimensions of matrices being concatenated are not consistent.
    
    >> c = [a; b]
    Error using vertcat
    Dimensions of matrices being concatenated are not consistent.

    In this example, a is a row vector and b is a column vector, so they can’t be concatenated in either direction.

    Reading the error messages, you might guess that horzcat is the function that performs horizontal concatenation, and likewise with vertcat and vertical concatenation. You would be correct.

    In Listing 12.1 we used vertical concatenation to pack dPdt and dVdt into the output variable:

    function res = rate_func(t, W)
        P = W(1:2);
        V = W(3:4);
    
        dPdt = V;
        dVdt = acceleration(t, P, V);
    
        res = [dPdt; dVdt];
    end

    As long as dPdt and dVdt are column vectors, the semicolon performs vertical concatenation, and the result is a column vector with four elements. But if either of them is a row vector, that’s trouble.

    The ode45 function expects the result from rate_func to be a column vector, so if you are working with ode45, it’s probably a good idea to make everything a column vector.

    In general, if you run into problems with horzcat and vertcat, use size to display the dimensions of the operands, and make sure you are clear on which way your vectors go.


    This page titled 12.5: What Could Go Wrong? was last modified on Thu, 17 Nov 2022 18:20:04 GMT and is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.