Commands, strings, loops and logic

There is a complete documentation of Octave at http://www.gnu.org/software/octave/doc/interpreter/

Formatting numbers

You can format numbers in three different ways; format long, format short and format bank (2 decimals)

>>> format long
>>> pi
ans = 3.14159265358979
>>> format short
>>> pi
ans = 3.1416
>>> format bank
>>> pi
ans = 3.14 

Just writing the command format will give you the default format short.

String variables

When handling text instead of numbers you use strings. A string is enclosed in either "-signs or '-signs.

>>> my_string_variable="Hello World!"
my_string_variable = Hello World!
>>> my_string_variable='Hello World!'
my_string_variable = Hello World! 

The help command

If you want to find information about a command or a function you can use the help command.

>>> help asin
`asin' is a built-in function

-- Mapping Function: asin (X)

Compute the inverse sine in radians for each element of X.

See also: sin, asind

The disp command

You can use the disp command together with an argument enclosed in brackets to display data. When using disp the output is displayed without the ans=.

>>> disp(pi)
3.1416
>>> disp(my_string_variable)
Hello World!
>>> disp("The value of pi is:"), disp(pi)
The value of pi is:
3.1416 

Date and time

When measuring time on a computer it is a convention to measure the time since 00:00:00 UTC 1 January 1970. The command time will give you the number of seconds since then; the command now will give you the number of days. The command date will give you the current date as a string. The command clock will give you the current year-month-day-hour-minute-second as a row-matrix.

>>> time
ans = 1.2737e+009
>>> now
ans = 7.3427e+005
>>> date
ans = 12-May-2010
>>> clock
ans =

2010.0000 5.0000 12.0000 15.0000 41.0000 40.3795

m-files

You can store a number of commands in a so called m-file. You enter the commands in an editor and run the m-file from Octave.

Working directory

All m-files should be stored in the so called working directory. Write pwd (print working directory) to see the current working directory of Octave. If you prefer to store your files at some other directory, go there by using the command cd (change directory). For more information about cd see http://www.computerhope.com/unix/ucd.htm.

Write your m-files in any editor that can handle pure text without adding file-extensions such as .txt. Some examples are Notepad++ for Windows, Sublime Text 2 for Mac (install from App Store), and gedit for Linux. Save the m-files in the working directory of Octave.

A first m-file

Save following lines:

a=1;
b=2;
disp('a+b=')
disp(a+b)

in a file called test1.m in your Octave working directory. Write test1 in Octave to see the output.

The for-statement

Repeating the same operation over and over again is called iterating. One way to iterate when programming is to use a for statement. A for statement in its simplest form uses a variable representing an index that is increased by 1 in each step.

In the code below, the index is called i. The command between for and end is repeated 10 times. The first time row 2 is executed, i is equal to 0, the second time i is equal to 1, and so on.

Row 4 starts with a %-sign, everything to the right of the %-sign is ignored when the program is run; this is a comment intended for those reading the actual code.

for i=0:9
    disp(i)
end
%the numbers 0, 1,...,9 are displayed

You can increment your index with something else than 1 by adding a number between the starting index and the last index.

for i=0:0.1:2
    disp(i)
end
%21 numbers are displayed, the numbers 0,0.1,0.2,...,2

Sequences, Continued

Exercise 1

Write for-statements in m-files to solve following exercises. Use format long.

  1. The recurrence equation \[ \left\{ \begin{align} a_0 &=c \\ a_{n+1} &=1+\frac{1}{a_n}, n\geq0 \end{align} \right. \] has two fixpoints \(x_1=\dfrac{1+\sqrt{5}}{2}\) and \(x_2=\dfrac{1-\sqrt{5}}{2}\).
    Write a for-statement to iterate the recurrence equation 10 times.
    What happens if you start with \(c=x_1\) and \(c=x_2\) respectively?
    What happens if you iterate 50 times? 100 times?
  2. Write a recurrence equation for the continued fraction \[ 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2+\cdots } } }} \] Hint: it is easier to write a recurrence equation for the continued fraction plus 1.
    1. Find the limit of the recurrence equation by iterating in Octave.
    2. Find the exact value of the continued fraction by first finding the fixpoints of the recurrence equation you used in a and then subtracting 1.

Some series

A series \(\sum a_i\) is said to converge if \(\sum_{i=0}^{n}a_i\) has a finite limit as \(n\rightarrow\infty\).

Exercise 2

  1. Use Octave to make a conjecture about the convergence of \[ \sum_{i=1}^{n}\left(\frac{1}{i}\right)^2 \] as \(n\rightarrow\infty\).
  2. Use Octave to make a conjecture about the convergence of \[ \sum_{i=1}^{n}\frac{1}{i} \] as \(n\rightarrow\infty\).
    1. Rewrite the series \[ 1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+\frac{1}{5}-\frac{1}{6}+\cdots \] using a \(\sum\).
    2. Use Octave to make a conjecture about the convergence of this series as \(n\rightarrow\infty\).
  3. Iterate to find the sum of the first 50 terms of \[ 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\cdots \] then multiply the result with 4. What happens when you iterate 500 times? 5000 times? 50000 times?

Boolean Logic

Boolean algebra

In Boolean algebra you represent the logical values true and false by the numbers 1 and 0 respectively.

>>> true
ans = 1
>>> false
ans = 0

and, or, not

The basic operators in logic are and, or and not, these are written using the symbols ∧, ∨ and ¬ respectively. If p and q are statements that are either true or false, then you get the truth table

p q pq pq ¬p
true true true true false
true false false true false
false true false true true
false false false false true

The operators ∧ and ∨ are binary operators, they are applied to two operands. The operator ¬ is an unary operator, it applies to one operand.

In Octave (and most other programming languages) the operators ∧, ∨ and ¬ are written using the symbols &&, || and !; giving the truth table

p q p && q p || q !p
1 1 1 1 0
1 0 0 1 0
0 1 0 1 1
0 0 0 0 1

In Octave (and most other programming languages) all numbers that are not 0 are thought of as being true.

>>> a=4.5;
>>> b=0;
>>> (a && b) || !b
ans = 1

Note that you can perform logical operations by using arithmetics.

p && q = pq

p || q = p+q-pq

!p = 1-p

Comparison Operators

When doing a comparison in Octave you apply a comparison operator on two numbers and the result is either true or false, represented by 1 or 0.

operation operators operands result
arithmetic operations + - * / ^ numbers a number
logical operations && || ! logical values a logical value
comparisons > >= < <= == != numbers a logical value

The comparison operators are:

operator explanation
> greater than, >
>= greater than or equal to,
< less than, <
<= less than or equal to,
== equal to, =
!= not equal to,
>>> a=1; b=2; c=2; d=3;
>>> a>=b
ans = 0
>>> b>=c
ans = 1
>>> (a < b) && (c!=d)
ans = 1

>>> a < b && c!=d
>>>parse error:

syntax error

>>> a < b && c!=d
^

further info:

Harmonic series
Madhava-Leibniz series

Fuzzy Logics: This article was published in Scientific American 1993, A Partly True Story, by Ian Stewart

That is true →      ← That is false

by Malin Christersson under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Sweden License

www.malinc.se