Informatics1-2017/Practice9
(→Indexes) |
|||
298. sor: | 298. sor: | ||
A (:) | A (:) | ||
− | == | + | == Vectorization == |
− | + | In Octave we usually work with multiple numbers not just one. | |
− | + | For example let us calculate the norm of every row in the matrix <tt>X</tt>: | |
X = rand (10, 3); | X = rand (10, 3); | ||
sqrt (sum (X.^2, 2)) | sqrt (sum (X.^2, 2)) | ||
314. sor: | 314. sor: | ||
1.01153 | 1.01153 | ||
1.07420 | 1.07420 | ||
− | + | Let us look at the functions: | |
− | * <tt>X.^2</tt>: | + | * <tt>X.^2</tt>: calculates the square of the items |
− | * <tt>sum(●, 2)</tt>: | + | * <tt>sum(●, 2)</tt>: sums up the values into a column vector (the meaning of the 2 is to sum up along the second dimension) |
− | * <tt>sqrt</tt>: | + | * <tt>sqrt</tt>: square root of every item |
− | + | Calculate the function value <tt>2x^2-3x+1</tt> where <tt>x</tt> is a row vector: | |
x=0:0.1:1; | x=0:0.1:1; | ||
fx=2.*x.^2 - 3.*x + 1 | fx=2.*x.^2 - 3.*x + 1 | ||
− | + | == Functions == | |
− | + | Try the following (multiple lines)! | |
− | == | + | |
− | + | ||
> function fx = f (x) | > function fx = f (x) | ||
> fx = 1 / (x^2 + 1); | > fx = 1 / (x^2 + 1); | ||
> endfunction | > endfunction | ||
− | + | Test it: | |
> f(3) | > f(3) | ||
ans = 0.10000 | ans = 0.10000 | ||
− | + | Defining functions: | |
− | function << | + | function <<result>> = <<function name>> (<<variables>>) |
... | ... | ||
endfunction | endfunction | ||
− | + | Any command can be inside the function, but at the end the <tt><<result>></tt> variable has to contain the result of the function. We usually use silent calculation ('''<tt>;</tt>''') inside the function. | |
− | + | Another function: | |
function R = remove_last (x) | function R = remove_last (x) | ||
R = x (1:end-1); | R = x (1:end-1); | ||
endfunction | endfunction | ||
− | + | Example: | |
> remove_last (1:5) | > remove_last (1:5) | ||
ans = | ans = | ||
1 2 3 4 | 1 2 3 4 | ||
− | == | + | == Tasks == |
=== Elemi sorműveletek elvégzése === | === Elemi sorműveletek elvégzése === |
A lap 2017. november 6., 07:06-kori változata
Tartalomjegyzék |
Octave
Octave is a numerical calculator, its the free (opensource) version of MatLab.
First steps
Getting the program
Use one of the following from home:
- install Octave, free for all platforms
- using putty log in to leibniz, start octave with the octave command
- run octave from the computer lab
Calculator
Octave can be used as an advanced calculator. Try the following (after entering octave):
2 + 3
press Enter:
> 2 + 3 ans = 5
Try these as well:
2 - 3 2 * 3 2 / 3 floor (2 / 3) mod (2, 3) 2^3 sqrt (2) log (2) log (3) log (8) / log (2) exp (1) pi cos (pi / 2) (180 / pi) * acos (0.5)
This is how you quit
exit
Data types
Every number is a floating point number, even if it happens to be an integer:
1000 / 9 ans = 111.11
We can however force it to use integers:
int32 (1000) / int32 (9) ans = 111
In Octave every number is real as long as it does not turn out to be complex:
sqrt (2) sqrt (-2)
Number representation
- double: double precision float, 64 bit (8 byte)
- real: 8 byte
- complex: 16 byte
- single: single precision float, 32 bit (4 byte)
- real: 4 byte
- complex: 8 byte
- int32: 32 bites two's complement integer (4 byte)
- int8: 8 bites two's complement integer -128..127 (1 byte)
- uint32: 32 bites unsigned integer (4 byte)
- uint8: 8 bites unsigned integer: 0..255 (1 byte)
Size does matter:
log(single(1.0001)) log(double(1.0001)) int32(100+100) int8(100+100)
Matrices
In octave every number is a matrix
- numbers: 1x1
- vectors:
- row vector: 1xn
- column vector: nx1
- matrix: nxm
Row vector:
[1 2 3 4] [1, 2, 3, 4]
Column vector:
[1; 2; 3; 4]
This is not a column vector:
[[1], [2], [3], [4]]
Matrix:
[1 2; 3 4] [1, 2; 3, 4]
Special matrices:
- zeros: all 0
- ones: all 1
- eye: 1 in diagonal, 0 elsewhere
- diag: square diagonal, with the speciafied diagonal
zeros (2, 3) eye (2, 3) ones (3, 1) diag ([1, 2, 3, 4])
Also try:
size (5) size ([1, 2, 3]) size ([1; 2; 3])
Ranges
Try the following:
1:10
We can specify a step as well:
1:0.1:2 1:2:10
Does not work for complex numbers, since there's no order on complex numbers!
The result will always be double but we can convert it:
int32 (1:0.5:10)
Decreasing range:
4:-1:1
Empty range:
4:1:1 4:1
Diagonal matrix:
> diag(1:4) ans = 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4
typeinfo
You can find out the type of an item with typeinfo.
typeinfo (1) typeinfo (int32(1)) typeinfo (i) typeinfo ([1 2 3 4]) typeinfo ([1 i -1 -i]) typeinfo (1:4) typeinfo ([1:4]) typeinfo (1 >= 0) typeinfo ("szöveg") typeinfo ([1 + 2i 1 - 1i])
Operations with matrices
Since every number is a 1x1 matrix, this can be applied to numbers as well.
Transpose
Use an accent ('):
> [1 2; 3 4]' ans = 1 3 2 4 > _
Or
> (1:4)' ans = 1 2 3 4
For complex matrices the accent means andjugate
in other words, the conjugate of the transpose: > [1 2i; 3i 4]' ans = 1 - 0i 0 - 3i 0 - 2i 4 - 0i
Conjugate: (2 + 1i)'
Addition
1 + (1:4) eye (2, 2) + ones (2, 2) [1; 2; 3; 4] - [4; 3; 2; 1]
Multiplication
Every multiplication is matrix multiplication:
> [1 2; 3 4]*[1 2; 3 4] ans = 7 10 15 22
Power as well, so is the inverse:
[1 2; 3 4]^2 [1 2; 3 4]^-1
For multiplications the dimensions must be compatible:
ones (2, 3) * ones (3, 5)
Multiplying a row vector and a column vector results in a scalar, while the reverse is a dyiadic multiplication:
[1,2,3]*[1;2;3] [1;2;3]*[1,2,3]
Every element or the whole
If power is a matrix operator, then what is this?
[1 2; 3 4]^0.5
And this?
sqrt([1 2; 3 4])
Some operators apply to every element of a matrix, while others apply to the matrix itself.
> (1:4)^2 error: for A^b, A must be a square matrix
This results in an error since multiplying a 1x4 matrix with itself is not possible. But:
> (1:4).^2 ans = 1 4 9 16
Putting a dot in front of an operator makes it apply to every element of a matrix instead of apply to the matrix itself. For addition this does not matter.
> [1 2; 3 4] + [1 2; 3 4] ans = 2 4 6 8 > [1 2; 3 4] .+ [1 2; 3 4] ans = 2 4 6 8
But for multiplication it matters:
> [1 2; 3 4] * [1 2; 3 4] ans = 7 10 15 22 > [1 2; 3 4] .* [1 2; 3 4] ans = 1 4 9 16
Power:
> [1 2; 3 4]^-1 ans = -2.00000 1.00000 1.50000 -0.50000 > [1 2; 3 4].^-1 ans = 1.00000 0.50000 0.33333 0.25000
Elementry functions apply to every element usually:
sin (0:0.1:2*pi) exp ([0,-1;1,0])
While elementry operators apply to the matrix (*, ^, /, \)
Variables
We use variables to store values.
a = 2 b = 3 a + b
The variable ans always exists. It stores the last calculated value.
If a variable doesn't exist we can't use it in a calculation:
> a / q error: `q' undefined
Using a semicolon (;) we can do silent calculations:
a = 2; b = 3; a + b
The whos commands shows the currently stored variables.
> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== a 1x1 8 double ans 1x1 8 double b 1x1 8 double Total is 3 elements using 24 bytes
Variables can be overwritten anytime:
> a = 2; > a = [1 2; 3 4]; > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== a 2x2 32 double
Indexes
Let M be a 3x3 matrix. The jth element of the ith row of this matrix is:
M = rand(3,3); i = 1; j = 3; M(i,j)
Concatenating matrices:
[M M] [M; M]
Partial series:
l = 0:0.1:1; l (:) l (1:11) l (1:5) l (5:end) l (1:2:11)
Also:
l (1:2:11) = 0
Part of a matrix:
A = [1 2 3; 4 5 6; 7 8 9]; A (1:3, 1:2) A (1:2, 1:3)
Skipping one line:
A ([1, 3], :)
Or selecting a specific part of a matrix:
S = ones (8, 8); S (3:6, 3:6) = -1
Or a given index:
S = ones (8, 8); S ([1 2 8], [2 6]) = 8
Flattening a matrix:
A = eye (3, 3); A (:)
Vectorization
In Octave we usually work with multiple numbers not just one. For example let us calculate the norm of every row in the matrix X:
X = rand (10, 3); sqrt (sum (X.^2, 2)) ans = 0.99105 0.86977 1.29362 0.91697 1.26149 0.84024 1.45410 1.19791 1.01153 1.07420
Let us look at the functions:
- X.^2: calculates the square of the items
- sum(●, 2): sums up the values into a column vector (the meaning of the 2 is to sum up along the second dimension)
- sqrt: square root of every item
Calculate the function value 2x^2-3x+1 where x is a row vector:
x=0:0.1:1; fx=2.*x.^2 - 3.*x + 1
Functions
Try the following (multiple lines)!
> function fx = f (x) > fx = 1 / (x^2 + 1); > endfunction
Test it:
> f(3) ans = 0.10000
Defining functions:
function <<result>> = <<function name>> (<<variables>>) ... endfunction
Any command can be inside the function, but at the end the <<result>> variable has to contain the result of the function. We usually use silent calculation (;) inside the function.
Another function:
function R = remove_last (x) R = x (1:end-1); endfunction
Example:
> remove_last (1:5) ans = 1 2 3 4
Tasks
Elemi sorműveletek elvégzése
Hozzunk létre egy egyjegyű nemnegatív egészekből álló 4x5-ös mátrixot, majd cseréljük ki két sorát, szorozzuk be a második sorát 2-vel és adjuk az első sorának kétszeresét a harmadik sorhoz! (A mátrix redukált lépcsős alakjának meghatározására azért a rref függvényt használjuk!)
Mi ez?
Figyeljük meg a következőket.
A=[1 2 3; 4 5 6; 7 8 9]; B=[9 8 7; 6 5 4; 3 2 1]; trace (A*B') A(:)' * B(:)
Mi a trace(A*B')?
LER
Számoljuk ki a következő lineáris egyenletrendszer megoldását:
x + 2y = 3 4x + 5y = 6
1. megoldás:
A = [1 2; 4 5] b = [3; 6] x = A^-1 * b
2. megoldás:
x = A \ b
Még LER
Oldjuk meg a következő egyenletrendszereket:
x + 5y = 1 2x + 4y = 2
x + 5y = 1 2x + 4y = 2 5x - 6y = -1
x + 2y + 5z = 1 5x + 4y + 6z = 2
Nagy mátrix okosan
- Készítsük el a következő mátrixot okosan! (Minél kevesebb karaktert használva.)
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 2 2 2 2 2 2 5 2 2 2 2 2 2 2 2 2 2 6 2 2 2 2 2 2 2 2 2 2 7 2 2 2 2 2 2 2 2 2 2 8 2 2 2 2 2 2 2 2 2 2 9 2 2 2 2 2 2 2 2 2 2 10
- És most ezt:
0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0
- Sakktáblaszabály
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1
Függvény mátrixokon
Írjunk függvényt, mely az adott mátrix minden elemére alkalmazza a 2sin2x + 1 függvényt.
Részmátrix
Írjunk függvényt, mely egy 5x5-ös mátrix 2. és 4. sorából és 1., 3. és 5. oszlopából álló mátrixot adja.
Részmátrixon függvény
Írjuk meg az elõzõ két függvény kombinációját, mely az adott mátrix 2. és 4. sorából és 1., 3. és 5. oszlopából álló mátrixon alkalmazza a 2sin2x + 1 függvényt.
Minden második oszlop
Írjunk függvényt, mely tetszőleges mátrix minden második oszlopából álló mátrixot adja vissza. (Segítség, a size sorvektorban megadja a mátrix dimenzióját.)
Függvény alkalmazás csak adott elemeken
Írjunk függvényt, mely a kapott mátrix csak minden második oszlopán hajtja végre a 2sin2x + 1 függvényt. (Az eredmény mátrix dimenziója ugyanaz, mint a kapott mátrix.)
Segíts magadon
Az octave dokumentációjában, a stackoverflow-n vagy a Google-n. Az elemi szintű dolgok általában a Matlab help segítségével is megkereshetők.
Numerikus deriválás
Deriváljuk az f(x)=2x^2-3x+1 függvényt numerikusan! Adott egy x sorvektor, ami az abszcissza értékeket tartalmazza, fx pedig a hozzájuk tartozó függvényértékeket.
x = 0:0.1:1 fx = 2.*x.^2 - 3.*x + 1
Ekkor a függvény numerikus deriváltja:
df = (fx(2:end) - fx(1:end-1)) ./ 0.1
Nem egyenletes lépésközzel pedig:
df = (fx(2:end) - fx(1:end-1)) ./ (x(2:end) - x(1:end-1))