Using OctaveLast revision: May 11, 2016
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!
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
you will get a new prompt like this:
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:
y are vectors of the same
x-y work, and give
you a new vector whose elements are the sums and differences of the
y. But in order to get the
products and quotients between the elements of
y, you have to do
x./y. And also to raise each element
x to a power:
Imagine that you want to sum all integers below 100. Your strategy may
be to fill up a vector with all those
x=1:100. But, when you do that, you realize
Octave insists prints them all on the screen!! That’s
a nuisance!! You can prevent the output by adding a semicolon
to the command:
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
y=1./x.^2;. Then, we sum the result. All
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?