One-time setup

Log on to a workstation. Use your regular Gordon username (in firstname.lastname form, lowercase letters only, no “@gordon.edu”) and your regular Gordon password.

If this is your first time logging in you will be greeted with a list of keyboard shortcuts; you can click the X button at the upper-right of the window to dismiss it.

Left-click on the "dash" icon: this is at the upper-left of your screen and looks something like , although the color scheme may be different. Type octave into the search bar and you should find an application named “GNU Octave”; click on it to start it.

Notice that after Octave started, its icon shows up in the Launcher on the left side of your screen. Take a moment now and right-click on the icon and and select ”Lock to Launcher”. After doing this the Octave icon will stay in your launcher and you won't need to use the Dash again.


Starting Octave

Octave should now be running; if not then click on the Octave icon you've locked to your launcher and the Octave program should start. The right side of the window contains a panel called the Command Window. This is where you can type Octave commands and see the output. For example, here is an actual Octave session to solve a 2×2 linear system (the user typed the text shown here in an alternate color; it won't be a different color when you type it):

GNU Octave, version 3.8.1
Copyright (C) 2014 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "x86_64-pc-linux-gnu".

Additional information about Octave is available at http://www.octave.org.

Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html

Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.

>> A = [5 -1; -2 4]
A =

   5  -1
  -2   4

>> b = [13 2]'
b =

  13
   2

>> x = A\b
x =

  3
  2

>>

White space is helpful for readability, but not required, the second command could just have easily been entered as b=[13 2]'. Also note here that the single quote (a "tick") is used to indicate the transpose operation.

Variables can be assigned and used:

>> x = 5;
>> y = 3;
>> x+y;
>> x+y
ans = 8
>> z = x+y;
>> z
z = 8
>>

Notice how the semicolon at the end of a command suppresses the output. In cases where the result of an operation is not assigned to a variable the special variable ans contains the answer.

One of the best things about Octave is that, as an interpreted and interactive language, it is possible to develop bits of larger programs easily. Throughout the semester we'll make use of Octave for graphing, computation, operations on matrices, displaying images, and many other things. None of these things are unique to Octave, but having access (and making use of) a tool like Octave is indispensable to a numerical analyst.


Variables and Assignments

Variables in Octave should start with an alphabetic character or an underscore followed by any alphanumeric character. It is possible to have something like C structures as demonstrated by

>> s.string = "she turned me into a newt";
>> s.numwords = 6;
>> s
s =
{
  numwords = 6
  string = she turned me into a newt
}

>> s.string
ans = she turned me into a newt
>> s.numwords
ans =  6
>>

Vectors

Vectors in Octave are quite important, even apart from calculations involving linear algebra. As we will see in a moment, they are very important in creating for loops.

One way to create a vector is to list the elements in the vector:

>> v = [ 1 2 3 4 5 ]
v =

  1  2  3  4  5

>>

All the elements must be of the same type, or of compatible types (i.e., if any entry in a vector is a real number, all integer values will be converted to real numbers).

Since vectors that contain consecutive values are so useful, Octave provides a way to specify them more succinctly. When two numbers (or variables containing numeric values) are separated by a colon, Octave interprets this to mean that a vector should be generated whose first entry is the first number, whose subsequent entries are one greater than the previous entry, and whose last entry is the largest number in this sequence not to exceed the number the right of the colon. For example:

>> 1:9
ans =

  1  2  3  4  5  6  7  8  9

>>

It's also easy to generate a vector of numbers whose stride is different than one:

>> 1:2:9
ans =

  1  3  5  7  9

>>

To count backward we use a negative stride:

>> 10:-1:1
ans =
   10    9    8    7    6    5    4    3    2    1

>>

Here the number between the two colons is the stride and the first and last numbers play the same role as boundaries as they did before.

There are often several ways to generate the same vector:

>> a1 = 0:2:8;
>> a2 = [0:2:8];
>> a3 = 2*[0:4];
>>

all generate a vector containing 0, 2, 4, 6, and 8.


Loops

In languages such as Java, C++, or Fortran the for-loop construct requires the programmer to specify the starting and stopping values of the loop variable, and optionally a stride value that indicates by how much the loop variable should be incremented during each iteration.

In Octave (as in Python), however, the for loop construct uses a vector that contains all the values that the loop variable will take on. A typical for-loop in Octave looks like

for i = 1:10
  i
end

In this loop the loop variable i will take on all of the values in the vector 1:10, which will be 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Here we've taken advantage of the fact that a variable name on a line all by itself causes Octave to display the value of the variable (just be sure not to put a semicolon after the variable or you won't get any output). Finally, note that the for-loop is terminated with the end keyword.

If you want a loop that only uses even values for its loop variable you merely need to create a vector that only contains even values:

for i = 0:2:10
  i
end

In this loop, i will take on the values 0, 2, 4 ,6, 8, and 10.

Single line loops are easy; just remember to use a comma (if you want the line's result echoed) or a semicolon (to suppress echoing) where line-breaks would be:

>> for i = 0:4; 2^i, end
ans =  1
ans =  2
ans =  4
ans =  8
ans =  16
>>

Functions

Functions in Octave begin with the keyword function and end with the keyword end. Unlike functions in Java and C++, Octave functions can return a single value or multiple values.

The following Octave function will evaluate the polynomial p(x) = 2x+1 at any value of x:

function y = p(x)
  y = 2*x + 1;
end

Note that the variable y is a dummy variable that contains the return value of the function. This function can be invoked by using its name (p in this case) followed by a value for x in parentheses:

>> p(5)
ans = 11
>>

Note also that the function can be entered on a single line, using semicolons whenever a line-break would be used:

>> function y = p(x); y = 2*x + 1; end
>>

This is particularly nice since by having the entire function on a single line it is easy to edit it using the arrow keys on the keyboard.

There is an even shorter way to enter a function:

>> p = @(x) 2*x + 1
>>

Google “octave anonymous functions” for more information.

Octave functions can return more than one value. The following function computes both the square and the cube of a number n:

>> function [s,c] = squarecube(n); s = n*n; c = s*n; end
>> squarecube(3)
ans = 9
>> [s,c] = squarecube(3)
s = 9
c = 27
>>

Notice how only the first value is returned unless we explicitly request both values.


Assignment

Use the Command Window to try some of the things described above. In particular, create several functions, such as a "square" function that squares the argument it is given.

After you are comfortable doing that, try each of the following exercises. Once you have a working function, cut and paste it into the text area provided.

To cut and paste on a Linux/Unix workstation: click and hold the left mouse button to select a region; move the mouse pointer to where you want to paste and click the left mouse button again to activate the input area; and finally click the middle mouse button to paste. Alternatively, you can use the usual Ctrl-C and Ctrl-V for cut and paste.

  1. Starting with the provided example function, write a function to evaluate the polynomial p(x) = 2x3 − 5x2 + x − 3

  2. Write a function to evaluate the polynomial p(x) = a0 + a1x + a2x2 + a3x3 given a vector a = [a0 a1 a2 a3] (remember that in Octave array subscripts begin with 1 so you will need to adjust all your subscripts accordingly). The first line of the function should look like function y = p(x,a) with the vector a containing the coefficients passed as the second argument. Use Horner's Method (found in our text) to evaluate the polynomial efficiently.

  3. Write a function to evaluate the polynomial p(x) = a0 + a1x + a2x2 + ... + an-1xn-1 + anxn given an n-element vector a = [a0 a1 a2 ... an-1 an]. Be sure and use Horner's method again (it actually makes this much easier to code). Hint: To write this function you will need to determine how many elements are in the array a; I suggest the command max(size(a)). You can read about the size function by typing help size at the Octave prompt.

Please enter your name below then click the submit button.

Name:


Logging Out

Please make sure you log off when you are done. Close the Octave window and click on the gear icon at the upper-right of the screen and select "Log Out..."; please do not select "Shut Down...".