Anteprima
Vedrai una selezione di 9 pagine su 37
Calcolo numerico - il Tutorial Matlab Pag. 1 Calcolo numerico - il Tutorial Matlab Pag. 2
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 6
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 11
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 16
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 21
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 26
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 31
Anteprima di 9 pagg. su 37.
Scarica il documento per vederlo tutto.
Calcolo numerico - il Tutorial Matlab Pag. 36
1 su 37
D/illustrazione/soddisfatti o rimborsati
Disdici quando
vuoi
Acquista con carta
o PayPal
Scarica i documenti
tutte le volte che vuoi
Estratto del documento

Calcolo del determinante di una matrice

B*Aans = 1 0 00 1 00 0 1This shows that B is indeed the inverse matrix of A.' ( In some applications of linear algebra knowledge of the determinant of a matrix is required.MATLAB built-in function det is designed for computing determinants.LetA = magic(3);Determinant of A is equal todet(A)ans =-360One of the classical methods for computing determinants utilizes a cofactor expansion. For moredetails, see e.g., [2], pp. 103-114. entry of the matrix AFunction ckl = cofact(A, k, l) computes the cofactor ckl of the a klfunction ckl = cofact(A,k,l)% Cofactor ckl of the a_kl entry of the matrix A.[m,n] = size(A);if m ~= nerror('Matrix must be square') 15endB = A([1:k-1,k+1:n],[1:l-1,l+1:n]);ckl = (-1)^(k+l)*det(B);Function d = mydet(A) implements the method of cofactor expansion for computingdeterminantsfunction d = mydet(A)% Determinant d of the matrix A. Function cofact must be% in MATLAB's search path.[m,n] = size(A);if m ~= nerror('Matrix must be

square')enda = A(1,:);
c = [];
for l=1:nc
    1l = cofact(A,1,l);
    c = [c;c1l];
end
d = a*c;

Let us note that function mydet uses the cofactor expansion along the row 1 of the matrix A.

Method of cofactors has a high computational complexity. Therefore it is not recommended for computations with large matrices. Its is included here for pedagogical reasons only.

To measure a computational complexity of two functions det and mydet we will use MATLAB built-in function flops. It counts the number of floating-point operations (additions, subtractions, multiplications and divisions).

Let
A = rand(25);
be a 25-by-25 matrix of uniformly distributed random numbers in the interval ( 0, 1 ).

Using function det we obtain
flops(0)
det(A)
ans =-0.1867
flops
ans = 10100

For comparison, a number of flops used by function mydet is
flops(0) 16
mydet(A)
ans =-0.1867
flops
ans = 223350

The adjoint matrix adj(A) of the matrix A is also of interest in linear algebra (see, e.g., [2],p.108).

function B = adj(A)
% Adjoint matrix B of the

square matrix A.

[m,n] = size(A);
if m ~= n
    error('Matrix must be square')
end
B = [];
for k = 1:n
    for l=1:n
        B = [B;cofact(A,k,l)];
    end
end
B = reshape(B,n,n);

The adjoint matrix and the inverse matrix satisfy the equation -1A = adj(A)/det(A) (see [2], p.110 ). Due to the high computational complexity this formula is not recommended for computing the inverse matrix.)

The 2-norm (Euclidean norm) of a vector is computed in MATLAB using function norm.

Let a = -2:2
a = [-2 -1 0 1 2]
The 2-norm of a is equal to
twon = norm(a) = 17
twon = 3.1623

With each nonzero vector one can associate a unit vector that is parallel to the given vector. For instance, for the vector a in the last example its unit vector is

unitv = a / twon
unitv = [-0.6325 -0.3162 0 0.3162 0.6325]

The angle between two vectors a and b of the same dimension is computed using the formula θ = arccos(a.b/||a|| ||b||), where a.b stands for the dot product of a and b, ||a|| is the norm of the vector a and arccos is the inverse cosine function.

Let

Il vettore a sia lo stesso definito sopra e sia b = (1:5) b = 12345 Poi angle = acos((a*b)/(norm(a)*norm(b))) angle = 1.1303 Il concetto del prodotto vettoriale può essere generalizzato facilmente all'insieme composto da n-1 vettori nello spazio euclideo di dimensione n. La funzione crossprod fornisce una generalizzazione della funzione cross di MATLAB. function cp = crossprod(A) % Prodotto vettoriale cp di un insieme di vettori che sono memorizzati nelle colonne di A. [n, m] = size(A); if n ~= m+1 error('Il numero di colonne di A deve essere uno in meno del numero di righe') endif rank(A) < min(m,n) cp = zeros(n,1); else C = [ones(n,1) A]'; cp = zeros(n,1); for j=1:n cp(j) = cofact(C,1,j); end end Sia A = [1 -2 3; 4 5 6; 7 8 9; 1 0 1] A = 1 -2 3 4 5 6 7 8 9 1 0 1 Il prodotto vettoriale dei vettori colonna di A è cp = crossprod(A) cp = -6 20 -14 24 Il vettore cp è ortogonale allo spazio delle colonne della matrice A. Questo può essere facilmente verificato calcolando il prodotto vettore-matrice cp'*A ans = 0 0 0 Sia L: una trasformazione lineare.

It is well known that any linear transformation in n.question is represented by an m-by-n matrix A, i.e., L(x) = Ax holds true for any x.

Matrices of some linear transformations including those of reflections and rotations are discussed in detail in Tutorial 4, Section 4.3.

With each matrix one can associate four subspaces called the four fundamental subspaces. The subspaces in question are called the column space, the nullspace, the row space, and the left nullspace. First two subspaces are tied closely to the linear transformations on the finite-dimensional spaces.

Throughout the sequel the symbols (L) and (L) will stand for the range and the kernel of the linear transformation L, respectively. Bases of these subspaces can be computed easily. Recall that (L) = column space of A and (L) = nullspace of A. Thus the problem of computing the bases of the range and the kernel of a linear transformation L is equivalent to the problem of finding bases of the column space and the nullspace of a.

matrix that represents transformation L. Function fourb uses two MATLAB functions rref and null to compute bases of four fundamental subspaces associated with a matrix A. function [cs, ns, rs, lns] = fourb(A) % Bases of four fundamental vector spaces associated % with the matrix A. % cs- basis of the column space of A % ns- basis of the nullspace of A % rs- basis of the row space of A % lns- basis of the left nullspace of A [V, pivot] = rref(A); r = length(pivot); cs = A(:,pivot); ns = null(A,'r'); rs = V(1:r,:)'; lns = null(A','r'); In this example we will find bases of four fundamental subspaces associated with the random matrix of zeros and ones. This sets up the seed of the randn function to 0. randn('seed',0) Recall that this function generates normally distributed random numbers. Next, a 3-by-5 random matrix is generated using the function randn. A = randn(3,5) A = 1.1650 0.3516 0.0591 0.8717 1.2460 0.6268 -0.6965 1.7971 -1.4462 -0.6390 0.0751 1.6961 0.2641 -0.7012

0.5774The following trick creates a matrix of zeros and ones from the random matrix AA = A >= 0A = 1 1 1 1 11 0 1 0 01 1 1 0 1 20Bases of four fundamental subspaces of matrix A are now computed using function fourb[cs, ns, rs, lns] = fourb(A)cs = 1 1 11 0 01 1 0ns = -1 00 -11 00 00 1rs = 1 0 00 1 01 0 00 0 10 1 0lns =Empty matrix: 3-by-0Vectors that form bases of the subspaces under discussion are saved as the column vectors.The Fundamental Theorem of Linear Algebra states that the row space of A is orthogonal to thenullspace of A and also that the column space of A is orthogonal to the left nullspace of A(see [6] ). For the bases of the subspaces in this example we havers'*nsans = 0 00 00 0cs'*lnsans =Empty matrix: 3-by-0+ , In this section we discuss some computational tools that can be used in studies of real vectorspaces. Focus is on linear span, linear independence, transition matrices and the Gram-Schmidtorthogonalization. 21Linear spanConcept of the linear span of

A set of vectors in a vector space is one of the most important ones in linear algebra. Using MATLAB one can determine easily whether or not a given vector is in the span of a set of vectors. Function span takes a vector, say v, and an unspecified number of vectors that form a span. All inputted vectors must be of the same size. On the output, a message is displayed to the screen. It says that either v is in the span or that v is not in the span.

function span(v, varargin)
    % Test whether or not vector v is in the span of a set
    % of vectors.
    
    A = [];
    n = length(varargin);
    for i=1:n
        u = varargin{i};
        u = u';
        A = [A u(:)];
    end
    
    v = v';
    v = v(:);
    
    if rank(A) == rank([A v])
        disp('Given vector is in the span.')
    else
        disp('Given vector is not in the span.')
    end
end

The key fact used in this function is a well-known result regarding the existence of a solution to the system of linear equations. Recall that the system of linear equations Ax = b possesses a solution if and only if rank(A) = rank([A b]).

MATLAB function varargin used here allows a user to enter a variable number of vectors of the span. To test function span we will run this function on matrices. Let v = ones(3) and choose matrices A = pascal(3) and B = rand(3) to determine whether or not v belongs to the span of A and B. Executing function span we obtain span(v, A, B). Given vector is not in the span.

Linear independence

Suppose that one wants to check whether or not a given set of vectors is linearly independent. Utilizing some ideas used in function span one can write his/her function that will take a unspecified number of vectors and return a message regarding linear independence/dependence of the given set of vectors. We leave this task to the reader (see Problem 32).

Transition matrix

Problem of finding the transition matrix from one vector space to another vector space is interesting in linear algebra. We assume that the ordered bases of these spaces are stored in columns of matrices T and S, respectively. Function transmat

implements a well-known method for finding the transition matrix.
function V = transmat(T, S)
% Transition matrix V from a vector space having the ordered
% basis T to another vector space having the ordered basis S.
% Bases of the vector spaces are stored in columns of the
% matrices T and S.
[m, n] = size(T);
[p, q] = size(S);
if (m ~= p) | (n ~= q)
error('Matrices must be of the same dimension')
end
V = rref([S T]);
V = V(:,(m + 1):(m + n));

Let T = [1 2;3 4]; S = [0 1;1 0]; be the ordered bases of two vector spaces. The transition matrix V from a vector space having the ordered basis T to a vector space whose ordered basis is stored in columns of the matrix S is

V = transmat(T, S)
V = 3 4
    1 2

We will use the transition matrix V to compute a coordinate vector in the basis S. Let

[x]_T

be the coordinate vector in the basis T. Then the coordinate vector [x]_S, is

Dettagli
Publisher
A.A. 2012-2013
37 pagine
SSD Scienze matematiche e informatiche MAT/08 Analisi numerica

I contenuti di questa pagina costituiscono rielaborazioni personali del Publisher cecilialll di informazioni apprese con la frequenza delle lezioni di Calcolo numerico e studio autonomo di eventuali libri di riferimento in preparazione dell'esame finale o della tesi. Non devono intendersi come materiale ufficiale dell'università Università degli studi di Napoli Federico II o del prof D'Amore Luisa.