Skip to main content
Library homepage
 
Engineering LibreTexts

4.11: Common Vector Functions

  • Page ID
    84399
  • \( \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}}\)

    By Carey A. Smith

    Additional MATLAB vector functions:

    Example \(\PageIndex{1}\) Functions that compute a single number from a vector

    % This is vector of CO2 levels (ppm) measured at Mauna Loa Hawaii in 2018:
    CO2 = [407.96, 408.32, 409.39, 410.25, 411.24, 410.79, 408.70, 406.97, 405.52, 406.00, 408.02, 409.08];
    % Reduce functions:
    CO2_sum = sum(CO2) % Total
    CO2_length = length(CO2) % The number of items in the vector
    CO2_min = min(CO2) % Minimum
    CO2_max = max(CO2) % Maximum
    Solution

    Add example text here.

    Example \(\PageIndex{1}\) Functions that apply an operation to a vector

    This is vector of CO2 levels (ppm) measured at Mauna Loa Hawaii in 2018:
    CO2 = [407.96, 408.32, 409.39, 410.25, 411.24, 410.79, 408.70, 406.97, 405.52, 406.00, 408.02, 409.08];
    % Apply functions and operators:
    CO2_sqrt = sqrt(CO2) % Square roots of the elements
    CO2_square = CO2.^2 % Squares of the elements (element-wise)
    CO2_log10 = log10(CO2) % logarithm, base 10
    CO2_log = log(CO2) % logarithm, base e (natural logarithm)
    % log() should be ln(), but this is a long tradition in programming languages.
    CO2_exp = exp(CO2) % e^x

    Note that sin(theta), cos(theta), tan(theta) are for theta in radians.

    theta = pi*(0 : 0.1 : 0.3)
    theta_sin = sin(theta)
    theta_cos = cos(theta)
    theta_tan = tan(theta)

    Note that sind(alpha), cosd(alpha), tand(alpha) are for alpha in degrees.

    alpha = 0 : 15 : 45
    sin_alpha = sind(alpha)
    cos_alpha = cosd(alpha)
    tan_alpha = tand(alpha)

    The attached file, MatlabCommands_Brian_Vick.pdf, courtesy of Dr. Brian Vick from Virginia Tech, is a very useful summary of the the most common MATLAB commands.

    Solution

    Add example text here.

    Exercise \(\PageIndex{1}\) Squareroot of the sum of squares

    Create this vector: v = [3, 4, 5, 6, 7]

    Then write 1 line of cose that computes the square root of the sum of the squares of the elements of the vector, without using a loop.Write 1 line of code that

    Answer

    Use the vector operator v.^2 to square each element of the vector; sum the elements sum(); compute the squareroot.

    Exercise \(\PageIndex{1}\) sin() of a vector

    (1 pt) Create a Matlab script: sin_vector_YourName.m
    (1 pt) Clear any variables; close any figures; eliminate white space; clear the console with this line of code:
    clear all; close all; format compact; clc;

    %% Part 1
    (1 pt) Create a vector called degrees = 0 to 90 in steps of 10 degrees
    degrees = 0 : 10 : 90;

    (2 pts) Use sind() to compute the sines of the degrees vector.

    (2 pts) Create a 2nd vector: radians = (2*pi/360)*degrees.
    (2 pts) Use sin() to compute the sines of the radians vector.
    %% Part 2
    (2 pts) Convert the radians vector created in part 1 from a row vector
    to a column vector with this code:
    radians_col = radians' % The ' transposes a vector
    (1 pt) Use cos() to compute the cosine this column radians vector.

    Answer

    Add texts here. Do not delete this text first.

    .

    In the "Back Matter" of this book, there is a section called "MATLAB Commands and Functions". This has an attachment with short description of many MATLAB command and functions. This is a good place to search for a function that would be useful for a particular program.

    You and also use the MATLAB help browser to find useful functions.

    .


    This page titled 4.11: Common Vector Functions is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.