Tutorial on Numerical Computation doing maths with computer

Using Octave

In 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:


We will write always > to mark the Octave prompt. Let’s start with a simple example:

> 2 + 3
ans = 5

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:

> a = 2
> b = 3
> a * b
ans = 6

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

> a = 1
> b = -6
> c = 8
> x1 = (-b+sqrt(b^2-4*a*c))/(2*a)
ans = 2
> x2 = (-b-sqrt(b^2-4*a*c))/(2*a)
ans = 4

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

> v = [2 3 4]
> w = [5 6 7]
> v + w
ans = 7 9 11

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:

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

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

> sin(x)
ans =

   0.84147   0.90930   0.14112  -0.75680  -0.95892  
  -0.27942   0.65699   0.98936   0.41212  -0.54402

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…

> x.^2
ans = 1 4 9 16 25 36 49 64 81 100

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.


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:

> x = 1:100;
> sum(x)
ans = 5050

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:

> x = 1:1000;
> y = 1./x.^2;
> sum(y)
ans = 1.6439

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


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?