1> % plot delle due rette del sistema lineare mal condizionato della lezione 1> x = linspace(-1,16); 2> size(x) ans = 1 100 3> y1 = (2 - 9*x)/(-10); 4> y2 = (1 - 8*x)/(-9); 5> plot(x, y1) 6> hold on 7> plot(x, y2, 'r') 8> % codice di una funzione che calcola la fattorizzazione LU di una 8> % matrice quadrata 8> edit fattlu.m 9> help fattlu `fattlu' is a function from the file /home/achilles/fattlu.m fattlu calcola la fattorizzazione LU di una matrice quadrata. Additional help for built-in functions and operators is available in the on-line version of the manual. Use the command `doc ' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help@octave.org mailing list. 10> A = [1 -2 4; -3 1 0; 5 -1 1] A = 1 -2 4 -3 1 0 5 -1 1 11> B = fattlu(A) B = 1.0000 -2.0000 4.0000 -3.0000 -5.0000 12.0000 5.0000 -1.8000 2.6000 12> U = triu(B) U = 1.00000 -2.00000 4.00000 0.00000 -5.00000 12.00000 0.00000 0.00000 2.60000 13> tril(B) ans = 1.00000 0.00000 0.00000 -3.00000 -5.00000 0.00000 5.00000 -1.80000 2.60000 14> tril(B,1) ans = 1.00000 -2.00000 0.00000 -3.00000 -5.00000 12.00000 5.00000 -1.80000 2.60000 15> tril(B,-1) ans = 0.00000 0.00000 0.00000 -3.00000 0.00000 0.00000 5.00000 -1.80000 0.00000 16> L = tril(B,-1) + eye(3,3) L = 1.00000 0.00000 0.00000 -3.00000 1.00000 0.00000 5.00000 -1.80000 1.00000 17> L = tril(B,-1) + eye(3) L = 1.00000 0.00000 0.00000 -3.00000 1.00000 0.00000 5.00000 -1.80000 1.00000 18> A A = 1 -2 4 -3 1 0 5 -1 1 19> L * U ans = 1.00000 -2.00000 4.00000 -3.00000 1.00000 0.00000 5.00000 -1.00000 1.00000 20> format long 21> L * U ans = 1.000000000000000 -2.000000000000000 4.000000000000000 -3.000000000000000 1.000000000000000 0.000000000000000 5.000000000000000 -1.000000000000000 1.000000000000001 22> A = [0 -2 4; -3 1 0; 5 -1 1] A = 0 -2 4 -3 1 0 5 -1 1 23> % il pivot e' zero! 23> fattlu(A) warning: division by zero warning: division by zero ans = 0 -2 4 -Inf -Inf Inf Inf NaN NaN 24> % lu(A) calcola la fattorizzazione LU con il pivoting per righe 24> % P e' una matrice di permutazione tale che PA = LU 24> [L U P] = lu(A) L = 1.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.000000000000000 0.000000000000000 -0.600000000000000 -0.200000000000000 1.000000000000000 U = 5.000000000000000 -1.000000000000000 1.000000000000000 0.000000000000000 -2.000000000000000 4.000000000000000 0.000000000000000 0.000000000000000 1.400000000000000 P = Permutation Matrix 0 0 1 1 0 0 0 1 0 25> A A = 0 -2 4 -3 1 0 5 -1 1 26> % PA scambia le righe di A 26> P * A ans = 5 -1 1 0 -2 4 -3 1 0 27> % AP scambia le colonne di A 27> A * P ans = -2 4 0 1 0 -3 -1 1 5 28> L * U ans = 5.000000000000000 -1.000000000000000 1.000000000000000 0.000000000000000 -2.000000000000000 4.000000000000000 -3.000000000000000 1.000000000000000 0.000000000000000 29> % Quarteroni-Saleri, pag. 146, esempio 5.8 30> A = [1 1+0.5e-15 3; 2 2 20; 3 6 4] A = 1.00000000000000 1.00000000000000 3.00000000000000 2.00000000000000 2.00000000000000 20.00000000000000 3.00000000000000 6.00000000000000 4.00000000000000 31> C = fattlu(A); 32> U = triu(C); 33> L = tril(C,-1) + eye(3); 34> A - L*U ans = 0 0 0 0 0 0 0 0 4 35> [L U P] = lu(A); 36> P*A - L*U ans = 0.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 -2.22044604925031e-16 0.00000000000000e+00 37> P P = Permutation Matrix 0 0 1 0 1 0 1 0 0 38> format 39> P*A - L*U ans = 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 -2.2204e-16 0.0000e+00 40> help format `format' is a built-in function -- Command: format -- Command: format options Reset or specify the format of the output produced by `disp' and Octave's normal echoing mechanism. This command only affects the display of numbers but not how they are stored or computed. To change the internal representation from the default double use one of the conversion functions such as `single', `uint8', `int64', etc. By default, Octave displays 5 significant digits in a human readable form (option `short' paired with `loose' format for matrices). If `format' is invoked without any options, this default format is restored. Valid formats for floating point numbers are listed in the following table. `short' Fixed point format with 5 significant figures in a field that is a maximum of 10 characters wide. (default). If Octave is unable to format a matrix so that columns line up on the decimal point and all numbers fit within the maximum field width then it switches to an exponential `e' format. `long' Fixed point format with 15 significant figures in a field that is a maximum of 20 characters wide. As with the `short' format, Octave will switch to an exponential `e' format if it is unable to format a matrix properly using the current format. `short e' `long e' Exponential format. The number to be represented is split between a mantissa and an exponent (power of 10). The mantissa has 5 significant digits in the short format and 15 digits in the long format. For example, with the `short e' format, `pi' is displayed as `3.1416e+00'. `short E' `long E' Identical to `short e' or `long e' but displays an uppercase `E' to indicate the exponent. For example, with the `long E' format, `pi' is displayed as `3.14159265358979E+00'. `short g' `long g' Optimally choose between fixed point and exponential format based on the magnitude of the number. For example, with the `short g' format, `pi .^ [2; 4; 8; 16; 32]' is displayed as ans = 9.8696 97.409 9488.5 9.0032e+07 8.1058e+15 `short eng' `long eng' Identical to `short e' or `long e' but displays the value using an engineering format, where the exponent is divisible by 3. For example, with the `short eng' format, `10 * pi' is displayed as `31.4159e+00'. `long G' `short G' Identical to `short g' or `long g' but displays an uppercase `E' to indicate the exponent. `free' `none' Print output in free format, without trying to line up columns of matrices on the decimal point. This also causes complex numbers to be formatted as numeric pairs like this `(0.60419, 0.60709)' instead of like this `0.60419 + 0.60709i'. The following formats affect all numeric output (floating point and integer types). `+' `+ CHARS' `plus' `plus CHARS' Print a `+' symbol for nonzero matrix elements and a space for zero matrix elements. This format can be very useful for examining the structure of a large sparse matrix. The optional argument CHARS specifies a list of 3 characters to use for printing values greater than zero, less than zero and equal to zero. For example, with the `+ "+-."' format, `[1, 0, -1; -1, 0, 1]' is displayed as ans = +.- -.+ `bank' Print in a fixed format with two digits to the right of the decimal point. `native-hex' Print the hexadecimal representation of numbers as they are stored in memory. For example, on a workstation which stores 8 byte real values in IEEE format with the least significant byte first, the value of `pi' when printed in `native-hex' format is `400921fb54442d18'. `hex' The same as `native-hex', but always print the most significant byte first. `native-bit' Print the bit representation of numbers as stored in memory. For example, the value of `pi' is 01000000000010010010000111111011 01010100010001000010110100011000 (shown here in two 32 bit sections for typesetting purposes) when printed in native-bit format on a workstation which stores 8 byte real values in IEEE format with the least significant byte first. `bit' The same as `native-bit', but always print the most significant bits first. `rat' Print a rational approximation, i.e., values are approximated as the ratio of small integers. For example, with the `rat' format, `pi' is displayed as `355/113'. The following two options affect the display of all matrices. `compact' Remove blank lines around column number labels and between matrices producing more compact output with more data per page. `loose' Insert blank lines above and below column number labels and between matrices to produce a more readable output with less data per page. (default). See also: fixed_point_format, output_max_field_width, output_precision, split_long_rows, rats Additional help for built-in functions and operators is available in the on-line version of the manual. Use the command `doc ' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help@octave.org mailing list. 41> format short G 42> P*A - L*U ans = Columns 1 and 2: 0 0 0 0 0 -2.2204E-16 Column 3: 0 0 0 43> % esempi della lezione di oggi 43> A = [3 -2 5; -1 4 -7; 5 0 3] A = 3 -2 5 -1 4 -7 5 0 3 44> null(A) ans = -0.30305 0.80812 0.50508 45> norm(ans) ans = 1 46> x = [-3 8 5] x = -3 8 5 47> x/norm(x) ans = -0.30305 0.80812 0.50508 48> b = [5; -5; 5] b = 5 -5 5 49> det(A) ans = 8.8818E-15 50> rank(A) ans = 2 51> A\b warning: matrix singular to machine precision, rcond = 9.25186e-18 ans = 0.66327 -0.10204 0.56122 52> % problemi mal posti 52> a = [9 -10; 8 -9] a = 9 -10 8 -9 53> b = [2; 1] b = 2 1 54> a\b ans = 8 7 55> % matrici di Hilbert sono mal condizionate 55> hilb(1) ans = 1 56> hilb(2) ans = 1 0.5 0.5 0.33333 57> hilb(3) ans = 1 0.5 0.33333 0.5 0.33333 0.25 0.33333 0.25 0.2 58> b = hilb(13) * ones(13,1) b = 3.1801 2.2516 1.8182 1.5474 1.3562 1.2118 1.0977 1.0049 0.9275 0.86184 0.80532 0.75608 0.71275 59> % La soluzione precisa e' (1, 1, 1, ..., 1). 59> H\b warning: matrix singular to machine precision, rcond = 1.24226e-18 warning: matrix singular to machine precision, rcond = 1.34211e-18 ans = 1 1 1 0.99972 1.0019 0.99217 1.0187 0.97528 1.0119 1.0129 0.97618 1.0144 0.99679 60> quit