Skip to main content
Engineering LibreTexts

12.5: What Could Go Wrong?

  • Page ID
    84748
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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? 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; a detailed edit history is available upon request.

    • Was this article helpful?