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
Norm L1, L2 and inf It is helpful to view the unit sphere of a norm, the unit spheres for the -, 1-, and 2-norm have equations
LU Decomposition We practiced the process of solving a non-singular linear system Ax = b using Gaussian elimination. We formed the augmented matrix A b and applied the elementary row operations as below, to reduce A to upper-triangular form. 1. Multiplying a row by a scalar. 2. Subtracting a multiple of one row from another 3. Exchanging two rows Following this step, back substitution computed the solution. In many applications where linear systems appear, one needs to solve Ax = b for many different vectors b. Most of the work in Gaussian elimination is applying row operations to arrive at the uppertriangular matrix. If we need to solve several different systems with the same A, then we would like to avoid repeating the steps of Gaussian elimination on A for every different b. This can be accomplished by the LU decomposition, which in effect records the steps of Gaussian elimination. Since Gaussian elimination is used so often, the algorithm must be stable. Unfortunately, this is not true, and we must add an operation termed partial pivoting.
LU Decomposition The first step of Gaussian elimination is to use a 11 = 1 as the pivot and subtract 2 times the first row from the second and 3 times the first row from the third Record these actions by placing the multipliers 2 and 3 into the entries they made zero. In order to make it clear that we are recording multipliers and not elements of A, put the entries in parentheses. This leads to: Thus, we see that A is the product of the lower triangular L and the upper triangular U. When a matrix can be written as a product of simpler matrices, we call that a decomposition and this one we call the LU decomposition.
Using LU To Solve Equations Factor A into the product of L and U: First solve Ly = b. This finds y = Ux. Now solve Ux = y to find x. Each of these solution steps is simple. First, the system Ly is lower triangular. Next, the system Ux = y is upper triangular.
Using LU To Solve Equations
Using LU To Solve Equations
Gaussian Elimination With Partial Pivoting Gaussian elimination can produce extremely bad results under certain circumstances; in fact, the results can be completely wrong!!! Consider the matrix, and use three-digit arithmetic. There is only one step required to produce the LU decomposition. Use the multiplier 2/0.00001 The problem arose when we divided by a small pivot element and obtained a large multiplier. The large multiplier resulted in the addition of a very large number to a much smaller number. The result was loss of any contribution from the small number. In our three-digit arithmetic, the 1 is lost, and the result is Now compute LU to get which is disastrously different from A!
Using LU To Solve Equations There is a good solution to this problem. When choosing the pivot element on the diagonal at position aii, locate the element in column i at or below the diagonal that is largest in magnitude, say aji, i j n. If j i, interchange row j with row i, and then the multipliers, a ji /a ii, satisfy a ji /a ii 1, i + 1 j n, and we avoid multiplying a row by a large number and losing precision. We call this Gaussian elimination with partial pivoting (GEPP). Of course, this is not the original matrix A, but A with its two rows swapped (permuted). If we use GEPP, then an LU decomposition for A consists of three matrices P, L, and U such that
Using LU To Solve Equations P is a permutation matrix, also called the pivot matrix. Start with P = I, and swap rows i and j of the permutation matrix whenever rows i and j are swapped during GEPP. For instance, would be the permutation matrix if the second and third rows of A are interchanged during pivoting. Pivot row = 1. Swap rows 1 and 3, and permute P. Do not interchange rows of L until arriving at the pivot in row 2, column 2 Apply the pivot element, and add multipliers to L. Pivot row = 2. Swap rows 2 and 3. Permute P and L.
Using LU To Solve Equations Apply the pivot element and update L. The final results are NOTE: Even if a row interchange occurs when dealing with column 1, do not interchange the corresponding rows of L until moving to column 2. Think of it this way. The matrix A after a row swap defines the starting configuration. The elements in the first column of L correspond to the multipliers after the row interchange involving pivot position (1, 1),if any.
A good reason for computing L and U is that the cubic LU decomposition algorithm allows us to repeatedly solve Ax = b for many b s without having to re-compute either L or U
Eq. with One Variable Numerical methods are commonly used for solving mathematical problems that are formulated in science (and engineering) where it is difficult or impossible to obtain exact solutions A solution to the equation (also called a root) is a numerical value of x that satisfies the equation (graphically, crosses or touches the x axis) If such a value does not exist or is difficult to determine, a numerical solution can be determined by finding an x that is very close to the solution. This is done by the iterative process, where in each iteration we determine a value of x that is closer to the solution.
Eq. with One Variable NOTE: The function has to be written in a standard form. EX) Determine the solution of the above equation Lets to plot this fun. first
Max or Min of a Function The fzero command finds zeros of a function only where the function crosses the x axis. The command does not find a zero at points where the function touches but does not cross the X axis. The fzero command can also be used to find the value of x where the function has a specific value. This is done by translating the function up or down 0.1 In many applications there is a need to determine the local minimum or maximum of a function In calculus the value of x that corresponds to a local minimum or maximum is determined by finding the zero of the derivative of the function. The value of y is determined by substituting the x into the function.
ODEs Ex) Local min Abs. min Notice that the fminbnd command gives the local minimum.
Numerical Integration The fminbnd command can also be used to find the maximum of a function. This is done by multiplying the function by 1 and finding the minimum. Calculating area and volume, velocity from acceleration, and work from force and displacement are just a few examples where integrals are used. In calculus courses the integrand (the quantity to be integrated) is usually a function. In applications of science and engineering the integrand can be a function or a set of data points. A definite integral In numerical integration the total area is obtained by dividing the area into small sections, calculating the area of each section, and adding Them up.
Numerical Integration Various numerical methods have been developed for this purpose. The difference between the methods is in the way that the area is divided into sections and the method by which the area of each section is calculated. In MATLAB The quad and quadl commands are used for integration when f(x) is a function, and trapz is used when f(x) is given by data points. The quad command, uses the adaptive Simpson method of integration. The quadl command uses the adaptive Lobatto method, which can be more efficient for high accuracies and smooth integrals. The function must be written for an argument x that is a vector (use element-by-element operations) such that it calculates the value of the function for each element of x.
ODEs make sure that the function does not have a vertical asymptote between a and b. Ex) Use numerical integration to calculate the following integral: The trapz command can be used for integrating a function that is given as data points. It uses the numerical trapezoidal method of integration where x and y are vectors with the x and y coordinates of the points, respectively. The two vectors must be of the same length.
ODEs Differential equations play a crucial role in science and engineering since they are in the foundation of virtually every physical phenomenon. However, only a limited number of differential equations can be solved analytically. Numerical methods, on the other hand, can result in an approximate solution to almost any equation. An ordinary differential equation (ODE) is an equation that contains an independent variable, a dependent variable, and derivatives of the dependent variable. x and y are the independent and dependent variables, respectively. A solution is a function y=f(x) that satisfies the equation. In general, many functions can satisfy a given ODE, and more information is required for determining the solution of a specific problem.
ODEs Step 1: Write the problem in a standard form. Step 2: Create a user-defined function (in a function file) or an anonymous function. The ODE to be solved has to be written as a user-defined function or as an anonymous function. Step 3: Select a method of solution. lists seven ODE solver commands, which are MATLAB built-in functions that can be used for solving a first order ODE. Step 4: Solve the ODE. A vector that specifies the interval of the solution. The vector must have at least two elements but can have more.
ODEs In general, the solvers can be divided into two groups according to their ability to solve stiff problems and according to whether they use on-step or multistep methods Stiff problems are ones that include fast and slowly changing components and require small time steps in their solution One-step solvers use information from one point to obtain a solution at the next point. Multistep solvers use information from several previous points to find the solution at the next point. It is impossible to know ahead of time which solver is the most appropriate for a specific problem. A suggestion is to first try ode45, which gives good results for many problems. If a solution is not obtained because the problem is stiff, trying the solver ode15s is suggested.
ODEs
Examples The ideal gas equation relates the volume (V in L), temperature (T in K), pressure (P in atm), and the amount of gas (number of moles n) by: where R = 0.08206 (L atm)/(mol K) is the gas constant. The van der Waals equation gives the relationship between these quantities for a real gas by where a and b are constants that are specific for each gas. Use the fzero function to calculate the volume of 2 mol CO2 at temperature of 50 C, and pressure of 6 atm.
ODEs In order for the script and function files to work correctly, the variables P, T, n, a, b, and R are declared global. To estimate the amount of water that flows in a river during a year, a section of the river is made to have a rectangular cross section as shown. In the beginning of every month (starting at January 1st) the height h of the water and the speed v of the water flow are measured. The first day of measurement is taken as 1, and the last day which is January 1st of the next year is day 366. The following data was measured:
ODEs Use the data to calculate the flow rate, and then integrate the flow rate to obtain an estimate of the total amount of water that flows in the river during a year. The flow rate, Q (volume of water per second), at each data point is obtained by multiplying the water speed by the width and height of the cross-sectional area of the water that flows in the channel: The total amount of water that flows is estimated by the integral: