UE CS 390 - A 30 Minute Introduction to Octave ENGR 390 - Engineering Mathematics

Unformatted text preview:

A 30 Minute Introduction to OctaveENGR 390 - Engineering MathematicsTony RichardsonIntroductionThis is a brief introduction to Octave. It covers several topics related to both the statistics and linear algebra portions of the course. It is recommended that you try the examples as you read through this introduction.Basic OperationsDefine a scalar (a single number) and a matrix: > a = 2; > A = [ 1 2; 3 4] # Spaces between cols, semicolons between rows A = 1 2 3 4a is a scalar while A is a 2x2 matrix whose first row contains the elements 1 and 2. Matrices are entered in row order with spaces (or commas) used to separate columns and semicolons (or a carriage return) between rows. By default the result of the definition is displayed unless the line of input ends with a semicolon (as when a is defined above). Comments begin with a hash symbol (#).We can access a particular element by specifying indices inside parenthesis (row #, column #): > A(1,1) = 6 # Change a single element in the matrix A = 6 2 3 4 > B(3,3) = 5 # Define a new matrix by defining a single element B = 0 0 0 0 0 0 0 0 5The functions zeros(m,n), ones(m,n), eye(m,n) and rand(m,n) can be used to define m x n matrices of all zeros, all ones, the identity matrix, and a matrix of random numbers.The special variables i, pi, and e represent −1, 3.1415926532..., and 2.7182818284... respectively: > A = [2+2i cos(2*pi); log(e) e^(i*pi)] A = 2.0000 + 2.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i -1.0000 + 0.0000iThe ':' (colon) operator can be used to form a vector whose elements increase (or decrease) incrementally: > x = [1:6] x = 1 2 3 4 5 6Anthony M. Richardson 1 of 10 8/20/2009> y = [9:-2:1] y = 9 7 5 3 1You can also use vectors as indices to select a submatrix: > A = rand(4,4) # Define a new random matrix A = 0.151267 0.565735 0.823435 0.032972 0.150306 0.488791 0.788545 0.115653 0.611969 0.615926 0.281130 0.085860 0.239982 0.749596 0.329931 0.979783 > A ([1 2],1) # Select first two elements from the first column ans = 0.15127 0.15031 > A([3:rows(A)],[2:columns(A)]) # Cols 2-4 of rows 3-4 ans = 0.615926 0.281130 0.085860 0.749596 0.329931 0.979783A(:) would be a column vector with all the elements of A stacked in a single column. The reshape function can be used to reshape a vector or matrix: > A = reshape([1:10],2,5) A = 1 3 5 7 9 2 4 6 8 10 > A = reshape([1:10],5,2)' # Note the transpose (') operation! A = 1 2 3 4 5 6 7 8 9 10Matrix OperationsOctave's native data type is the matrix, so the +, -, and * operators perform matrix addition, subtraction, and multiplication. > A = [ # Define a matrix separating rows by newlines > 1 2 3 > 4 5 6 > ] A = 1 2 3 4 5 6 > B = A' # B is the transpose of A (rows and columns swapped) B = 1 4 2 5 3 6Anthony M. Richardson 2 of 10 8/20/2009> A*B # Multiply matrix A times B ans = 14 32 32 77 > A + A # Add A to itself ans = 2 4 6 8 10 12 > A .* A # Element-by-element multiplication ans = 1 4 9 16 25 36Notice that element-by-element multiplication uses “.*” instead of “*”. The slash and backslash operators correspond to right and left multiplication by a matrix inverse. For example A\b is equivalent to inv(A)*b and B/A is equivalent to B*inv(A). The slash and backslash operators require fewer calculations and are less subject to numerical error than calculation and multiplication by the inverse.Getting HelpOctave has an extensive on line help system. Typing “doc” brings up the interactive help system. You can get help on any command by typing either “doc command” or “help command”. Note: The doc command does not work within QtOctave, the same interactive help is available from the Help menu or by just pressing the F1 function key on your keyboard.ProgrammingOctave provides a full programming environment with loops, selection, and functions. Listing 1 shows an example Octave function that calculates ex from its power series expansion. (Note that the exp(x) function is built into Octave, this is just an example to illustrate Octave programming.) The code can be typed in at the Octave prompt, but typically it would be saved in a file using a text editor (Notepad for example). Save the code in Listing 1 in a file named “myexp.m”. The function can be loaded and run using the following command: > myexp(1) ans = 2.7183# Compute exponential using power series definition# e(x) = 1 + x/1! + (x*x)/2! + (x*x*x)/3! + (x*x*x*x)/4! + ...function [y] = myexp(x) y = 0; n = 1; term = 1; # Loop until term is numerically equal to 0 while term != 0 y = y + term; term = term*(x/n); n = n + 1; endendfunctionListing 1Anthony M. Richardson 3 of 10 8/20/2009The Octave m-file should be in the current directory or in directory contained in the LOADPATH variable. A while loop is shown in Listing 1. The “!=” is one of the Octave comparison operators, it represents “not equal to”. Other comparison operators are: ==, <, >, <=, >= and represent “equal to”, “less than”, greater than”, “less than or equal to” and “greater than or equal to” respectively.Octave functions should normally be written to accept matrix arguments. The function in Listing 1 does not. Listing 2 shows a matrix version of the exponential function.The matrix version of this function is very similar to the scalar version in Listing 1. y and term are matrices instead of scalars now. Instead of a while loop a do-until loop is now used instead. The body of the loop will be executed until the condition (term == 0) becomes true. A matrix is not considered true until all matrix elements are true. Notice that element-by-element multiplication is used to update the elements in the term matrix. We can compare the built-in exp() function and our own mymatexp() expression as follows: > exp([0 1; 2 3]) ans = 1.0000 2.7183 7.3891 20.0855 > mymatexp([0 1; 2 3]) ans = 1.0000 2.7183 7.3891 20.0855# Compute exponential using power series definition (matrix version)function [y] = mymatexp(x) # allocate space for y y = zeros(size(x)); n = 1; term = ones(size(x)); # loop until all elements in the term matrix are 0 do y = y + term; # use element-by-element multiplication to update the terms


View Full Document

UE CS 390 - A 30 Minute Introduction to Octave ENGR 390 - Engineering Mathematics

Download A 30 Minute Introduction to Octave ENGR 390 - Engineering Mathematics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A 30 Minute Introduction to Octave ENGR 390 - Engineering Mathematics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A 30 Minute Introduction to Octave ENGR 390 - Engineering Mathematics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?