









Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!










This document looks at some important concepts in linear and shows how to solve some basic problems using MATLAB.
Let’s think about what a matrix-vector product actually does. It’s more inter- esting that you think! Lets take a simple example. Consider the vector [1;0] multiplied by a matrix [1,1;1,1]
figure(’color’,[1 1 1]) A=ones(2) x=[1;0] Ax=Ax line([0 x(1)],[0 x(2)]), hold on, grid on line([0,Ax(1)],[0,Ax(2)],’color’,[1 0 0]) text(1.1,0,’x’,’fontweight’,’bold’), text(1.1,1.1,’Ax’,’fontweight’,’bold’) axis([-0.5 1.5 -0.5 1.5]) title(’Action of a matrix A on a vector x’)
x =
1 0
Ax =
1
−0.5 0 0.5 1 1.
−0.
0
1
x
Ax*
Action of a matrix A on a vector x
We can see that the matrix multiplication resulted in a vector that was rotated by a certain angle and also stretched by a certain factor. This action (stretching and rotating) is extremely useful for switching from one co-ordinate system [x,y] to another [x’, y’]. Something that you will come across in special relativity and a number of other areas. To visualise what we mean by a co-ordinate transformation, consider the basis vectors x:=[1,0] and y:=[0,1], and a point p:=[0.2,0.1].
figure(’color’,[1 1 1]) xy=[1,0;0,1] p=[0.5,0.3,0.1;0.4,0.2,0.0] A=[-1 1;1 1] xyp=Axy; % Co-ordinate rotation step Ap=Ap; line([0 xy(1,1)],[0 xy(2,1)]), grid on, hold on line([0,xy(1,2)],[0,xy(2,2)]) plot(p(1,:),p(2,:),’’,’markersize’,6,’color’,’b’) line([0, xyp(1,1)],[0 xyp(2,1)],’color’,[1 0 0]) line([0, xyp(2,1)],[0,xyp(2,2)],’color’,[1 0 0]) plot(Ap(1,:),Ap(2,:),’’,’markersize’,6,’color’,’r’) axis([-1.5 1.5 -1.5 1.5]), axis square title(’Changing the co-ordinate basis with matrix multiplication’)
There are a number of important matrices that are extremely useful, and worth knowing about. We will use several of these later on in this lecture. Here are a few:
The Identity Matrix or I
I = eye(4)
A matrix of ones or zeros:
O = ones(4) Z = zeros(4)
Upper and lower triangular matrices. These are particularly important, as we shall see below, and consist of matrices that have zeros below, or above the main diagonal
UT = triu(randn(4)) LT = tril(randn(4))
Symmetric matrices are matrices that have the same elements above he main diagonal as below the main diagonal:
A = randn(4); SYM = A*A’
The Vandermonde matrix is a matrix whose columns are polynomials in x. This is useful in curve fitting and interpolation, for example, if I wanted to find the 3rd^ order interpolating polynomial for some data [x,y], then I would use the following matrix:
figure(’color’,[1 1 1]) x=linspace(0,1,5)’;
Here the dash represents a derivative with respect to x. If we ignore terms above the first derivative, we see we can say that
f (x+∆x)−f (x) ∆x
This is called the forward difference approximation. If we apply this formula to itself, we come up with an approximation for the second derivative of a function:
∆x
f (x+∆x+∆x)−f (x+∆x)−f (x+∆x)+f (x) ∆x
or that, finally:
How to turn this into a matrix, however? If we imagine a function expressed on a grid of points x 1 , x 2 , · · · , xN and replace the function evaluation f (x + ∆x) with function evaluations at the grid points, then the second derivative can be expressed as the following matrix:
I=ones(N,1); D2 = spdiags([I, -2*I, I],[-1:1],N,N); full(D2)
ans =
-2 1 0 0 0 1 -2 1 0 0 0 1 -2 1 0 0 0 1 -2 1 0 0 0 1 -
Here we use the construct spdiags to tell MATLAB that the matrix is sparse, i.e. is composed of mainly zero elements. In fact, this particular type of sparsity pattern is called tridiagonal. Consider the second row of this matrix mul- tiplied by a vector which is the function evaluated on the grid of points xi, [f (x 0 ), f (x 1 ), f (x 2 ), · · · , f (xN )]T^. We have the following result:
f (x 0 ) f (x 1 ) f (x 2 ) .. . f (xN )
= f (x 0 ) − 2 × f (x 1 ) + f (x 2 )
That is, the result of multiplying the second row of the differentiation matrix by a vector representing our function on the grid, gives an approximation to the derivative of our function on the grid. As an example, let’s calculate the second derivative of f (x) = expsin(x)^ on a grid of 21 points:
N=21; x=linspace(0,2pi,N)’; % set up the grid I=ones(N,1); dx=x(2)-x(1); D2 = spdiags([I, -2I, I],[-1:1],N,N)/dx^2; %2nd derivative matrix f=@(x)(exp(sin(x))) % function d2 = D2f(x); % numerical derivative d2(1)=1; d2(N)=1; % fix boundary points figure(’color’,[1 1 1]) plot(x,f(x),x,d2), grid on, hold on d2f=@(x)(cos(x).^2.exp(sin(x))-sin(x).*exp(sin(x))) % actual second derivative plot(x,d2f(x),’r-.’) title(’Approximating a second derivative using a differentiation matrix’); legend(’f(x)=exp^{sin(x)}’,’Approximate 2^{nd} deriv’,’Actual 2^{nd} deriv’); hold off
f =
@(x)(exp(sin(x)))
d2f =
@(x)(cos(x).^2.exp(sin(x))-sin(x).exp(sin(x)))
We want to calculate the currents I 1 , I 2 , I 3
Using Kirchoff’s voltage laws we get the following three equations for the three unknown currents:
which, with a little re-arranging becomes:
Finally, we can gather all the coefficients into a matrix, and the unknowns into a vector, to write the single matrix linear equation:
In this way we have got the system of equations into the form that we described above, namely: Ax = b
Of course, for a small system (3 by 3 in this case) we can solve this by hand. For a larger system, however, we need a computer. The basic approach to solving such a system is an extension of that used by hand, namely we add and subtract multiples of one row to one another until we finally end up with an upper triangular matrix. This action of adding and subtrating multiples of one row to another is known as a basic row operation. Let us illustrate the process with MATLAB. Before we begin, we augment the right hand side vector to be
an extra column in our matrix A, so it is now three rows by four columns.
rrefexample(A)
Column: 1 Divide row 1 by 76 Subract -25 times row 1 from row 2 Subract -50 times row 1 from row 3
A =
1.0000 -0.3289 -0.6579 0. 0 47.7763 -17.4474 3. 0 -17.4474 73.1053 6.
Column: 2 Divide row 2 by 47. Subract -17.4474 times row 2 from row 3
A =
1.0000 -0.3289 -0.6579 0. 0 1.0000 -0.3652 0. 0 0 66.7337 7.
Column: 3 Divide row 3 by 66.
A =
1.0000 -0.3289 -0.6579 0. 0 1.0000 -0.3652 0. 0 0 1.0000 0.
Matrix A is now upper triangular
ans =
QR decomposition In this case, a general matrix is written as the product of two matrices, one orthogonal and one upper triangular: A = QR As the inverse of an orthogonal matrix is equal to its transpose, we find the solution to the general system Ax = b can be calculated as:
Ax = b QRx = b Q(Rx) = b Rx = QT^ × b x = R \ QT^ × b
LU Decomposition In this case the matrix is decomposed into two, a lower and an upper triangular matrix. As both the matrices are triangular, the back (and forward) substitution process is very quick:
Ax = b LUx = b L(Ux) = b Lx = U \ b x = L \ (U \ b)
As another example of using MATLAB to solve linear systems, let us perform a least squares fit on some experimental data. We have run an experiment to measure the distance fallen by an object in a given time. We know from the laws of motion that the distance is related to the initial velocity and position, and the acceleration through a quadratic relationship:
x(t) = x(0) + v(0)t + 12 gt^2
We want to fit a quadratic line of best fit through the data, and hence work out a rough estimate for g the acceleration due to gravity.
load fall_data t=data(:,1); dist=data(:,2); I=ones(size(t)); R=[I,t,t.^2]; % This is a VANDERMONDE matrix (see above) coeffs = R\dist g=2coeffs(3) % approximation for g (coeff is g/2) plot(t,dist,’o’,t,Rcoeffs,’r’), grid on title(’Fitting a quadratic curve to experimental data’)
coeffs =
g =
0 0.5 1 1.5 2
0
2
4
6
8
10
12
14
16
18
20
Fitting a quadratic curve to experimental data
As we have seen, the action of a mtrix on a vector is one of rotation and scaling. There are certain vectors, however, which in some sense resonate with some fun- damental property of the matrix, and do not get rotated. These special vectors (or directions in N-Dimensional space) are called eigenvectors and the scaling factor by which they are stretched are called the eigenvalues. Mathematically what we are saying is: Ax = λx Or visually we are saying:
evecPlot
Φ(¨t)
Θ′′(x)
The only way that the left hand side, which is completely independent of x, can be equal to the right hand side, which is completely independent of t, is if both sides are equal to a constant, −k^2. We are interested in time independent solutions (standing waves) for this problem, so we can consider only the spatial parts of the equation, which can be written as:
∂^2 Θ(x)
This is an eigenvalue equation, exactly as we had above, and the solutions we are looking for are called eigenfunctions. This particular equation is important in physics and is known as the Helmholtz equation.
Using the second derivative differentiation matrix we described above (actually a more accurate version of it) and extending the notion to two dimensions, we transform a differential equation into a matrix eigenvalue equation. We can then use MATLAB to compute the eigenvalues and eigenvectors numerically to investigate the standing wave patterns, for example:
chladni
0
10
20
30
40
0
10
20
30
40
−0.
0