Time for action – using the cos function

  1. In order to calculate cosine of a number, say π, we simply type:

    pi is the input argument, cos is the Octave function and -1 is the output from cos. When we use a function we often say that we call that function

    octave:1>cos(pi)
    
    ans = -1
    
  2. What if we use a vector as input? Well, why not just try. We know how to instantiate a vector:
    octave:2> x = [0:pi/2:2*pi]
    
    ans =
      0.0000    1.5708    3.1416    4.7124    6.2832    
    

    Cosine of x is then:

    octave:3>cos(x)
    
    ans =
      1.0000e+000   6.1230e-017   -1.0000e+000   -1.8369e-016   1.0000e+000
    

    that is, Octave calculates the cosine of each element in the vector.

  3. In Command 2, we created a row vector, and the result of Command 3 is therefore also a row vector. If we use a column vector as input ...

Get GNU Octave now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.