Octave Revision Pack

download Octave Revision Pack

of 32

Transcript of Octave Revision Pack

  • 8/3/2019 Octave Revision Pack

    1/32

    Octave

    OR MATH, SCIENCE, AND ENGINEERING STUDENTS

    Fotios Kasolis

    [ free your numbers ]

    revision pack

  • 8/3/2019 Octave Revision Pack

    2/32

    GNU OCTAVE/MATLAB REVISION PACK

    Fotios Kasolis 2010 - 2011

    Version 1.4.10

    ABSTRACT. Matlab and GNU Octave are interactive scientific environments for

    numerics that integrate computation, visualization, and programming. They

    both include a large number of functionalities related to linear algebra,

    polynomials, ordinary differential equations, optimization, etc. Their matrix

    oriented language and high level interpreted character make them easy to learn

    and use. GNU Octave is available for Linux, MacOS X, and Windows and is

    freely redistributable under the GPL, whereas Matlab is a commercial product.

    CONTENTS

    1. INTRODUCTION 2

    2. ASSIGNMENTS AND VARIABLES 3

    3.VECTORS, RANGES, AND MATRICES 4

    4. GRAPHS 6

    5. LOOPS AND CONDITIONALS 8

    6. SCRIPTS AND FUNCTIONS 10

    7. APPLICATION 1.ROOT FINDING 14

    8. APPLICATION 2.SPARSE MATRICES AND DERIVATIVES 18

    9. APPLICATION 3.THE VAN DER POL OSCILLATOR 21

    10. APPLICATION 4.CELLULAR AUTOMATA 26

    PROBLEM SET 29

    REFERENCES 31

  • 8/3/2019 Octave Revision Pack

    3/32

    1 INTRODUCTION

    +, -, *, /, ^, pi, i, inf, eps, format, sin, cos, tan, asin, acos, atan, asinh, acosh, exp, log,log10, log2, abs, sqrt, sign, round, ceil, floor, fix,max,min, help.

    The simplest way to use Octave is to do arithmetic as you would do with a commoncalculator. Addition, subtraction, multiplication, division, and exponentiation arerepresented by the operators +, -, *, /, ^ respectively. For instance, type at the prompt thefollowing expression

    > (3 + 2^(-1)) / 0.5 ! ans = 7Note that white spaces are ignored by Octave and the example above could be equivalently

    written as

    > (3+2^(-1))/0.5 ! ans = 7Octave displays the answer and assigns the resulting value to the variable ans. To changethe format in which numbers are displayed, use the format function with basic argumentsshort (default) and long.

    > pi!

    ans = 3.1416! > format long! > pi! ans = 3.14159265358979where the variable pi represents the mathematical constant !=3.14... . Octave knows most(if not all) of the standard mathematical functions. A function is invoked with its namefollowed by the arguments in round brackets.

    > sin (2.7)^2 + cos (2.7)^2! ans = 1! > log (exp (2))! ans = 2where we note that the trigonometric functions work with radians, and the naturallogarithm is represented by the function log.

    A complex number is one that can be written in the form z=x+yi where i=(-1)1/2 is theimaginary unit, x=Re(z) is the real part of z, and y=Im(z) the imaginary part. In Octave,the imaginary unit is represented by the symbol i (or j). The complex number 1+2.3i isentered as

    > 1 + 2.3i

    ! ans = 1.0000 + 2.3000i! > 1+2.3j2 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    4/32

    ! ans = 1.0000 + 2.3000iThe magnitude |z|=(x2+y2)1/2 of a complex number z with x=Re(z) and y=Im(z) can becalculated by the function abs (or norm); moreover you can use the functions real and imagto retrieve the real and imaginary part respectively.

    To get detailed information about a function func, type help func, whereas you cansearch for functions related to a keyword key by typing lookfor key. Moreover, twoautocompletion shortcuts are provided. The TAB key tries to autocomplete a function namebased on Octaves knowledge, while the UP-ARROW key tries to autocomplete based on userinput (history).

    2 ASSIGNMENTS AND VARIABLES

    =, ,, ;, ..., #, %, #{#}, %{%},who,whos, exist, clear, global, isglobal, persistent.

    Variables are symbolic names associated with values. The name of a variable must be asequence of letters, digits, and underscores, but it must not begin with a digit. Octave doesnot enforce a limit on the length of variable names. Case is significant in variable names.The symbols x and X can be used as distinct variable names. In Octave, the assignmentoperator is the equal sign (=). Some valid assignments are

    ! > x = 1! x = 1! > distance = sqrt (4^2 + 3^2)! distance = 5! > i_am_a_long_variable_name = 25! i_am_a_long_variable_name = 25New variables can be defined by using existing ones.

    ! > x = 3; y = 4; # ; is used to suppress output! > d = sqrt (x^2 + y^2)! d = 5where semicolons (;) are used to separate multiple instructions and suppress the output,whereas # (or % for both Octave and Matlab) initializes a single line comment.

    To get a list showing the user defined variables we typewho, whereas we can check avariables value by typing its name. Octave does not forget assignments unless instructed todo so. To clear a previous assignment we use the clear command.

    ! > clear x y! > x! error: `x' undefined near line 9 column 1! > y! error: `y' undefined near line 9 column 1! > d! d = 5! > exist ('x')! ans = 0 # this is false

    GNUOCTAVE/MATLAB REVISION PACK | 3

  • 8/3/2019 Octave Revision Pack

    5/32

    ! > exist('y')! ans = 0! > exist ('d')! ans = 1 # this is true

    3 VECTORS, RANGES, AND MATRICES

    zeros, ones, eye, diag, rand, full, sparse, toeplitz, norm, det, trace, inv, lu, qr, eig,

    svd, cond, expm.

    A vector is an ordered set of numbers. To enter a row vector type a set of numbersseparated by commas (,) or white spaces inside square brackets.

    ! > v = [ 7, 3, 9, 0, 2, 4, 1 ]! v =! 7 3 9 0 2 4 1We can enter a column vector by typing a set of numbers separated by semicolons (;) inside

    square brackets.

    ! > w = [ 7; 3; 9 ]! w =! 7! 3! 9

    A range is a row vector with evenly spaced elements. Ranges are created by instructionsof the form ::, where the can be omitted (the default is 1).

    ! > a = 0:5! a =! 0 1 2 3 4 5! > b = 0:0.2:0.6! b =! 0.00000 0.20000 0.40000 0.60000To change v from row to column vector we ask from Octave for its transpose by typing

    v.', while for the conjugate transpose we omit the period v'. Another way to to get acolumn vector is by typing v(:). We can perform several operations on vectors. For

    instance, to square the elements of the vector v we type

    ! > v.^2! ans =! 49 9 81 0 4 16 1The period in the above instruction says that the numbers in v should be squared

    individually. Typing v^2 would tell Octave to use matrix multiplication to multiply v byitself and would have produce an error. Similarly, we must use .* and ./ for element-wise

    multiplication and division respectively. Most Octave functions are, by default, performed

    4 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    6/32

    element by element. For example, we type exp(v) to get the exponential of each number inv.

    The ith element of a vector can be specified through Octaves indexing rules, that is bytyping the vector variable followed by the position index inside round brackets. Ifv=[7,3,9,0,2,4,1] we have v(1)==7, v(2)==3, v(3)==9, v(4)==0, v(5)==2, v(6)==4, andv(7)==v(end)==7. To extract a vector containing the first, the third, and the seventhelement of v we type

    > v([ 1, 2, 3 ])! ans =! 7 3 9Elements of vectors can be extracted with ranges. For instance, v(4:length(v)) is the sameas v(4:end) and as v([4,5,6,7]), where the length function returns the number ofelements in v.

    Entering matrices in Octave is as easy. For example, the matrix

    A =1 1

    2 2

    !

    "

    ####

    $

    %

    &&&&

    is entered in Octave by typing

    ! > A = [ 1, 1; 2, 2 ]! A =! 1 1! 2 2For that specific example we could compose the matrix A by two column vectors [1;2].

    ! > v = [ 1; 2 ]! v =! 1! 2! > A = [ v, v ]! A =! 1 1! 2 2The individual elements are extracted by typingA(row,column), we haveA(1,1)==A(1,2)==1,A(2,1)==A(2,2)==2, whereas an entire row or column is denoted by a colon (:).

    ! > A(:, 1)! ans =! 1! 2! > A(:, 2)! ans =!1! 2! > A(1, :)

    GNUOCTAVE/MATLAB REVISION PACK | 5

  • 8/3/2019 Octave Revision Pack

    7/32

    ! ans =! 1 1! > A(2, :)! ans =! 2 2

    4 GRAPHSplot, contour, polar, quiver, bar, plot3, surf,mesh, imagesc,meshgrid, xlabel, ylabel,

    zlabel, title, grid, axis, hold, subplot, figure, print, set, gcf.

    The basic plotting function is plot. To plot the graph of the function f:X!Y we define a

    discrete version of the domain X as follows.

    ! > x = 0:0.01:2*pi;Moreover, we calculate the function values at X, for instance if f(x)=sinx we have

    ! > y = sin (x);The last step is to invoke the plot function as shown below.

    ! > plot (x, y)To save the produced graph we call the function print.

    > print ~/Desktop/sin_plot.eps -deps

    We can modify a graph in a number of ways. To add axes labels and title we use the

    functions xlabel, ylabel, and title.

    6 | FOTIOS KASOLIS 2010-2011

    -1

    -0.5

    0

    0.5

    1

    0 1 2 3 4 5 6 7-1

    -0.5

    0

    0.5

    1

    0 1 2 3 4 5 6 7

    y

    x

    f(x)=sin(x)

  • 8/3/2019 Octave Revision Pack

    8/32

    We would also like to change the font size, the font style, and the axes limits to fit exactlythe plotted function. To permanently enforce the changes we use the following statements inthe file .octaverc.

    WARNING! All the graphics of this document have been plotted using the followingstatements in .octaverc and printed as eps using the option -deps in the print function.

    set (0, 'defaultaxesfontsize', 26);! set (0, 'defaulttextfontsize', 26);! set (0, 'defaultaxesfontname', 'Times-Roman');! set (0, 'defaultaxesfontname', 'Times-Roman');

    Often we want to overlay two (or more) plots on the same set of axes. The way to do itis shown below.

    > x = 0:0.01:2*pi;! > plot (x, sin (x), 'g', x, cos (x), 'm')This example also shows how to control the line color. To change the color replace g andmin the above instruction with k, b, y, r. Apart from color we can use different markersinstead of a continuous line.

    > x = 0:0.1:2*pi;! > plot (x, sin (x), 'xg', x, cos (x), 'om')

    Below we give a complete example in which several different properties of a plot arechanged in order to achieve a publication quality result.

    > x = 0:0.1:40;! > f = besselj (0, x);! > d = f + rand (1, length(x)) - 0.5;! > u = f + 0.5;! > l = f - 0.5;! > plot (x, f, 'k', 'linewidth', 5, ...

    GNUOCTAVE/MATLAB REVISION PACK | 7

    -1

    -0.5

    0

    0.5

    1

    0 1 2 3 4 5 6 7-1

    -0.5

    0

    0.5

    1

    0 1 2 3 4 5 6 7

  • 8/3/2019 Octave Revision Pack

    9/32

    x, d, '*k', 'markersize', 1, ...x, u, 'm', 'linewidth', 5, ...

    x, l, 'g', 'linewidth', 5)! > xlabel ('x')> ylabel ('J_0(x)')! > grid on

    ! > legend ('Bessel function', 'Measured data', ...! ! ! ! 'Upper bound', 'Lower bound')

    5 LOOPS AND CONDITIONALS

    &, |, ~, !, ==, ~=, !=, , =, if, elseif, else, endif, for,while, switch, break,

    continue, pause.

    In Octave, control flow statements operate like those in common computer languages.

    Indenting the instructions of a loop or conditional statement is optional, but helpsreadability.

    ! > if v < 0.25! > !! x = -1;! > !! y = 0;! > elseif v < 0.5!>!!

    x = +1;! > !! y = 0;! > elseif v < 0.75! > !! x = 0;! > !! y = -1;! > else! > !! x = 0;! > !! y = +1;! > endif

    The if statement evaluates a logical expression and executes a group of instructions

    when the expression is true (=1). Alternative instructions can be executed by using the

    optional keywords elseif and else. In the example above a random number is generatedusing the function rand and a conditional decides the direction of a step.

    8 | FOTIOS KASOLIS 2010-2011

    -1

    -0.5

    0

    0.5

    1

    1.5

    0 5 10 15 20 25 30 35 40

    J0

    (x)

    x

    Measured dataUpper boundLower bound

    boxon

  • 8/3/2019 Octave Revision Pack

    10/32

    Octave includes for andwhile loops. To make the above example more interesting wemodify it as follows.

    ! > N = 10000;! > x = zeros (1, N);! > y = zeros (1, N);! > for i = 1:N-1! >! ! v = rand (1);! >! ! if v > 0.75! >! ! ! x(i+1) = x(i) - 1;! >! ! ! y(i+1) = y(i);! >! ! elseif v > 0.5! >! ! ! x(i+1) = x(i) + 1;! >! ! ! y(i+1) = y(i);! >! ! elseif v > 0.25! >! ! ! x(i+1) = x(i);! >! ! ! y(i+1) = y(i) - 1;! >! ! else! >! ! ! x(i+1) = x(i);! >! ! ! y(i+1) = y(i) + 1;! >! ! endif! > end! > plot (x, y)

    WARNING! In Matlab, loops and conditionals end with the same end statement. A simpleend is an accepted end of block statement for Octave. It is a coding style guideline to usespecific end of block statements (endif, endswitch) rather than the generic end that is usedonly in case offor loops.

    C OMPARISON AND BOOLEAN OPERATORS

    x < y True (=1) ifx is less than y.

    GNUOCTAVE/MATLAB REVISION PACK | 9

    0

    20

    40

    60

    80

    100

    120

    140

    0 20 40 60 80 100 120

    y

    x

    Random walk

  • 8/3/2019 Octave Revision Pack

    11/32

    x y True ifx is greater than y.

    x >= y True ifx is greater than or equal to y.

    (Octave) x != y

    (Octave and Matlab) x ~= yTrue ifx is not equal to y.

    b1 & b2 True if both b1 and b2are true.

    b1 | b2 True if either ofb1 and b2are true.

    (Octave) ! b

    (Octave and Matlab) ~ bTrue if the b is false (=0).

    Some examples follow.

    ! > (1 < 2) & (2 (1 == 2) & (2 false | true! ans = 1! > ~ true # not true = false! ans = 0! > ! false # not false = true! ans = 1

    6 SCRIPTS AND FUNCTIONS

    function, endfunction, return, nargin, nargout, varargin, varargout, feval, eval, tic,

    toc, keyboard, dbcont, dbquit.

    We can execute a sequence of regular Octave instructions stored in simple text files

    called scripts. Apart from regular statements we can create new function files specific to theproblem we want to solve. Script and function files are sometimes called m-files since they

    have the extension .m.

    To write our first script, we open our favorite (simple) text editor (nano, vim, emacs,etc.) and we type the instructions shown below.

    # example 1: xmpl.mA = [ 1, 2; 2, 2];b = [3; 2];x = A \ b

    10 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    12/32

    We save the file as xmpl.m. To execute the contents of the created script we move to thedirectory we saved the file (for instance ~/Desktop) using

    > cd ~/Desktop

    and then we type the name of the file without the extension .mand hit enter.

    ! > xmpl! x =! -1! 2A function file has the general structure

    function [] = ()

    endfunction

    where endfunction denotes the end of the function and should be avoided if you wantMatlab compatible code. To invoke a function we type the function name followed by itsargument in round brackets. Preferably the and the name of the .mfilethat defines the function are the same.

    Numerical calculation of derivatives

    Below we give an example of a function which uses the complex step derivative

    approximation to calculate the derivative of a function f at a point x0. By Taylor expansionwe have that

    f(x + ih)= f(x)+ ihf '(x)! h2f ''(x)

    2!! ih3

    f '''(x)

    3!+!

    Taking the imaginary part of both sides and dividing by h yields

    f '(x)=

    Im(f(x+ ih))

    h+

    h

    2 f '''(x)

    3!+!

    that is an O(h2) estimate of the derivative of the function f at x. This method isimplemented in Octave as follows.

    function [ dfdx ] = complexStepDeriv (f, x0, h)

    dfdx = imag (f (x0 + i * h)) ./ h;

    endfunction

    GNUOCTAVE/MATLAB REVISION PACK | 11

  • 8/3/2019 Octave Revision Pack

    13/32

    To call this function we have to define the mathematical function f which we want

    differentiate. To do so, we could create another function file, but for simple cases we can use

    anonymous functions, for instance if f(x)=sinx we type

    > f = @(x) sin (x); # anonymous function definition

    and we invoke the complexStepDeriv function as in the following example.

    > format long! > complexStepDeriv (f, pi, 10^-1)! ans = -1.00166750019844! > complexStepDeriv (f, pi, 10^-2)! ans = -1.00001666675000! > complexStepDeriv (f, pi, 10^-3)! ans = -1.00000016666667! > complexStepDeriv (f, pi, 10^-4)! ans = -1.00000000166667! > complexStepDeriv (f, pi, 10^-5)! ans = -1.00000000001667! > complexStepDeriv (f, pi, 10^-6)! ans = -1.00000000000017! > complexStepDeriv (f, pi, 10^-7)! ans = -1.00000000000000!The complexStepDeriv function accepts vectors as input for x0 or h as shown in the

    following example.

    ! > n = -7:0;! > h = 10.^n;! > dfdx = complexStepDeriv (f, pi, h);! > loglog (h, abs (-1-dfdx), 'linewidth', 5);! > xlabel h;! > ylabel '|-1-df(\pi)/dx|';! > grid on; axis equal;

    12 | FOTIOS KASOLIS 2010-2011

    10-16

    10-14

    10-12

    10-10

    10-8

    10-6

    10-4

    10-2

    100

    10-7

    10-6

    10-5

    10-4

    10-3

    10-2

    10-1

    100

    |-1-

    df()/dx|

    h

  • 8/3/2019 Octave Revision Pack

    14/32

    When writing a function you can easily define default values for input arguments.

    function [ dfdx ] = complexStepDeriv (f, x0, h = 10^-7)

    dfdx = imag (f (x0 + i * h)) ./ h;

    endfunction

    WARNING! Defining default values as in the example above is valid only in Octave.

    The above function could be called with only two arguments.

    > dfdx = complexStepDeriv (f, pi)

    dfdx = -1.00000000000000

    A Matlab compatible way for defining default values is

    function [ dfdx ] = complexStepDeriv (f, x0, h)

    if nargin == 2% nargin is the number of input arguments

    h = 10^-7;

    end

    dfdx = imag (f (x0 + i*h)) ./ h;

    % endfunction

    Variables in a function file are local, meaning that unlike the variables defined in scriptfiles, these variables are completely unrelated to any of the variables with the same namesthat are defined in the command line, and Octave does not remember their values afterexecuting the function.

    Time can be measured by the tic - toc functions (tic starts the timer and toc printsthe elapsed time since tic was used).

    ! > tic; complexStepDeriv (f, 0:10^-6:2*pi); toc! Elapsed time is 2.8265950679779 seconds.Below we give a script that could be used to check how Octave compares to Matlab withrespect to efficiency (a sloppy benchmark!).

    clear all;

    f = @(x) sin (x);

    time = zeros (1, 7);

    for i = 1:7

    step = 10^-i;

    tic; complexStepDeriv (f, 0:step:2*pi); time(i) = toc;

    end

    Debugging

    Often (or very often!) your code does not work at all, or has some strange behavior. You

    can debug Octave code using the function keyboard. The function keyboard stops theexecution and gives control to the user. The debugging status is indicated by the keyword

    GNUOCTAVE/MATLAB REVISION PACK | 13

  • 8/3/2019 Octave Revision Pack

    15/32

    debug (or K in Matlab) appearing before the prompt. In debugging mode you can examine

    variables and use any valid Octave command. As an example we modify the

    complexStepDeriv function as shown below.

    function [ dfdx ] = complexStepDeriv (f, x0, h = 10^-3)

    keyboard;

    dfdx = imag (f (x0 + i*h)) ./ h;

    endfunction

    ! > complexStepDeriv (f, pi)! keyboard: stopped in /Users/fotios/Desktop/complexStepDeriv.m! debug> h! h = 0.00100000000000000! debug> h = 10^-7;! debug> h! h = 1.00000000000000e-07! debug> dbcont! ans = -1.00000000000000

    where dbcont is used to terminate the debugging mode and continue execution(alternatively you can use return). If you enter debug mode in a long for loop, it willprobably be enough to check some iterations and then stop, to do so use dbquit.

    7 APPLICATION.ROOT FINDINGroots, polyval, polyfit, polyder, polyint, fzero, optimset, optimget, fminunc, gls, sqp.

    Brief theory

    The problem of finding analytically the roots of a non-linear equation f(x)=0 is difficultor not feasible, while with a few lines of code we can find numerical solutions instantly. A

    14 | FOTIOS KASOLIS 2010-2011

    10-4

    10-3

    10-2

    10-1

    100

    101

    102

    1 2 3 4 5 6 7

    Timeinsecs

    Problem size indicator

    Octave vs Matlab

    OctaveMatlab

  • 8/3/2019 Octave Revision Pack

    16/32

    slow but yet robust method is the bisection method. This method approximates the rootguaranteed by Bolzanos theorem.

    (Bolzano) LetD=[a,b] andf:D!. If fis continuous onD andf(a)f(b)0, there may or may not be a root between a and b. If f(a)f(b)

  • 8/3/2019 Octave Revision Pack

    17/32

    Its roots can be found using the function roots.

    ! > r = roots (p)! r =! -3.0764! -1.5925! 3.0764! 1.5925To check the correctness of this answer we evaluate the polynomial values at the roots.

    ! > f = polyval (p, r)! f =! 1.3323e-15! 1.1102e-15! 1.3323e-15! 4.4409e-16and we also check it graphically.

    ! > x = -4:0.1:4;! > plot (x, x.^4 / 24 - x.^2 / 2 + 1, 'k', 'linewidth', 5, r, f, 'or'); The more general problem of finding the zeros of a transcendental function of a single

    variable is solved in Octave by the command fzero. Suppose we are interested in finding

    the zeros of f(x)=cosx-x2, to do so we type

    ! > f = @(x) cos (x) - x.^2;! > z = fzero (f, 0.5)! z = 0.82413! > fz = f (z)! fz = 1.2212e-15! > x = 0:0.01:1;! > plot (x, f (x), 'k', 'linewidth', 5, z, fz, 'or')where the second argument in the fzero function is a starting point. In one dimensionalproblems a good starting point (a point relatively close to the root) can be found by

    plotting the function.

    16 | FOTIOS KASOLIS 2010-2011

    -1

    0

    1

    2

    3

    4

    -4 -3 -2 -1 0 1 2 3 4

    p(x)

    x

    -1

    -0.5

    0

    0.5

    1

    0 0.2 0.4 0.6 0.8 1

    f(x)

    x

  • 8/3/2019 Octave Revision Pack

    18/32

    Octaves function fsolve can be used to solve systems of non-linear equations. Considerthe following set of three equations in three unknowns (x, y, z).

    sinxy+ exp(!xz)! 0.95908 = 0

    z x2 + y2!

    6.7082= 0

    tany

    x+ cosz+ 3.17503= 0

    To solve this system we first define the following function

    function [ J ] = systemEq (x)

    J = zeros (3, 1);

    J(1) = sin (x(1) * x(2)) + exp (-x(1) * x(3)) - 0.95908;

    J(2) = x(3) * sqrt (x(1)^2 + x(2)^2) - 6.7082;J(3) = tan (x(2) / x(1)) + cos (x(3)) + 3.17503;

    endfunction

    and then we call fsolve as shown below.

    ! > [ x, fval, info ] = fsolve (@systemEq, [ 0.1, 1.8, 2.2 ],...> optimset ('TolX', eps, 'TolFun', eps))! x =! 0.12519 2.21695 3.02106! fval =

    ! -1.1102e-16! -1.7764e-15! 2.1760e-14! info = 1where info=1 means that the relative residual error is less than specified by TolFun.

    Appendix. Implementation of bisection method

    function [ root ] = bisectionMethod (f, a, b , tol = 10^-5)

    fa = f (a); if abs (fa) < eps root = fa; return; endif

    fb = f (b); if abs (fb) < eps root = fb; return; endif

    iterNum = ceil (log (abs (b - a) / tol) / log (2.0));

    for i = 1 : iterNum

    c = 0.5 * (a + b); fc = f (c);

    if abs (fc) < eps

    root = c; return; endif

    if fb * fc < 0

    a = c; fa = fc;

    else

    b = c; fb = fc;

    endif

    disp ([ 'iter = ', num2str(i), ' x0 = ', num2str((a + b) / 2) ]);

    end

    root = (a + b) / 2;endfunction

    GNUOCTAVE/MATLAB REVISION PACK | 17

  • 8/3/2019 Octave Revision Pack

    19/32

    8 APPLICATION.SPARSE MATRICES AND DERIVATIVEStoeplitz, sparse, nnz, spy.

    Sparse matricesA sparse matrix is a matrix dominated by zeros, so that special techniques can be used

    for storing instead of storing every single element. One well-known sparse (banded,tridiagonal) matrix comes from the finite difference approximation to the second derivative

    of a function. By Taylors expansion we have that

    f(x + h)! f(x)+ hf '(x)+h2

    2f ''(x)

    f(x!

    h)"

    f(x)!

    hf '(x)+

    h2

    2 f ''(x)

    By adding the above approximations we get

    f(x + h)! 2f(x)+ f(x! h)" h2f ''(x)

    Given the function values f=(f1,f2,...,fn)T at the points x=(x1,x2,...,xn)T an approximation of

    the second derivative is

    f ''(xi)!

    f(xi+ h)" 2f(x

    i)+ f(x

    i" h)

    h2=

    1

    h2f

    i+1" 2f

    i+ f

    i"1( )

    or in matrix form

    f '' =1

    h2Tf

    where

    T =

    !2 1 0 ! 0

    1 !2 1 " 0

    0 1 !2 " 0

    # " " " 1

    0 0 0 1 !2

    "

    #

    $$$$$$$$$$$$$

    %

    &

    '''''''''''''''

    To construct the above matrix in Octave we type

    ! > T = toeplitz ([ -2, 1, 0, 0, 0 ])18 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    20/32

    ! T =! -2 1 0 0 0! 1 -2 1 0 0! 0 1 -2 1 0! 0 0 1 -2 1! 0 0 0 1 -2Since this is a sparse matrix (as its size grows it is dominated by zeros) we only need tostore the non-zero entries. The storing technique used by Octave is the compressed columnformat meaning that it stores a position index and the values of the non-zero entries in acolumn-wise manner.

    ! > T = sparse (T)! T =! Compressed Column Sparse (rows = 5, cols = 5, nnz = 13 [52%])! (1, 1) -> -2! (2, 1) -> 1! (1, 2) -> 1! (2, 2) -> -2! (3, 2) -> 1! (2, 3) -> 1! (3, 3) -> -2! (4, 3) -> 1! (3, 4) -> 1! (4, 4) -> -2! (5, 4) -> 1! (4, 5) -> 1! (5, 5) -> -2The non-zero elements of a matrix can be found by the nnz function.

    ! > nnz (T)! ans = 13Finally, Octave offers the possibility to plot the sparsity pattern, that is the structure of thenon-zero elements of a matrix.

    ! > spy (T)

    GNUOCTAVE/MATLAB REVISION PACK | 19

    0

    1

    2

    3

    4

    5

    60 1 2 3 4 5 6

  • 8/3/2019 Octave Revision Pack

    21/32

    Dirichlet boundary value problem

    Consider the following one-dimensional boundary value problem

    u ''(x)=!1, "x # (0,1)

    u(0)=

    u(1)=

    0

    To discretize the domain [0,1] we distribute equidistant points from 0 to 1, that is xi=ih

    i=0,...,N and Nh=1, or in vector form x=(0,h,2h,...,1)T. The aim is to compute the vector of

    unknowns u=(0,u1,u2,...,uN-1,0)T, where the ends are known by the Dirichlet boundarycondition (constraint on the function values). As we already mentioned the second

    derivative can be approximated by

    u '' =1

    h

    2Tu

    thus the discrete problem can be written as

    1

    h2Tu =!diag(I

    N!1)" Tu =!h2diag(I

    N!1)

    This system can be solved in Octave by using the backslash operator \ as shown below.

    function [ u ] = boundaryValueProb (h)n = 1 / h - 1;

    I = -h^2 * diag (ones (n));

    T = toeplitz ([ -2, 1, zeros(1, n - 2) ]);

    u = T \ I;

    u = [ 0; u; 0];

    endfunction

    Then we define h and the domain [0,1] and we call our function.

    ! > h = 0.1;! > x = 0:h:1;! > u = boundaryValueProb (h);! > plot (x, u, 'linewidth', 5, x, u, 'ok'); > xlabel x;! > ylabel u(x);! > title 'd^2u(x)/dx^2=1, u(0)=u(1)=0';! > grid onTo make our problem slightly more interesting we let u''(x)=-cos(0.5!x) for all x in (0,1). Tochange the rhs in our function we modify it as shown below.

    20 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    22/32

    function [ u ] = boundaryValueProb (h)

    n = 1 / h - 1;

    x = h:h:1-h;

    rhs = cos (0.5*pi*x(:));

    I = -h^2 * rhs;

    T = toeplitz ([ -2, 1, zeros(1, n - 2) ]);u = T \ I;

    u = [ 0; u; 0];

    endfunction

    Try it!

    9 APPLICATION.THE VAN DER POL OSCILLATORlsode.

    Brief theory

    The French physicist Lienard studied dynamical systems of the form

    !!x + f(x) !x + g(x) = 0

    This second order differential equation includes the van der Pol equation

    !!x + (x2!1) !x + x = 0

    (where ##0) that arose in connection with non-linear electrical circuits. The van der Polequation looks like a simple harmonic oscillator, but with a non-linear factor $=#(x2-1). Forx>1 the factor $ is positive and acts like regular damping, while for x

  • 8/3/2019 Octave Revision Pack

    23/32

    !x = y

    the van der Pol equation becomes a system of two first order equations

    !x= y, !y

    =!x!

    (x2!

    1)y

    To qualitatively analyze the behavior of the system we find the fixed points, that is the

    points at which the velocities are zero.

    ( !x, !y) = (0, 0)! (y*,"x

    *" (x

    *

    2"1)y

    *) = (0, 0)

    Thus, the unique fixed point is

    (x*, y

    *) = (0, 0)

    Given a fixed point we are interested in its stability properties. Using (linear) stability

    analysis we find what is the future of small perturbations around the fixed point.

    x = x*+ u, y = y

    *+ v

    By using Taylors expansion and keeping only the first order terms we get for the general

    system

    !x = f(x, y), !y = g(x, y)

    two linear differential equations that predict the future of the perturbations (u,v).

    !u

    !v

    !

    "###

    $

    %&&&&=

    df(x*, y

    *) / dx df(x

    *, y

    *) / dy

    dg(x*, y

    *) / dx dg(x

    *, y

    *) / dy

    !

    "

    #####

    $

    %

    &&&&&&

    u

    v

    !

    "###

    $

    %&&&&

    The evolution of the perturbed system is completely defined by the eigenvalues of theJacobian matrix

    J(x*, y

    *) =

    df(x*, y

    *) / dx df(x

    *, y

    *) / dy

    dg(x*, y

    *) / dx dg(x

    *, y

    *) / dy

    !

    "

    #####

    $

    %

    &&&&&&

    There are three (basic) cases that are described below.

    22 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    24/32

    The eigenvalues of the Jacobian are negative real numbers, or complex numbers with

    negative real part. In that case (u(t),v(t))!(0,0) as t!$ and the fixed point (0,0) is saidto be stable.

    At least one of the eigenvalues of the Jacobian is positive, or in the case of complex

    eigenvalues at least one has positive real part. Then (u(t),v(t))!($,$) as t!$ andthe fixed point (0,0) is said to be unstable.

    The eigenvalues of the Jacobian are imaginary. In that case the functions u(t) and v(t)

    are periodic and they do not approach zero or infinity as t!$. The fixed point (0,0) is saidto be Lyapunov stable.

    The Jacobian of the van der Pol system is

    J(0, 0) =0 1

    !1

    "

    #

    $$$$

    %

    &

    ''''

    with eigenvalues

    !1,2

    =

    2" 4

    4

    which are positive for ##2, or complex with positive real part for #

  • 8/3/2019 Octave Revision Pack

    25/32

    Lets start the experimental study with the weak damping case, #=0.1, initialdisplacement x(0)=0.5, y(0)=0 and x(0)=3, y(0)=0, and integrate from 0 to 60, that issome periods of the free oscillation T0=2!/%0%6.28.

    ! > x1 = solveVdp ([ 0.5; 0.0 ], 0:0.1:60, 0.1);! > x2 = solveVdp ([ 3.0; 0.0 ], 0:0.1:60, 0.1);! > plot (0:0.1:60, x1(:,1), 'k', 0:0.1:60, x2(:,1), 'm');! > xlabel ('t');! > ylabel ('x(t)');

    The conclusion from that first result is that independently of the initial condition our

    oscillator settles to a constant amplitude (close to 2) oscillation of (almost) the same periodas if there was no damping. What is more interesting to see is the phase space x-y. To plotthe phase space we type

    ! > plot (x1(:,1), x1(:,2), 'k', x2(:,1), x2(:,2), 'm');! > axis tight equal;! > xlabel ('x');! > ylabel ('y');

    24 | FOTIOS KASOLIS 2010-2011

    -3

    -2

    -1

    0

    1

    2

    3

    0 10 20 30 40 50 60

    x(t)

    t

    -2

    -1

    0

    1

    2

    -2 -1 0 1 2 3

    y

    x

  • 8/3/2019 Octave Revision Pack

    26/32

    and the conclusion is of course the same. Both phase trajectories settle to the same closedorbit, which in that case is called limit cycle (since it is isolated). A limit cycle is nothingmore than the geometrical equivalent to periodic motion.

    An even better graphical result could be obtained by plotting the vector field (phaseflow, velocity field) using the quiver function.

    ! > [ x, y ] = meshgrid (-3:0.25:3, -3:0.25:3);! > u = y;! > v = - x - 0.1 * (x.^2 - 1) .* y;! > quiver (x, y, u, v, 'k');! > hold on;! > plot (x1(:,1), x1(:,2), 'k', x2(:,1), x2(:,2), 'm');! > hold off;! > axis tight equal;! > xlabel ('x');! > ylabel ('y');Lets repeat the experiment, but this time change the damping factor from 0.1 to 0.7.

    ! > x1 = solveVdp ([ 0.5; 0.0 ], 0:0.1:60, 0.7);! > x2 = solveVdp ([ 3.0; 0.0 ], 0:0.1:60, 0.7);! > v = - x - 0.7 * (x.^2 - 1) .* y;! > quiver (x, y, u, v, 'k');! > hold on;! > plot (x1(:,1), x1(:,2), 'k', x2(:,1), x2(:,2), 'm');! > hold off;! > axis tight equal;! > xlabel ('x');! > ylabel ('y');

    GNUOCTAVE/MATLAB REVISION PACK | 25

    -3

    -2

    -1

    0

    1

    2

    3

    -3 -2 -1 0 1 2 3

    y

    x

    -3

    -2

    -1

    0

    1

    2

    3

    -3 -2 -1 0 1 2 3

    y

    x

  • 8/3/2019 Octave Revision Pack

    27/32

    10 APPLICATION.CELLULAR AUTOMATA+, -, *, /, ^, pi, i, inf, eps, format, sin, cos, tan, asin, acos, atan, asinh, acosh, exp, log,

    log10, log2, abs, sqrt, sign, round, ceil, floor, fix, max, min, help.

    Brief theoryA cellular automaton is a mathematical machine that consists of cells that get their

    state from a finite set according to a local rule. The rule is said to be local because it onlyuses the neighborhood of each cell as its input. Two commonly used 2d neighborhoods are

    the von Neumann neighborhood

    NN={(0,-1),(-1,0),(0,0),(+1,0),(0,+1)}

    and the Moore neighborhood

    NM=NN{(-1,-1),(-1,+1),(+1,-1),(+1,+1)}

    and their representation is shown in the figure below.

    (-1,+1) (0,+1) (+1,+1)

    (-1,0) (0,0) (+1,0)

    (-1,-1) (0,-1) (+1,-1)

    To model forest fire we allow three different states for each cell. If a cell is empty its

    state is 0, if a cell is burning its state is 1, and if a cell represents a tree its state is 2.Moreover we assume that the array is toroidly connected, meaning that fire which burns to

    the left side of the forest will start fire on the right side and the same is true for the topand bottom sides. To continue we must define the local rules. To do so we choose a von

    Neumann neighborhood and we represent an empty cell with black color, a burning cellwith pink color, and a tree with gray color.

    If one or more of the four neighbors of a cell is in burning state and the cell represents a

    tree (2), then its new state is burning.

    26 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    28/32

    A cell that is burning becomes an empty cell.

    There is a low probability (1%) for an empty cell to become a tree cell.

    There is a very low probability (0.0005%) for a tree cell to start burning because ofnatural cause (like lightning or human irresponsibility).

    Implementation

    We choose the number of cells and the number of iterations to be our experimentalvariables. Keep in mind that the problem should not be programmed with for loops runningthrough the cells, because it will be extremely slow in every (pure) interpreted language forbig problem size (big number of cells). To resolve the looping problem we must combine thematrix manipulation capabilities of Octave together with logicals. Here is an example

    ! > a = round (10 * rand (3, 3))! a =! 6 4 4! 4 4 0! 6 5 1! > a == 4! ans =! 0 1 1! 1 1 0! 0 0 0The function that solves the fire forest problem is shown below.

    Let us first comment on the variable s. Each line checks if there are any burning cells

    and returns a matrix with ones at the burning cells and zeros everywhere else. Moreover,the toroidal symmetry is enforced by the indexing. Consider the matrix

    GNUOCTAVE/MATLAB REVISION PACK | 27

  • 8/3/2019 Octave Revision Pack

    29/32

    1 x x

    x 1 1

    x x x

    !

    "

    ####

    $

    %

    &&&&

    then the variable s will be

    0 1 0

    1 0 1

    0 0 0

    !

    "

    ####

    $

    %

    &&&&

    +

    0 0 1

    1 1 0

    0 0 0

    !

    "

    ####

    $

    %

    &&&&

    +

    0 0 0

    1 0 0

    0 1 1

    !

    "

    ####

    $

    %

    &&&&

    +

    0 1 1

    0 0 0

    1 0 0

    !

    "

    ####

    $

    %

    &&&&

    =

    0 2 2

    3 1 1

    1 1 1

    !

    "

    ####

    $

    %

    &&&&

    that is it contains the number of neighbors in burning state for every cell.

    function [ v ] = caForestFire (n = 100, iter = 100)z = zeros (n, n);v = z;s = z;pl = 0.000005;pg = 0.01;h = imagesc (cat (3, z, z, z));axis equal off;for i = 1:iters = (v(1:n, [ n, 1:n-1 ]) == 1) + ...

    (v(1:n, [ 2:n, 1 ]) == 1) + ...(v([ n, 1:n-1 ], 1:n) == 1) + ...(v([ 2:n, 1 ], 1:n) == 1);

    v = 2*(v == 2) - ...((v == 2) & (s > 0 | (rand (n, n) < pl))) + ...

    2*((v == 0) & rand (n, n) < pg);set (h, 'cdata', cat(3, v == 1, v == 2, z));drawnow

    endendfunction

    Regarding the variable v, its first line checks for tree cells and sets them to 2, meaning

    that it keeps them as tree cells. The second line subtracts 1 if a cell is in tree state and ithas burning neighbors or exists natural cause (first and last rule). The last line applies the

    rule for empty cells to become trees. Notice that burning cells become automatically zerossince v==2 and v==0 return matrices with zeros at the cells v==1.

    28 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    30/32

    PROBLEM SET

    Introduction

    01. Calculate the value of the expression 2/(e2+2/3) and its square without retyping the

    expression.

    02. What is the expected value of the expression 1-sin2(!/2)-cos2(!/2)? Change the

    format of the displayed output to long and calculate its value using Octave. Can youexplain why the computed and expected values are not the same?

    Assignments and variables

    03. Find the distance between the points A(1.1,2.7) and B(7.9,2.2) and assign the resultto the variable d. Use the function normto calculate the same distance.

    04. (a) Set the variable x equal to 1 and suppress the output. (b) Set the variable yequal to 7 using the line continuation operator after the assignment operator. (c) Set thevariable z equal to 3.1 and in the same line set the variablew equal to true.

    Vectors, ranges, and matrices

    05. Let x=[9,8,3,1,0,2,3] and (a) add 2 to the odd indexed elements, (b) compute thesquare root of each element, (c) divide each element with its position index squared.

    06. Write the inner product of two vectors v and w using matrix multiplication. Choosev=[1;0;2] andw=[8;1;1] to check your answer.

    07. Create a vector e with elements given by 1n/n!, where n=0,1,2,...,N is the position

    index, and n!=123...n is the factorial of n (given in Octave by the functionfactorial!). Choose N=10, N=100, and N=1000 and sum up the elements of the vector e.Interpret the result. You must not use a for loop.

    08. Given the matrix A=[2,7,9,7;3,1,5,6;8,1,2,5], explain the results of the following

    instructions. (a) A', (b) A(:,[1 4]), (c) A(:), (d) reshape(A,2,6), and length(A).

    Graphs

    09. Make a good plot of the tan function over the domain [0,4!].

    Loops and conditionals

    10. Given v=[1,5,5,6,7] and w=[2,5,6,6,9], execute and explain the following

    instructions. (a) v>w, (b) v|w, (c) (v==5)&(w!=6).

    11. Evaluate n and m in the following Octave code fragments and use Octave to verify

    your answers.! n = 1; m = 1; if n > 3 m = n + 1; else m = m - 1; end

    GNUOCTAVE/MATLAB REVISION PACK | 29

  • 8/3/2019 Octave Revision Pack

    31/32

    ! n = 4; m = 2; if n > 3 m = n + 1; else m = m - 1; end! n = 3; m = 1; if n * m > 3 m = m^2; else m = m - 1; end! n = 1; m = 4; if n != 4 m = n - 1; n = n - 1; end12. Evaluate the vector x in the following Octave code fragments and use Octave to

    verify your answers.(a) (b) (c)

    n = 1; m = 2;

    for i = 1:10

    if n == m

    x(i) = n^2 + m^2;

    n = m + 1;

    else

    x(i) = n / m;

    n = m - 1;

    end

    end

    n = 1; m = 2;

    for i = 1:10

    if n == m

    x(i) = n^2 + m^2;

    n = m + 1;

    else

    x(i) = n / m;

    n = m;

    end

    end

    n = 1; m = 2;

    for i = 1:10

    if n == m

    x(i) = n^2 + m^2;

    n = m + 1;

    else

    x(i) = n / m;

    n = n * m;

    end

    end

    Scripts and functions

    13. Study the following scripts. In all cases the resulting vector is the same column

    vector. What is the problem with the cases (a) and (b)?

    (a) (b) (c)

    clear x

    tic

    x=[];

    for i = 1:10^5x=[ x; sin(i) ];

    end

    toc

    clear x

    tic

    for i = 1:10^5

    x(i) = sin (i);end

    toc

    clear x

    tic

    x = zeros (10^5,1);

    for i = 1:10^5x(i) = sin (i);

    end

    toc

    14. Write a function that returns a vector with the Fibonacci numbers which are given

    by

    xn

    = xn!1

    + xn!2

    with initial conditions x0=0 and x1=1, and n=0,1,2,...N. Use the number N as user input.

    Execute your function for N=100 and plot the resulting values (vs position index). For thefirst 50 Fibonacci numbers, compute the ratio xn/xn-1.

    15. Write two function that return the roots of the quadratic equation ax2+bx+c=0given a, b, and c. In the first function use the formulas

    x!

    =!b! b

    2! 4ac

    2a, x

    +

    =!b+ b

    2! 4ac

    2a

    and in the second use the equivalent alternatives

    30 | FOTIOS KASOLIS 2010-2011

  • 8/3/2019 Octave Revision Pack

    32/32

    x!

    =2c

    !b+ b2! 4ac

    , x+

    =2c

    !b! b2! 4ac

    Invoke both functions for a=10-5, b=105, and c=-10-5 and compare the solutions with

    respect to accuracy.

    16. Make sure that you know how to use the functions given below each chapter title.Use help to find the syntax of the functions that are not used in the text.

    REFERENCES

    01. Octave documentation: http://www.gnu.org/software/octave/doc/interpreter/.*****

    02. Introduction to Octave. P.J.G. Long, 2005.*****

    03. Computational science and engineering. Gilbert Strang. Wellesley-Cambridge press.2007.

    *****

    04. Applied numerical methods using Matlab. Won Young Yang, Wenwu Cao, Tae-SangChung, John Morris. John Wiley & Sons. 2005.

    *****

    05. Numerical methods in engineering with Matlab. Jaan Kiusalaas. Cambridgeuniversity press. 2005.

    ****

    06. A brief introduction to Scilab. Fotios Kasolis. 2010.****

    07. Essential Matlab for scientists and engineers. Brian D. Hahn. Elsevier. 2002. ***

    08. A brief introduction to Matlab. Fotios Kasolis. 2008.***

    Please contact the author for corrections and commentsfotios kasolis@gmail com