Top Banner
Numerical Mathematical Analysis Outline 1 Introduction, matlab notes 2 Chapter 1: Taylor polynomials 3 Chapter 2: Error and Computer Arithmetic 4 Chapter 4: 4.1 - 4.3 Interpolation 5 Chapter 5: Numerical integration and differentiation 6 Chapter 3: Rootfinding Numerical Mathematical Analysis Math 1070
28

Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

May 15, 2018

Download

Documents

NguyenKiet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

Numerical Mathematical Analysis

Outline

1 Introduction, matlab notes

2 Chapter 1: Taylor polynomials

3 Chapter 2: Error and Computer Arithmetic

4 Chapter 4: 4.1 - 4.3 Interpolation

5 Chapter 5: Numerical integration and differentiation

6 Chapter 3: Rootfinding

Numerical Mathematical Analysis Math 1070

Page 2: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction

Numerical Analysis

Numerical Analysis: This refers to the analysis of mathematicalproblems by numerical means, especially mathematical problemsarising from models based on calculus.Effective numerical analysis requires several things:

An understanding of the computational tool being used, be it acalculator or a computer.

An understanding of the problem to be solved.

Construction of an algorithm which will solve the givenmathematical problem to a given desired accuracy andwithin the limits of the resources (time, memory, etc) that areavailable.

1. Introduction, Matlab notes Math 1070

Page 3: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction

This is a complex undertaking. Numerous people make this their lifeswork, usually working on only a limited variety of mathematicalproblems.Within this course, we attempt to show the spirit of the subject.

Most of our time will be taken up with looking at algorithms forsolving basic problems such as rootfinding and numerical integration;but we will also look at the structure of computers and theimplications of using them in numerical calculations.

We begin by looking at the relationship of numerical analysis to thelarger world of science and engineering.

1. Introduction, Matlab notes Math 1070

Page 4: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

Traditionally, engineering and science had a two-sided approach tounderstanding a subject: the theoretical and the experimental. More recently,a third approach has become equally important: the computational.

Traditionally we would build an understanding by building theoreticalmathematical models, and we would solve these for special cases.

For example, we would study the flow of an incompressible irrotational fluidpast a sphere, obtaining some idea of the nature of fluid flow.

But more practical situations could seldom be handled by direct means,because the needed equations were too difficult to solve.

Thus we also used the experimental approach to obtain better informationabout the flow of practical fluids.

The theory would suggest ideas to be tried in the laboratory, and the

experimental results would often suggest directions for a further development

of theory.

1. Introduction, Matlab notes Math 1070

Page 5: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

With the rapid advance in powerful computers, we now can augmentthe study of fluid flow by directly solving the theoretical models of fluidflow as applied to more practical situations; and this area is oftenreferred to as computational fluid dynamics.

At the heart of computational science is numerical analysis; and toeffectively carry out a computational science approach to studying aphysical problem, we must understand the numerical analysis beingused, especially if improvements are to be made to the computationaltechniques being used.

1. Introduction, Matlab notes Math 1070

Page 6: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

Mathematical Models

A mathematical model is a mathematical description of a physicalsituation.By means of studying the model, we hope to understand more aboutthe physical situation. Such a model might be very simple. Forexample,

A = 4πR2e, Re ≈ 6, 371 km

is a formula for the surface area of the earth. How accurate is it?

1 First, it assumes the earth is sphere, which is only anapproximation. At the equator, the radius is approximately 6,378km; and at the poles, the radius is approximately 6,357 km.

2 Next, there is experimental error in determining the radius; and inaddition, the earth is not perfectly smooth.

Therefore, there are limits on the accuracy of this model for thesurface area of the earth.

1. Introduction, Matlab notes Math 1070

Page 7: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

An infectious disease model

For rubella measles, we have the following model for the spread of theinfection in a population (subject to certain assumptions).

ds

dt= −asi

di

dt= asi− bi

dr

dt= bi

In this, s, i, and r refer, respectively, to the proportions of a totalpopulation that are susceptible, infectious, and removed (from thesusceptible and infectious pool of people). All variables are functionsof time t. The constants can be taken as

a =6.811, b =

111.

1. Introduction, Matlab notes Math 1070

Page 8: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

Mathematical Models

The same model works for some other diseases (e.g. flu), with asuitable change of the constants a and b. Again, this is anapproximation of reality (and a useful one).

But it has its limits. Solving a bad model will not give good results, nomatter how accurately it is solved; and the person solving this modeland using the results must know enough about the formation ofthe model to be able to correctly interpret the numerical results.

1. Introduction, Matlab notes Math 1070

Page 9: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

The logistic equation

This is the simplest model for population growth. Let N(t) denote thenumber of individuals in a population (rabbits, people, bacteria, etc).Then we model its growth by

N ′(t) = cN(t), t ≥ 0, N(t0) = N0

The constant c is the growth constant, and it usually must bedetermined empirically.

Over short periods of time, this is often an accurate model forpopulation growth. For example, it accurately models the growth ofUS population over the period of 1790 to 1860, with c = 0.2975.

1. Introduction, Matlab notes Math 1070

Page 10: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

The predator-prey model

Let F (t) denote the number of foxes at time t; and let R(t) denote thenumber of rabbits at time t. A simple model for these populations is calledthe Lotka-Volterra predator-prey model:

dR

dt= a[1− bF (t)]R(t)

dF

dt= c[−1 + dR(t)]F (t)

with a, b, c, d positive constants.If one looks carefully at this, then one can see how it is built from the logisticequation. In some cases, this is a very useful model and agrees with physicalexperiments.

Of course, we can substitute other interpretations, replacing foxes and rabbits

with other predator and prey. The model will fail, however, when there are

other populations that affect the first two populations in a significant way.

1. Introduction, Matlab notes Math 1070

Page 11: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

1. Introduction, Matlab notes Math 1070

Page 12: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

Newton’s second law

Newtons second law states that the force acting on an object is directlyproportional to the product of its mass and acceleration,

F ∝ ma

With a suitable choice of physical units, we usually write this in its scalarform as

F = ma

Newtons law of gravitation for a two-body situation, say the earth and anobject moving about the earth is then

md2r(t)dt2

= −Gmme

|r(t)|2· r(t)|r(t)|

with r(t) the vector from the center of the earth to the center of the object

moving about the earth. The constant G is the gravitational constant, not

dependent on the earth; and m and me are the masses, respectively of the

object and the earth.

1. Introduction, Matlab notes Math 1070

Page 13: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Introduction > Science

This is an accurate model for many purposes. But what are somephysical situations under which it will fail? When the object is veryclose to the surface of the earth and does not move far from one spot,we take |r(t)| to be the radius of the earth. We obtain the new model

md2r(t)dt2

= −mgk

with k the unit vector directly upward from the earths surface at thelocation of the object. The gravitational constant

g.= 9.8 meters/second2

Again this is a model; it is not physical reality.

1. Introduction, Matlab notes Math 1070

Page 14: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Matlab notes

Matlab designed for numerical computing.

Strongly oriented towards use of arrays, one and two dimensional.

Excellent graphics that are easy to use.

Powerful interactive facilities; and programs can also be written init.

It is a procedural language, not an object-oriented language.

It has facilities for working with both Fortran and C languageprograms.

1. Introduction, Matlab notes Math 1070

Page 15: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

At the prompt in Unix or Linux, type Matlab.Or click the Red Hat, then DIVMS, then Mathematics, then Matlab.Run the demo program (simply type demo). Then select one of themany available demos.To seek help on any command, simply type

help commandor use the online Help command. To seek information on Matlabcommands that involve a given word in their description, type

lookfor wordLook at the various online manuals available thru the help page.

1. Introduction, Matlab notes Math 1070

Page 16: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

MATLAB is an interactive computer language.For example, to evaluate

y = 6− 4x+ 7x2 − 3x5 +3

x+ 2

usey = 6− 4 ∗ x+ 7 ∗ x ∗ x− 3 ∗ x5 + 3/(x+ 2);

There are many built-in functions, e.g.

exp(x), cos(x),√x, log(x)

The default arithmetic used in MATLAB is double precision (16decimal digits and magnitude range 10−308 : 10+308) and real.However, complex arithmetic appears automatically when needed.

sqrt(-4) results in an answer of 2i.

1. Introduction, Matlab notes Math 1070

Page 17: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

The default output to the screen is to have 4 digits to the right of thedecimal point.To control the formatting of output to the screen, use the commandformat. The default formatting is obtained using

format shortTo obtain the full accuracy available in a number, you can use

format longThe commands

format short eformat long e

will use ‘scientific notation’ for the output. Other format options arealso available.

1. Introduction, Matlab notes Math 1070

Page 18: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

SEE plot trig.m

MATLAB works very efficiently with arrays, and many tasks are best donewith arrays. For example, plot sinx and cosx on the interval 0 ≤ x ≤ 10.

t = 0 : .1 : 10;x = cos(t); y = sin(t);plot(t, x, t, y, ’LineWidth’, 4)

0 1 2 3 4 5 6 7 8 9 10−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Figure: SEE plot trig.m

1. Introduction, Matlab notes Math 1070

Page 19: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

The statementt = a : h : b;

with h > 0 creates a row vector of the form

t = [a, a+ h, a+ 2h, . . .]

giving all values a+ jh that are less than b.When h is omitted, it is assumed to be 1. Thus

n = 1 : 5

creates the row vectorn = [1, 2, 3, 4, 5]

1. Introduction, Matlab notes Math 1070

Page 20: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Arrays

b = [1, 2, 3]

creates a row vector of length 3.

A = [1 2 3; 4 5 6; 7 8 9]

creates the square matrix

A =

1 2 34 5 67 8 9

Spaces or commas can be used as delimiters in giving the components of anarray; and a semicolon will separate the various rows of a matrix.For a column vector,

b = [1 3 − 6]′

results in the column vector

13−6

1. Introduction, Matlab notes Math 1070

Page 21: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Array Operations

Addition: Do componentwise addition.

A = [1, 2; 3,−2;−6, 1];B = [2, 3;−3, 2; 2,−2];C = A+B;

results in the answer

C =

3 50 0−4 −1

Multiplication by a constant: Multiply the constant times each componentof the array.

D = 2 ∗A;

results in the answer

D =

2 46 −4

−12 2

1. Introduction, Matlab notes Math 1070

Page 22: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Array Operations

Matrix multiplication: This has the standard meaning.

E = [1, −2; 2, −1; −3, 2]F = [2, −1, 3; −1, 2, 3];G = E ∗ F ;

results in the answer

G =

1 −22 −1−3 2

[ 2 −1 3−1 2 3

]=

4 −5 −35 −4 3−8 7 −3

A nonstandard notation:

H = 3 + F ;

results in the computation

H = 3[

1 1 11 1 1

]+[

2 −1 3−1 2 3

]=[

5 2 62 5 6

]1. Introduction, Matlab notes Math 1070

Page 23: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Componentwise operations

Matlab also has component-wise operations for multiplication, division andexponentiation. These three operations are denoted by using a period toprecede the usual symbol for the operation. With

a = [1 2 3]; b = [2 −, 1 4];

we have

a.∗b = [2 − 2 12]a./b = [.5 − 2.0 0.75]a.̂ 3 = [1 8 27]2.̂ a = [2 4 8]b.̂ a = [2 1 64]

The expression

y = 6− 4x+ 7x2 − 3x5 +3

x+ 2can be evaluated at all of the elements of an array x using the command

y = 6− 4 ∗ x+ 7 ∗ x.∗x− 3 ∗ x.̂ 5 + 3./(x+ 2);

The output y is then an array of the same size as x.

1. Introduction, Matlab notes Math 1070

Page 24: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Special arrays

A = zeros(2, 3)

produces an array with 2 rows and 3 columns, with all components setto zero, [

0 0 00 0 0

]B = ones(2, 3)

produces an array with 2 rows and 3 columns, with all components setto 1, [

1 1 11 1 1

]eye(3) results in the 3× 3 identity matrix, 1 0 0

0 1 00 0 1

1. Introduction, Matlab notes Math 1070

Page 25: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Array functions

There are many MATLAB commands that operate on arrays, weinclude only a very few here.For a vector x, row or column, of length n, we have the followingfunctions.

max(x) = maximum component of xmin(x) = minimum component of xabs(x) = vector of absolute values of components of xsum(x) = sum of the components of x

norm(x)=√|x1|2 + · · ·+ |xn|2

1. Introduction, Matlab notes Math 1070

Page 26: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Script files

A list of interactive commands can be stored as a script file.For example, store

t = 0 : .1 : 10;x = cos(t); y = sin(t);plot(t, x, t, y)

with the file name plot trig.m. Then to run the program, give thecommand

plot trigThe variables used in the script file will be stored locally, andparameters given locally are available for use by the script file.

1. Introduction, Matlab notes Math 1070

Page 27: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Functions

To create a function, we proceed similarly, but now there are input andoutput parameters. Consider a function for evaluating the polynomial

p(x) = a1 + a2x+ a3x2 + · · ·+ anx

n−1

MATLAB does not allow zero subscripts for arrays.The following function would be stored under the name polyeval.m.The coefficients {aj} are given to the function in the array namedcoeff, and the polynomial is to be evaluated at all of the componentsof the array x.

1. Introduction, Matlab notes Math 1070

Page 28: Introduction, matlab notes Chapter 1: Taylor polynomials ... · Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their

> Matlab notes

Functions

function value = polyeval(x,coeff);%% function value = polyeval(x,coeff)%% Evaluate a polynomial at the points given in x.% The coefficients are to be given in coeff.% The constant term in the polynomial is coeff(1).

n = length(coeff)value = coeff(n)*ones(size(x));for i = n-1:-1:1value = coeff(i) + x.*value;end

>> polyeval(3,[1,2])yieldsn=2

ans = 7

1. Introduction, Matlab notes Math 1070