Sie sind auf Seite 1von 25

Introduction

Linear Algebra and Geometry

SCIENTIFIC COMPUTING

teacher
Davide Fransos
davide.fransos@polito.it

Lecture n° 1

Preliminaries
Scientific computing deals with methods we use to obtain a numerical
solution for mathematical problems whose analytical solutions is far too
complex.

Ex: solve a 1000 linear equation system with 1000 unknowns using pen and
paper and a method you remember from high school. Ok?

A numerical solution is a solution we can obtain by following a set of precisely


described operations. We call this set an algorithm and usually want to
execute the operations contained in the algorithm on a computer.

We will see in the following:


1. How to represent numbers on a computer and how to operate on them
2. How to make the computer execute operations through programming them
in a proper language (MATLAB in this course)
3. How to pose and solve some typical problems arising from linear algebra
and geometry

Scientific computing
Preliminaries
An example:
Computational

simulation

Mathematical model

Model solution
(through scientific computing)

Why is SC important?
Preliminaries
Computational simulation, and though scientific computing, has application in every
field of engineering.

Automotive Civil Electronics Communications

… and much more …

We will stop a bit earlier.

Why is SC important?
Preliminaries
Course The course will explore four classical areas of scientific
content computing:

Machine numbers and operations

Approximation of functions and data

Linear systems

Eigenvalues
and an exercise part devoted to scientific programming:

Matlab® (S rooms on thursdays)

The topics will be covered in a similar way and with a similar language as in:
A. Quarteroni, F: Saleri, P. Gervasio, Scientific Computing with MATLAB and
Octave, Springer

Please note that course slides are just a support to the oral presentation of the
Preliminaries topics during lectures, not a textbook.
Machine numbers and operations
We want our computer execute algebraic operations 

(in the applications we mentioned before they are many, we
often talk about billions of algebraic operations to be
executed in few days or hours)

Easy! You just have to write numbers in the computer, e.g.

π = 3,14159 26535 89793 23846 26433 83279


50288 41971 69399 37510 58209 74944 59230
78164 06286 20899 86280 34825 34211 70679…

? mmm, how do I memorise infinite digits in my 500 Gb hard disk?

We’ll se we have to:


1. find a way to represent integer and real numbers in a finite memory
2. Understand which error we make when we should use π and we use one of
its finite representations in its place
3. define how to execute algebraic operations on this representation

Course content
Preliminaries
Approximation of functions and data
A man who really wanted to drink his coffee at a
precise temperature measured that a coffee who gets
out from the coffee machine at 50° and is put into a
ceramic cup at room temperature (16°) colds down
following the scheme:

time [s] temperature T [°C]


0 50
620 44
2266 32
3482 26

? this fanatic wants to drink this coffee absolutely at 39°C! 



How much does he have to wait?

Course content
Preliminaries
Approximation of functions and data

Let’s represent it graphically:

60
dati misurati
55

50 ? a vs b ?

Temperatura [°C] 45

40 39
35

30
a
25
b

20
0 500 1000 1500 2000 2500 3000 3500
tempo [s]

Course content
Preliminaries
Approximation of functions and data

A closer look

40

error for 
 error for 



choice b choice a
39.5
Temperatura [°C]

39
a
b
reality
38.5

38
1200 1250 1300 1350
tempo [s]

Course content
Preliminaries
Linear systems

Alcoholic fermentation:

a(C6H12O6) + b(O2) + c(NH3) → C6H10NO3 + d(H2O) + e(CO2) + f(C2H6O)

glucose yeast ethanol


But we have :
1. balance in C, H, O e N number of atoms on both sides
2. the ratio R = CO2/O2 = e/b is measured by a sensor
3. the produced yeast Y=144/180a is measured by a sensor

Course content
Preliminaries
Linear systems
a(C6H12O6) + b(O2) + c(NH3) → C6H10NO3 + d(H2O) + e(CO2) + f(C2H6O)

6a = 6 + e + 2f
6a + 2b = 3 + d + 2e + f ⎧ −e − 2 f = 6 − 6 (144 /180Y )
c=1 ⎪
⎪ 2b − d − 2e − f = 3 − 6 (144 /180Y )
12a + 3c = 10 + 2d + 6f ⎨
Rb - e = 0 ⎪ −2d − 6 f = 7 − 12 (144 /180Y )

a = 144/180Y ⎩ Rb − e = 0
what if in place
? of 4x4 we had 

10000x10000 ?
⎡ 0 0 −1 −2 ⎤⎡ b ⎤ ⎡ 6 − 6 (144 /180Y ) ⎤
⎢ ⎥⎢ ⎥ ⎢ ⎥
and then ⎢ 2 −1 −2 −1 ⎥⎢
d ⎥ = ⎢ 3 − 6 (144 /180Y ) ⎥
⎥ ⎢ 7 − 12 (144 /180Y ) ⎥

⎢⎣
0 −2 0 −6
R 0 −1 0
⎥⎢
⎥⎦ ⎢

e
f ⎥ ⎢
⎦ ⎢⎣ 0


? how to solve it?

A x b
Ax = b
(matrix) (vector) (vector)

Course content
Preliminaries
First steps in Matlab
⎡ 0 0 −1 −2 ⎤⎡ b ⎤ ⎡ 6 − 6 (144 /180Y ) ⎤
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ 2 −1 −2 −1 ⎥⎢
d ⎥ = ⎢ 3 − 6 (144 /180Y ) ⎥
⎢ 0 −2 0 −6 ⎥⎢ e ⎥ ⎢ 7 − 12 (144 /180Y ) ⎥
⎢⎣ R 0 −1 0 ⎥⎦ ⎢ f ⎥ ⎢ ⎥
⎣ ⎦ ⎢⎣ 0 ⎥

A x b
% memorise a number in Matlab
% measured by the sensor
intuitive % measured by the sensor
no need to % memorise a vector in Matlab
declare
variables
% memorise a matrix in Matlab

% solve a linear system in Matlab


? magic? b
d
% show the result e
f

Course content
Preliminaries
Machine A common problem: thinking that operations executed by a
computer are always correct.
numbers and
operations See http://ta.twi.tudelft.nl/users/vuik/wi211/disasters.html
and look at some examples of how this can be false!

One between the others:


On June 4, 1996 an unmanned Ariane 5 rocket launched by
the European Space Agency exploded just forty seconds
after lift-off: $500 million lost in a fraction of a second!

Cause of the failure:


error in the storage
of a single number
in a wrong format! 

(we’ll see how to
store numbers in
few moments)

Machine numbers
Less spectacular but…

Let’s create the first 30 elements of the sequence:


n− 1 2
x1 = 1, x2 = 2, xn+1 = 2 1− 1− 41−n xn2
The sequence converges to 𝜋, but we obtain:

n xn n xn
01 1.00000000 15 3.14159263
02 2.00000000 …
03 2.82842712 25 3.14245127
04 3.06146746 …
05 3.12144515 29 3.46410162
06 3.13654849 30 4.00000000

What happened ?

Machine numbers
and operations Machine numbers
Number systems
Storing
numbers on a We are used to represent numbers with the 0 .. 9 digits,
but this can be sometimes non optimal.
computer

It is difficult to find objects who can stay in 10 different


quite distinct physical stases in nature…

Much easier to find object with two distinct states:


- on / off
- magnetised / non magnetised (e.g. HDD)
- conductive / non conductive (e.g. SSD)
- …

!!!

Machine numbers 0, 1 (“bit”): binary system


Representing numbers in the decimal or binary system ?

Decimal system:

43.12 = 4 ⋅10 + 3⋅10 + 1⋅10 + 2 ⋅10


1 0 −1 −2
notation: ( 43.12 )10

Binary system:

1111 = 1⋅ 2 + 1⋅ 2 + 1⋅ 2 + 1⋅ 2
3 2 1 0
notation: (15 )10 = (1111)2

More in general → base N system:

( a )N = ± ( am am−1 …a1a0 .a−1a−2 …)N =


= ± ( am ⋅ N + am−1 ⋅ N
m m−1
+…+ a1 ⋅ N + a0 ⋅ N + a−1 ⋅ N + a−2 ⋅ N +…)
1 0 −1 −2

where ai are integers such that 0 ≤ ai ≤ N − 1

Storing numbers on
a computer Machine numbers
Representing Finite memory!
numbers on a
computer
We have to choose a maximum number
of digits we can use to represent
numbers

The numbers for which that number of digits is sufficient are


exactly represented. They are called machine numbers.

Example: INTEGERS

l digits memory cell

we can only represent integers who have 



l digits in the chosen N base at most
Machine numbers
Example: INTEGERS (continues)

Matlab’s int32 data type

base N = 2, 32 bit

with 32 digits in base 2 you can represent the integers


⎛ ⎞ ⎛ ⎞
from ⎜ 0…0
! ⎟ =0 to ⎜ 1…1
! ⎟ = 1⋅ 2 31
+ 1⋅ 2 30
+…+ 1⋅ 21
+ 1⋅ 2 0
= 2 32
−1
⎝ 32 ⎠ 2 ⎝ 32 ⎠ 2

In the standard representation you want to include in fact also the negative integers. 

You can then translate the available integers in order to centre them on the zero:

store 0 ≤ q* ≤ 2 32 − 1 but work with q = q * −2 31 , − 2 31 ≤ q ≤ 2 31 − 1

Representing numbers on
a computer Machine numbers
From Matlab documentation

Working with int32:


• 231-1 is a machine number (greatest representable integer)
• 231 IS NOT a machine number - 

Matlab will represent it as 231-1 with the introduction of an error!

Representing numbers on
a computer Machine numbers
Example: REAL numbers - floating point representation
integer

(exponent)
a = pN q

real number chosen base

real number

(mantissa)

not unique… a = ( 0.023)10 = 0.0023⋅101 = 0.23⋅10 −1 = 23⋅10 −3


is uniquely determined if we choose

−1
normalised
N ≤ p ≤1 floating-point 

representation
that is equivalent to
p = 0. a−1 …
always zero! different from zero!

Representing numbers on
a computer Machine numbers
Exercise

Given the numbers


a = 9.785 ⋅10 −3 , b = 9.763⋅10 −2
find their normalised floating-point representation

a = .9785 ⋅10 −2 , b = .9763⋅10 −1

Representing numbers on
a computer Machine numbers
Storing a = pN q in a computer means:

• representing a in the binary system (N=2)


• assigning a finite number of bits to the storage of p and q

1 8 23 IEEE standard 32 bit


(1 11 52 IEEE standard 64 bit )
s q* |p|

sign of a
normalised exponent normalised mantissa
0 ≤ q* ≤ 255 p = 0.1a−2 …a−23a−24
q* = 0 ⇒ a = 0
q* = 255 ⇒ a = ∞, −1,… as N=2 it will always be
1, then I can avoid its
−126 ≤ q ≤ 127, q = q * −127 storage
(false if N>2!)

a = ( −1) ⋅ p ⋅ 2
s q*−127
We'll have

Representing numbers on
a computer Machine numbers
a = ( −1) ⋅ p ⋅ 2
s q*−127

From Matlab documentation:

32 bit !

In Matlab: help single, help double

Representing numbers on
a computer Machine numbers
Examples (base N=10, let’s keep it simple)

N=10 base
t=5 digits for the mantissa
m=-126 minimum exponent
M=127 maximum exponent

a = 1.43256 ⋅10 −3 = .143256 ⋅10 −2 is not a machine number


6 digits for the mantissa!

a = 1.432 ⋅10 −129 = .1432 ⋅10 −128 is not a machine number


exponent < m → underflow

a = 1.432 ⋅10 130


= .1432 ⋅10 131
is not a machine number
exponent > M → overflow

a = 1.4325 ⋅10 −22 = .14325 ⋅10 −21 is a machine number

Representing numbers on
a computer Machine numbers
When two numbers are machine numbers and the results of an operation involving
them is again a machine number, is everything ok?

M1 + M 2
M= where M2 is the largest possible machine number and M1 is smaller
2
M is then a machine number but:

1. if you compute M 1 + M 2 and then divide by two, you obtain OVERFLOW!


2. if you compute M 1 / 2, M 2 / 2 and then sum you get the correct result!
⎧ 2
Solution: ⎪ x ⎛ x2 ⎞
R = x12 + x22 : 

⎪ 1 1+ ⎜⎝ x ⎟⎠ , if x1 > x2
Same for
⎪ 1
we can have for instance x12 too big! R=⎨
⎪ ⎛ x1 ⎞
2

⎪ x2 ⎜ ⎟ + 1, if x2 > x1
⎪⎩ ⎝ x2 ⎠

Representing numbers on
a computer Machine numbers

Das könnte Ihnen auch gefallen