Applied Linear Algebra in Geoscience Using MATLAB
Contents Getting Started Creating Arrays Mathematical Operations with Arrays Using Script Files and Managing Data Two-Dimensional Plots Programming in MATLAB User-Defined Functions and Function Files Polynomials, Curve Fitting, and Interpolation Applications in Numerical Analysis Three-Dimensional Plots Symbolic Math Matrices Linear equations Determinants Eigenvalues and eigenvectors Orthogonal vectors and matrices Vector and matrix norms Gaussian elimination and the LU dec. Linear system applications Gram-Schmidt decomposition The singular value decomposition Least-squares problems Linear Independence Basis of a Subspace The Rank of a Matrix Implementing the QR factorization The algebraic eigenvalue problem
Linear system applications four additional applications that make use of linear algebra. Fourier series, Finite Difference schemes for solving PDE. Least Squares Cubic Spline interpolation. FOURIER SERIES To this point, we have dealt with finite-dimensional vector spaces, but there are many applications for vector spaces with infinitely many dimensions. A particularly important example is Fourier series. we introduced the inner product of vectors also presented the L 2
Linear system applications Consider the infinite sequence of trigonometric functions A straightforward computation shows that this is an orthonormal sequence over the interval π x π.
Linear system applications We now consider this infinite sequence to be an orthonormal basis for what we term a function space, a set of linear combinations of the basis functions. Note: Not all functions belong to this function space (e.g. ai = 1, bi = 0) For f to belong to the function space, we require that f 2 be finite. Since is an orthonormal sequence Thus, our function is in this function space if the above series converges, and we call it the Fourier series for f. The a i, b i are called the Fourier coefficients for f.
Linear system applications How do we compute the Fourier coefficients? ----- > The basis is orthonormal < ------ Multiply both sides by cos kx for any k 1 and integrate from π to π. and so In a similar fashion It only remains to compute a 0 :
Linear system applications Summary The Fourier coefficients for the function f, π x π, are Now, if we have a periodic function f, is there a Fourier series that converges to f? Example: The Square Wave The square wave is periodic and, has period 2π, and is +1 in one-half the period and 1 in the other half. The Fourier series for the square wave is not difficult to compute. All the cosine terms in its Fourier series are 0, since the function is odd (f ( x) = f (x)). The series is
Linear system applications function psum = sqrwavepartialsums(t,n) m = length(t); psum = zeros(m,1); function psum = sqrwavepartialsums(t,n) sum = 0.0; for i = 1:n j = 2*i-1; sum = sum + sin(j.*t)./j; end psum = (4/pi)*sum; for k = 1:m x = t(k); sum = 0.0; for i = 1:n j = 2*i-1; sum = sum + sin(j*x)/j; end psum(k) = (4/pi)*sum; end
Advanced Plotting 3D Surface and Contour Plots To graph a function of two variables we need to first evaluate that function over a grid of points, not just a line as in the 2D case. Some functions, while still vectorized, will only operate on vectors and not matrices. for example the mvnpdf() function.we can still plot such functions in 3D with a few small changes After obtaining X and Y from meshgrid(), save X's size, evaluate f by passing in X and Y as column vectors, (using the : operator), and then reshape the output Z back to the original size and continue
Advanced Plotting There are several 3d plot types available. Here we use surf(), which plots the surface of the function, contourf(), which plots the contour lines of a function and fills the area between them with color, and mesh(), which is similar to surf(), displaying a wire mesh rather than a solid surface. The colors used in each are specified by the current colormap and can be changed by using the colormap() command. Type doc colormap for a list of options. There are several other 3d plotting functions: plot3() for instance is the 3d generalization of plot().
Advanced Plotting Note, the command view([90,90]) can be very useful to rotate a plot by 90 degrees, effectively reversing the locations of the x and y axes. Virtually everything about customizing 2D plots applies equally to 3D plots. We can add a title, a legend, labels using xlabel(), ylabel(), and zlabel(), change the range of the axes, the font size, etc.
Advanced Plotting Below we display a 3d bar plot of the same underlying data. We take advantage of the mat2cell() command to partition the Z data into 21x21 6-by-6 blocks, each block stored within a cell. We then use the cellfun() function to replace each block with its mean. We can use this same technique to apply any function to arbitrary sized blocks of a matrix. Note that when the size of the data within cells is different, 'UniformOutput' must be set to false.
Advanced Plotting 3D plotting in Matlab requires a uniform grid of points but the data we obtain from experiments or measurements may not satisfy this constraint. In such cases, we can use the griddata() function to interpolate along a uniform grid of points for us.
Advanced Plotting Rotating Figures and Creating Movies Figures can be rotated or moved with the mouse by first clicking on the appropriate shortcut button on a figure window toolbar. However, we can also automate and animate the rotation by using the camorbit() command. We can also create a movie by capturing frames using getframe() command and playing them with the movie() function. Alternatively we can create an avi file with the avifile(), andaddframe() functions. Read their help entries for more information and examples. There are many other functions that can be used to adjust the camera angle and motion such ascamdolly(), campan(), camzoom(), and camroll().
Advanced Plotting Annotations and the Plot Editor Matlab provides an interactive graphical interface for modifying and inspecting existing figures. This mode can be entered by selecting 'view->figure Palette' or by selecting the appropriate shortcut button on the figure toolbar. Here we can add annotations such as text, arrows, and shapes as you would in a program like powerpoint. We can also inspect and change attributes as an alternative to using set() and get(). Annotations are placed relative to the figure window, not the axes. The location is, by default, specified by normalized coordinates between 0 and 1 so that [0.8 0.4] is the point 80% of the width from the left and 40% of the height from the bottom. In the case of a line or an arrow, we specify two x-y pairs, denoting the start and end points. In the case of a text box we specify four numbers, the x and y coordinates followed by the width and height of the box. Type doc annotation for more information. These commands were generated automatically after adding the annotations in the plot editor.
Advanced Plotting