applied linear algebra in geoscience using...

27
Applied Linear Algebra in Geoscience Using MATLAB Applied Linear Algebra in Geoscience Using MATLAB

Upload: others

Post on 25-Jan-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Applied Linear Algebra in Geoscience Using MATLAB

Page 2: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Contents

Applied Linear Algebra in Geoscience Using MATLAB

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

Implementing the QR factorization

The algebraic eigenvalue problem

Linear Independence Basis of a Subspace The Rank of a Matrix

Page 3: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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

Page 4: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

LU Decomposition

We practiced the process of solving a non-singular linear system Ax = b using Gaussianelimination. We formed the augmented matrix A|b and applied the elementary row operationsas below, to reduce A to upper-triangular form.

1. Multiplying a row by a scalar.2. Subtracting a multiple of one row from another3. 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 upper-triangular matrix. If we need to solve several different systems with the same A, then wewould 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 ofGaussian elimination.

Since Gaussian elimination is used so often, the algorithm must be stable. Unfortunately, thisis not true, and we must add an operation termed partial pivoting.

Page 5: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

LU Decomposition

The first step of Gaussian elimination is to use a11 = 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. Inorder to make it clear that we are recording multipliers and not elements of A, put the entriesin parentheses. This leads to:

Thus, we see that A is the product of the lower triangular L and the upper triangular U. Whena matrix can be written as a product of simpler matrices, we call that a decomposition and thisone we call the LU decomposition.

Page 6: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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.

Page 7: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Using LU To Solve Equations

Page 8: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Using LU To Solve Equations

Page 9: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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 theLU decomposition. Use the multiplier 2/0.00001

In our three-digit arithmetic, the 1 is lost, and the result is

Now compute LU to get

which is disastrously different from A!

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 smallernumber. The result was loss of any contribution from the small number.

Page 10: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Using LU To Solve Equations

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

There is a good solution to this problem. When choosing the pivot elementon the diagonal at position aii, locate the element in column i at or below the diagonalthat is largest in magnitude, say aji, i ≤ j ≤ n. If j ≠i, interchange row j with row i, andthen the multipliers, aji/aii , satisfy |aji/aii |≤ 1 , i + 1 ≤ j ≤ n, and we avoid multiplying arow by a large number and losing precision. We call this Gaussian elimination withpartial pivoting (GEPP).

Page 11: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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.

Page 12: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Using LU To Solve Equations

NOTE: Even if a row interchange occurs when dealing with column 1, do not interchange thecorresponding rows of L until moving to column 2. Think of it this way. The matrix A after arow swap defines the starting configuration. The elements in the first column of L correspondto the multipliers after the row interchange involving pivot position (1, 1),if any.

Apply the pivot element and update L.

The final results are

Page 13: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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

Page 14: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Eq. with One Variable

Numerical methods are commonly used for solving mathematical problems that areformulated in science (and engineering) where it is difficult or impossible to obtain exactsolutions

A solution to the equation (also called a root) is a numerical value of x that satisfies theequation (graphically, crosses or touches the x axis)

If such a value does not exist or is difficult to determine, a numerical solution can bedetermined 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 thatis closer to the solution.

Page 15: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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

Eq. with One Variable

Page 16: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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 theX 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

Max or Min of a Function

In many applications there is a need to determine the local minimum or maximum of afunction

In calculus the value of x that corresponds to a local minimum or maximum is determined byfinding the zero of the derivative of the function. The value of y is determined by substitutingthe x into the function.

Page 17: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

ODEs

Local min

Abs. min

Ex)

Notice that the fminbnd command gives the local minimum.

Page 18: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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 anddisplacement are just a few examples where integrals are used.

In calculus courses the integrand (the quantity to be integrated) is usually a function. Inapplications 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 intosmall sections, calculating the area of each section, and adding Them up.

Page 19: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

Various numerical methods have been developed for this purpose. The difference betweenthe methods is in the way that the area is divided into sections and the method by which thearea of each section is calculated.

In MATLABThe 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 quadl command uses the adaptive Lobatto method, which can be more efficient for

high accuracies and smooth integrals.

The quad command, uses the adaptive Simpson method of integration.

The function must be written for an argument x that is a vector (use element-by-elementoperations) such that it calculates the value of the function for each element of x.

Numerical Integration

Page 20: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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. Ituses 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.

Page 21: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

ODEs

Differential equations play a crucial role in science and engineering since they are in thefoundation of virtually every physical phenomenon. However, only a limited number ofdifferential 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 independentvariable, 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 fordetermining the solution of a specific problem.

Page 22: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

ODEsStep 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 asan anonymous function.

Step 3: Select a method of solution.

lists seven ODE solver commands, which areMATLAB built-in functions that can be used forsolving 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.

Page 23: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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 thenext point.

It is impossible to know ahead of time which solver is the most appropriate for aspecific 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 thesolver ode15s is suggested.

Page 24: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

ODEs

Page 25: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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.

Page 26: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

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 istaken as 1, and the last day—which is January 1st of the next year—is day 366. The following data was measured:

Page 27: Applied Linear Algebra in Geoscience Using MATLABshahraki/index_htm_files/LA_Matlab-Lecture11.pdf · Applied Linear Algebra in Geoscience Using MATLAB ODEs Use the data to calculate

Applied Linear Algebra in Geoscience Using MATLAB

ODEsUse 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: