# Using Octave

Last revision: May 11, 2016In this first unit we will discuss our tools. Of course, the really
important part is the *logic* behind numerical computation, but
nonetheless the calculations can not be done on our heads!

Our basic tool will be the octave package. It is free software, a high level interpreted language, very similar to MatLab.

If you do not know what this means exactly, don’t worry. The program is installed in the computer rooms of the university, under linux. You can also install it at home under Windows or Mac, see. It even works under Android.

**Warning:** Learning programming without a computer can be a very
frustrating experience. Seriously,
get a running octave environment before proceeding.

## Starting out with octave

Today’s work is going to be a fast tutorial on octave. You might want
to keep some longer document bookmarked, such
as this one at Jacobs University
or this wikibook. So, get a console and start `$ octave`

,
you will get a new prompt like this:

```
octave:1>
```

We will write always `>`

to mark the Octave prompt. Let’s
start with a simple example:

You have the standard mathematical operators: ```
2+3, 2-3, 2*3,
2/3, 2^3
```

(which means $2^3$), `sqrt(2), exp(2)`

, etc.

Of course, you can assign variables:

Try solving a quadratic equation with the usual formula! For example, $x^2 - 6x + 8=0$

The basic objects in octave are *vectors*. For example, type:

Vectors are just arrays of numbers, of course. Imagine that you want to have in a vector the integers from 1 to 10. You can do it fast, with an octave trick:

Now we can do “things” to all elements. For example, if you type

We’ve taken the sine of all elements of the vector!! This can be quite useful sometimes. Now, let’s take the square of all those elements. Try by yourself before looking at the right way to do it…

So you can see, you have to insert a “dot” before the “^” symbol. The
reason for that is complicated at this stage. The rule is this:
if `x`

and `y`

are vectors of the same
dimension, then `x+y`

and `x-y`

work, and give
you a new vector whose elements are the sums and differences of the
elements of `x`

and `y`

. But in order to get the
*products and quotients* between the elements of `x`

and `y`

, you have to do `x.*y`

and `x./y`

. And also to raise each element
of `x`

to a power: `x.^2`

.

## Summing

Imagine that you want to sum all integers below 100. Your strategy may
be to fill up a vector with all those
numbers: `x=1:100`

. But, when you do that, you realize
that… `Octave`

insists prints them all on the screen!! That’s
a nuisance!! You can prevent the output by adding a *semicolon*
to the command: `x=1:100;`

.

OK, now that the vector `x`

is initialized, how to sum the
values? Easy: just `sum(x)`

. All together:

And you get `ans = 5050`

. Now, let us try a more difficult
exercise. I have been told that

Is that true? OK, I’ll check. The sum is *infinite*, but I will
do a finite one. Let’s try up to 1000. So, we create a vector of all
integers up to 1000: `x=1:1000;`

Now, we compute, in a
second vector, the inverses of their
squares: `y=1./x.^2;`

. Then, we sum the result. All
together:

Well… close enough! You may try a larger number yourself, to check that it really converges to $\pi^2/6$.

## Exercises

The purpose of these exercises is to check your understanding. If you have any difficulties, contact your teacher by e-mail or personally.

- Show, in a single command, the logarithms of the first 10 natural numbers.
- Sum the cubes of all the natural numbers under 20.
- Starting from 1, how many consecutive perfect squares do you have to add up in order to reach one million?