Informatics1-2017/Practice9
(Új oldal, tartalma: „= Octave = [http://www.gnu.org/software/octave/ Octave] is a numerical calculator, its the free ([https://en.wikipedia.org/wiki/Open_source opensource]) version of [h…”) |
|||
65. sor: | 65. sor: | ||
=== Matrices === | === Matrices === | ||
− | + | In <tt>octave</tt> '''every number is a matrix''' | |
− | * | + | * numbers: <tt>1x1</tt> |
− | * | + | * vectors: |
− | ** | + | ** row vector: <tt>1xn</tt> |
− | ** | + | ** column vector: <tt>nx1</tt> |
* matrix: <tt>nxm</tt> | * matrix: <tt>nxm</tt> | ||
− | + | [https://en.wikipedia.org/wiki/MATLAB#History About]. | |
− | + | Row vector: | |
[1 2 3 4] | [1 2 3 4] | ||
[1, 2, 3, 4] | [1, 2, 3, 4] | ||
− | + | Column vector: | |
[1; 2; 3; 4] | [1; 2; 3; 4] | ||
− | + | This is not a column vector: | |
[[1], [2], [3], [4]] | [[1], [2], [3], [4]] | ||
− | + | Matrix: | |
[1 2; 3 4] | [1 2; 3 4] | ||
[1, 2; 3, 4] | [1, 2; 3, 4] | ||
− | + | Special matrices: | |
− | * <tt>zeros</tt>: | + | * <tt>zeros</tt>: all 0 |
− | * <tt>ones</tt>: | + | * <tt>ones</tt>: all 1 |
− | * <tt>eye</tt>: | + | * <tt>eye</tt>: 1 in diagonal, 0 elsewhere |
− | * <tt>diag</tt>: | + | * <tt>diag</tt>: square diagonal, with the speciafied diagonal |
zeros (2, 3) | zeros (2, 3) | ||
eye (2, 3) | eye (2, 3) | ||
93. sor: | 93. sor: | ||
diag ([1, 2, 3, 4]) | diag ([1, 2, 3, 4]) | ||
− | + | Also try: | |
size (5) | size (5) | ||
size ([1, 2, 3]) | size ([1, 2, 3]) | ||
size ([1; 2; 3]) | size ([1; 2; 3]) | ||
− | === | + | === Ranges === |
− | + | Try the following: | |
1:10 | 1:10 | ||
− | + | We can specify a step as well: | |
1:0.1:2 | 1:0.1:2 | ||
1:2:10 | 1:2:10 | ||
− | + | Does not work for complex numbers, since there's no order on complex numbers!<br> | |
− | + | The result will always be <tt>double</tt> but we can convert it: | |
int32 (1:0.5:10) | int32 (1:0.5:10) | ||
− | + | Decreasing range: | |
4:-1:1 | 4:-1:1 | ||
− | + | Empty range: | |
4:1:1 | 4:1:1 | ||
4:1 | 4:1 | ||
− | + | Diagonal matrix: | |
> diag(1:4) | > diag(1:4) | ||
ans = | ans = | ||
124. sor: | 124. sor: | ||
=== typeinfo === | === typeinfo === | ||
− | + | You can find out the type of an item with <tt>typeinfo</tt>. | |
typeinfo (1) | typeinfo (1) | ||
typeinfo (int32(1)) | typeinfo (int32(1)) | ||
136. sor: | 136. sor: | ||
typeinfo ([1 + 2i 1 - 1i]) | typeinfo ([1 + 2i 1 - 1i]) | ||
− | == | + | == Operations with matrices == |
− | + | Since every number is a <tt>1x1</tt> matrix, this can be applied to numbers as well. | |
− | === | + | === Transpose === |
− | + | Use an '''accent''' (<tt>'</tt>): | |
> [1 2; 3 4]' | > [1 2; 3 4]' | ||
ans = | ans = | ||
146. sor: | 146. sor: | ||
2 4 | 2 4 | ||
> _ | > _ | ||
− | + | Or | |
> (1:4)' | > (1:4)' | ||
ans = | ans = | ||
153. sor: | 153. sor: | ||
3 | 3 | ||
4 | 4 | ||
− | + | For complex matrices the accent means [http://mathworld.wolfram.com/ConjugateTranspose.html andjugate] | |
+ | in other words, the conjugate of the transpose: | ||
> [1 2i; 3i 4]' | > [1 2i; 3i 4]' | ||
ans = | ans = | ||
1 - 0i 0 - 3i | 1 - 0i 0 - 3i | ||
0 - 2i 4 - 0i | 0 - 2i 4 - 0i | ||
− | + | Conjugate: <tt>(2 + 1i)'</tt> | |
− | === | + | === Addition === |
1 + (1:4) | 1 + (1:4) | ||
eye (2, 2) + ones (2, 2) | eye (2, 2) + ones (2, 2) | ||
[1; 2; 3; 4] - [4; 3; 2; 1] | [1; 2; 3; 4] - [4; 3; 2; 1] | ||
− | === | + | === Multiplication === |
− | + | Every multiplication is matrix multiplication: | |
> [1 2; 3 4]*[1 2; 3 4] | > [1 2; 3 4]*[1 2; 3 4] | ||
ans = | ans = | ||
7 10 | 7 10 | ||
15 22 | 15 22 | ||
− | + | Power as well, so is the inverse: | |
[1 2; 3 4]^2 | [1 2; 3 4]^2 | ||
[1 2; 3 4]^-1 | [1 2; 3 4]^-1 | ||
− | + | For multiplications the dimensions must be compatible: | |
ones (2, 3) * ones (3, 5) | 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] | ||
[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 | [1 2; 3 4]^0.5 | ||
− | + | And this? | |
sqrt([1 2; 3 4]) | sqrt([1 2; 3 4]) | ||
− | + | Some operators apply to every element of a matrix, while others apply to the matrix itself. | |
> (1:4)^2 | > (1:4)^2 | ||
error: for A^b, A must be a square matrix | error: for A^b, A must be a square matrix | ||
− | + | This results in an error since multiplying a <tt>1x4</tt> matrix with itself is not possible. But: | |
> (1:4).^2 | > (1:4).^2 | ||
ans = | ans = | ||
1 4 9 16 | 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] | > [1 2; 3 4] + [1 2; 3 4] | ||
ans = | ans = | ||
202. sor: | 203. sor: | ||
2 4 | 2 4 | ||
6 8 | 6 8 | ||
− | + | But for multiplication it matters: | |
> [1 2; 3 4] * [1 2; 3 4] | > [1 2; 3 4] * [1 2; 3 4] | ||
ans = | ans = | ||
211. sor: | 212. sor: | ||
1 4 | 1 4 | ||
9 16 | 9 16 | ||
− | + | Power: | |
> [1 2; 3 4]^-1 | > [1 2; 3 4]^-1 | ||
ans = | ans = | ||
221. sor: | 222. sor: | ||
0.33333 0.25000 | 0.33333 0.25000 | ||
− | + | Elementry functions apply to every element usually: | |
sin (0:0.1:2*pi) | sin (0:0.1:2*pi) | ||
exp ([0,-1;1,0]) | exp ([0,-1;1,0]) | ||
− | + | While elementry operators apply to the matrix (<tt>*, ^, /, \</tt>) | |
− | == | + | == Variables == |
− | + | We use ''variables'' to store values. | |
a = 2 | a = 2 | ||
b = 3 | b = 3 | ||
a + b | a + b | ||
− | + | The variable <tt>ans</tt> always exists. It stores the last calculated value.<br> | |
− | + | If a variable doesn't exist we can't use it in a calculation: | |
> a / q | > a / q | ||
error: `q' undefined | error: `q' undefined | ||
− | + | Using a semicolon (<tt>;</tt>) we can do silent calculations: | |
a = 2; | a = 2; | ||
b = 3; | b = 3; | ||
a + b | a + b | ||
− | + | The <tt>whos</tt> commands shows the currently stored variables. | |
> whos | > whos | ||
Variables in the current scope: | Variables in the current scope: | ||
251. sor: | 252. sor: | ||
Total is 3 elements using 24 bytes | Total is 3 elements using 24 bytes | ||
− | + | Variables can be overwritten anytime: | |
> a = 2; | > a = 2; | ||
> a = [1 2; 3 4]; | > a = [1 2; 3 4]; | ||
260. sor: | 261. sor: | ||
a 2x2 32 double | a 2x2 32 double | ||
− | == | + | == Indexes == |
− | + | Let <tt>M</tt> be a <tt>3x3</tt> matrix. The ''j''th element of the ''i''th row of this matrix is: | |
M = rand(3,3); | M = rand(3,3); | ||
i = 1; | i = 1; | ||
267. sor: | 268. sor: | ||
M(i,j) | M(i,j) | ||
− | + | Concatenating matrices: | |
[M M] | [M M] | ||
[M; M] | [M; M] | ||
− | + | Partial series: | |
l = 0:0.1:1; | l = 0:0.1:1; | ||
l (:) | l (:) | ||
277. sor: | 278. sor: | ||
l (5:end) | l (5:end) | ||
l (1:2:11) | l (1:2:11) | ||
− | + | Also: | |
l (1:2:11) = 0 | l (1:2:11) = 0 | ||
− | + | Partial matrix: | |
A = [1 2 3; 4 5 6; 7 8 9]; | A = [1 2 3; 4 5 6; 7 8 9]; | ||
A (1:3, 1:2) | A (1:3, 1:2) | ||
A (1:2, 1:3) | A (1:2, 1:3) | ||
− | + | Skipping one line: | |
A ([1, 3], :) | A ([1, 3], :) | ||
− | + | Or selecting a specific part of a matrix: | |
S = ones (8, 8); | S = ones (8, 8); | ||
S (3:6, 3:6) = -1 | S (3:6, 3:6) = -1 | ||
− | + | Or a given index: | |
S = ones (8, 8); | S = ones (8, 8); | ||
S ([1 2 8], [2 6]) = 8 | S ([1 2 8], [2 6]) = 8 | ||
− | + | Flattening a matrix: | |
A = eye (3, 3); | A = eye (3, 3); | ||
A (:) | A (:) |
A lap 2017. november 6., 06:34-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
Partial 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 (:)
Vektorizáció
Az Octave-ban (MatLab-ban) általában egyszerre sok dolgot számolunk, nem csak egy értéken értékelünk ki egy függvényt. Például az X mátrix minden sorának számoljuk ki a normáját (X lehet nx3-as, ahol n nagyon sok):
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
Belülről kifelé haladva elemezzük a függvényeket:
- X.^2: kiszámolja az elemenkénti négyzetet
- sum(●, 2): összegzi a mátrix sorait egy oszlopvektorba (a 2 azt jelenti, a tömb második dimenziója, azaz a 2. index mentén)
- sqrt: elemenként gyököt von
Számoljuk ki a 2x^2-3x+1 függvényértékeket, ahol x egy sorvektor:
x=0:0.1:1; fx=2.*x.^2 - 3.*x + 1
A vektorizáció lényege, hogy ahol lehet mátrix és vektor műveletekre vezessük vissza a számításainkat, mert 1000 darab számpár összeszorzása lassabb, mint két darab 1000 hosszú vektor szorzása!
Függvények
Írjuk be az octave a parancssorába a következőket, vigyázzunk több soros lesz!
> function fx = f (x) > fx = 1 / (x^2 + 1); > endfunction
Majd próbáljuk ki:
> f(3) ans = 0.10000
Függvények megadása:
function <<az eredmény>> = <<a függvény neve>> (<<változók>>) ... endfunction
A függvény hasában bármit számolhatunk, de a végén adjunk értéket <<az eredmény>> változónak. A függvény hasában érdemes csendes számítást végezni, itt használjunk mindenütt pontosvesszőt (;) a sor végén!
Egy másik függvény:
function R = remove_last (x) R = x (1:end-1); endfunction
Példa:
> remove_last (1:5) ans = 1 2 3 4
Feladatok
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))