MTH739N/P/U: Topics in Scientific Computing
1
Autumn 2016
Week 1: Basics of scientific programming I
1.1
Introduction
The aim of this course is use computing software platforms to solve scientific and mathematical problems. There are a huge number of options for scientific computing, each
with a set of benefits and challenges. They can be classed into overlapping categories
(note: these categories do not cover all of computing):
Compiler Languages
Interpreted Languages
Interfaces
C, C++
Fortran
Pascal
etc.
Java
Python
Perl
etc.
Mathematica
Maple
R, Matlab, Octave
etc.
Typically, compiled languages have the advantage of faster running programs (this is
always dependent on the quality of the code of course!). Interpreted languages are easier
to learn and use, but are often slower. Programming interfaces, such as Matlab, Octave
or R tend to be the slowest option for computing. However, they offer many additional
benefits for scientific computing:
• graphical output (e.g. plotting, 3D plotting)
• built in functions, often optimised (e.g.. matrix operations, ODE solvers)
• integrated debugging
Therefore, a sensible strategy in scientific computing is to develop algorithms and
functions on a high level platform, then translate to higher speed compiled language, if
and when it is needed (e.g. stochastic simulations of infectious disease processes).
Octave, a free version of Matlab, will be used as the programming language for this
module. Students can also use another programming language (C, Fortran, Python, etc.)
or programming environment (Maple, Mathematica) if they wish to. However, all code
examples and coursework solutions will be provided as Octave/Matlab code.
1
1.2
Variables and basic operations
Variables, mathematical or otherwise, need to be specified and assigned values. In Matlab/Octave this is done as follows:
Command Type
x=2
integer
x = 2.0
float
x = ”text” string
Variables are overwritten with the assignment command ”=”:
x=
x+1
There are several conventions that are commonly used in scientific computing for
variable names. These are needed to make code easier to share between scientists, and
to avoid confusion. Variable names should :
• have no numbers to start
• be capital sensitive (language dependent)
• contain no spaces (underscore is used instead; e.g. no spaces)
Matlab and Octave contain many predefined constants:
π → pi
i → i, j, the imaginary unit
e → e, Euler’s constant
Arithmetic operations are preformed by using common symbols:
[+] addition
[-] subtraction
[*] multiplication
[/] division
[ˆ] exponentiation
They follow the usual conventions, with the order of operations being from left to right:
1 + 2*5 = 11
1/4/2 = 0.125 NOT 0.5!
2
Vectors and matrices
Vectors and matrices are specified as follows:
V
V
= [1 2 3]
- row vector
n - th element: V(n)
= [1; 2; 3]
- column vector
; is the row delimiter
A = [1 2
1
A = 4
7
3;
2
5
8
4
3
6
9
5 6; 7 8 9]
- matrix
The elements of this matrix are accessed as follows:
Aij = A(i,j)
column j : A(:,j)
row i : A(i,:)
submatrix : A([1:2],[1:3])
Operations
Operations on vectors and matrices is similar to operations on variables.
• A * B - matrix multiplication
• u * v - vector multiplication has two types of output: scalar if u row vector, v
column vector; matrix if u column vector, v row vector
• A’ - transpose (also transpose(A))
• A^n - matrix exponent, n-th power
3
1.3
Basic functions
Built in mathematical functions
There’s a huge number of mathematical functions implemented in Octave/Matlab. Here
are just a few:
sin(x)
cos(x)
asin(x) acos(x)
sinh(x) cosh(x)
real(x) imag(x)
tan(x)
atan(x)
tanh(x)
mod(a,b)
exp(x)
log(x),log10(x)
sqrt(x) = xˆ(1/2)
factorial(n)
For the vast majority of built in functions, a description is available by looking at the
help files (e.g. help sin).
In addition, many custom built functions are available that increase the functionality
of the platform. These functions are not only useful for computation and calculation, but
input and output of data, data analysis, visualisation and so on.
But, mathematical problems will require the expansion of the basic functionality of
the platform. In the coursework, and in the project, you will employ built-in functions
as long as they don’t fulfil the required task. In that case, you should write your own
functions!
Function fct(x) where x can be of any type (integer, float, matrix, vector, etc.) can
be defined as follows:
[a,b] = fct(x,y,z ...)
Functions that are computer specific include:
floor(x) - round down to nearest integer
ceil(x) - round up to nearest integer
Functions can be written in such a way that they give different results for different
inputs. For example:
max(x) - highest value in vector x
min(x) - lowest value in vector x
sum(x) - sum of values of vector x
prod(x) - product of values of vector x
In the case of x being a matrix, these function acts on each of the columns of matrix x
in turn and outputs a row vector for each column. This type of multiple output is called
function overloading.
For matrices, simple matrix operations are implemented:
inv(A) - matrix inverse
det(A) - matrix determinant
4
eig(A) - vector of eigenvalues of A
[U,D] = eig(A) - returns U matrix with eigenvectors in columns and D diagonal
matrix with corresponding eigenvalues
Element-wise operations
Most mathematical functions act element-wise. If we have a matrix:
A=
1 2
3 4
!
then
sin(A) =
sin(1) sin(2)
sin(3) sin(4)
!
Element wise operations can be forced onto vectors and matrices:
.* - element-wise product
.^ - element-wise power
./ - element-wise division
+,- - always element-wise
Examples:
A^2 = A.A
!
7 10
=
16 22
1 4
9 16
A.^2 =
y
1./y
1/y
x
x*y’
=
=
=
=
=
!
[1
2]
[1
1/2]
error
[3 5]
13
!
3 6
x’*y =
5 10
x.*y = [3
10]
x*y = error
... and so on.
5
1.4
Plotting
Octave implements simple plotting commands, the most basic of which is plot(x,y).
This plots the points at x,y coordinates. For example if we define an range of x values
as follows:
x =
0.01
[0:0.01:10] - this defines a vector of values from 0 to 10 in increments of
plot(x,sin(x) - this plots sin(x) against x, since sin(x) operates on x in an
element-wise fashion.
plot(x,x.*sin(x) - this plots sin(x) against x sin(x), since sin(x) operates on
x in an element-wise fashion and .* is an element-wise operator.
1
8
6
0.5
4
2
0
0
-2
-0.5
-4
-1
-6
0
2
4
6
8
10
0
2
4
6
8
10
Figure 1: Left panel: x against sin(x). Right panel: x against x*sin(x).
Multiple lines can be plotted on the same plot using plot(x1,y1,x2,y2, ...). The
type of line, colour, symbols, can be specified using commands within plot. For example:
plot(x1,y1,"cmd1",x2,y2, "cmd2" ...), where cmd1 are cmd2 are different. This is
important when specifying a legend. Following the plot command, you can specify a
legend by executing legend("Plot 1","Plot1").
The title of the figure is specified with the command title("text"), and the x and
y labels are specified with the commands xlabel("text") and ylabel("text)".
And finally, the figure can be saved to file using the print command.
For example print -dpdf "name.pdf" produces a file called ”name.pdf” that contains
the figure plotted.
6
1.5
Procedures and functions
It’s cumbersome to type out and execute commands each time you want to perform a
calculation. To make this easier, and repeatable, we use procedures and functions.
Procedure
Commands and functions can be executed in consecutive order, automatically, and saved
in a text file (e.g. in MATLAB or Octave, the file ending will be “.m”).
For the code to be usable
• include comments : % everything after the % sign is a comment and not executed
• variables defined in procedures are global
• can include structured execution (conditional, repeated), and built-in functions
• can be called within other procedures/functions like built-in functions (function
files need to be known to the program)
Functions
Functions are, in essence, procedures that have flexible input and output, using variables.
They follow a predefined structure with a specific syntax.
function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN. This declaration statement must
be the first executable line of the function.
Save the function code in a text file with a .m extension. The name of the file should
match the name of the first function in the file. Valid function names begin with an
alphabetic character, and can contain letters, numbers, or underscores.
You can declare multiple local functions within the same file, or nest functions. If
any function in a file contains a nested function, all functions in the file must use the end
keyword to indicate the end of the function. Otherwise, the end keyword is optional.
• used for mathematical functions x → f (x).
• implement complicated functions using structured execution and / or built-in functions
• variables defined in functions are local
Examples
1. Define a function in a file named mypow.m that accepts a variable, and returns a
single result.
function y = mypow (x)
a = 1 / x;
y = x ^ a;
end
7
Here a is a local variable, whereas y is a returned variable. This can be made listable,
and operate element- wise, by using ./ instead of / and .^ instead of ^.
2. Define a function in a file named average.m that accepts an input vector, calculates
the average of the values, and returns a single result.
function y = average(x)
if ~isvector(x)
error(‘Input must be a vector’)
end
n = length(x);
% n,s are local variables
s = sum(x);
% length(x), sum(x) are built-in functions
y = s/n;
% x can be a scalar or a vector
end
Call the function from the command line as follows, for example:
z = 1:99;
average(z)
ans =
50
3. A multiple input function can be defined as follows:
function norm = mynorm(x,y)
norm = sqrt(x^2 + y^2);
end
% two arguments, x and y
Alternatively, both the values can be included within a vector x
function norm = mynorm(x)
% single argument, x vector
norm = sqrt(x(1)^2 + x(2)^2);
end
8
4. An arbitrary number of function arguments and outputs is possible:
function y = polar(x)
% single argument, x vector
y(1) = sqrt(x(1)^2 + x(2)^2);
y(2) = atan(x(2)/x(1));
% returns y, a vector of length two
end
5. Two variable output:
function [u, y] = rotation(x,theta)
% two arguments, two outputs
u(1) = x(1) * cos(theta) - x(2) * sin(theta);
% rotation
u(2) = x(1) * sin(theta) + x(2) * cos(theta);
y(1) = sqrt(x(1)^2 + x(2)^2);
% polar vector
y(2) = atan(x(2)/x(1));
end
Executing (or calling) this function a = rotation (x,0.5) with x = [1,4] would
return only the first output (rotation).
Calling this function [a,b] = rotation (x, 0.5) returns a (rotated vector) AND
b (polar vector).
6. Matrix eigenvectors:
[U,D] = eig(A)
returns U matrix with eigenvectors in columns and D diagonal matrix with corresponding eigenvalues.
9
© Copyright 2026 Paperzz