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 > Introduction Numerical Analysis Numerical Analysis: This refers to the analysis of mathematical problems by numerical means, especially mathematical problems arising from models based on calculus. Effective numerical analysis requires several things: An understanding of the computational tool being used, be it a calculator or a computer. An understanding of the problem to be solved. Construction of an algorithm which will solve the given mathematical problem to a given desired accuracy and within the limits of the resources (time, memory, etc) that are available. 1. Introduction, Matlab notes Math 1070 > Introduction This is a complex undertaking. Numerous people make this their lifes work, usually working on only a limited variety of mathematical problems. Within this course, we attempt to show the spirit of the subject. Most of our time will be taken up with looking at algorithms for solving basic problems such as rootfinding and numerical integration; but we will also look at the structure of computers and the implications of using them in numerical calculations. We begin by looking at the relationship of numerical analysis to the larger world of science and engineering. 1. Introduction, Matlab notes Math 1070 > Introduction > Science Traditionally, engineering and science had a two-sided approach to understanding 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 theoretical mathematical models, and we would solve these for special cases. For example, we would study the flow of an incompressible irrotational fluid past 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 information about 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 > Introduction > Science With the rapid advance in powerful computers, we now can augment the study of fluid flow by directly solving the theoretical models of fluid flow as applied to more practical situations; and this area is often referred to as computational fluid dynamics. At the heart of computational science is numerical analysis; and to effectively carry out a computational science approach to studying a physical problem, we must understand the numerical analysis being used, especially if improvements are to be made to the computational techniques being used. 1. Introduction, Matlab notes Math 1070 > Introduction > Science Mathematical Models A mathematical model is a mathematical description of a physical situation. By means of studying the model, we hope to understand more about the physical situation. Such a model might be very simple. For example, A = 4πRe2 , 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 an approximation. At the equator, the radius is approximately 6,378 km; and at the poles, the radius is approximately 6,357 km. 2 Next, there is experimental error in determining the radius; and in addition, the earth is not perfectly smooth. Therefore, there are limits on the accuracy of this model for the surface area of the earth. 1. Introduction, Matlab notes Math 1070 > Introduction > Science An infectious disease model For rubella measles, we have the following model for the spread of the infection in a population (subject to certain assumptions). ds = −asi dt di = asi − bi dt dr = bi dt In this, s, i, and r refer, respectively, to the proportions of a total population that are susceptible, infectious, and removed (from the susceptible and infectious pool of people). All variables are functions of time t. The constants can be taken as a= 1. Introduction, Matlab notes 6.8 , 11 b= 1 . 11 Math 1070 > Introduction > Science Mathematical Models The same model works for some other diseases (e.g. flu), with a suitable change of the constants a and b. Again, this is an approximation of reality (and a useful one). But it has its limits. Solving a bad model will not give good results, no matter how accurately it is solved; and the person solving this model and using the results must know enough about the formation of the model to be able to correctly interpret the numerical results. 1. Introduction, Matlab notes Math 1070 > Introduction > Science The logistic equation This is the simplest model for population growth. Let N (t) denote the number of individuals in a population (rabbits, people, bacteria, etc). Then we model its growth by N 0 (t) = cN (t), t ≥ 0, N (t0 ) = N0 The constant c is the growth constant, and it usually must be determined empirically. Over short periods of time, this is often an accurate model for population growth. For example, it accurately models the growth of US population over the period of 1790 to 1860, with c = 0.2975. 1. Introduction, Matlab notes Math 1070 > Introduction > Science The predator-prey model Let F (t) denote the number of foxes at time t; and let R(t) denote the number of rabbits at time t. A simple model for these populations is called the Lotka-Volterra predator-prey model: dR = a[1 − bF (t)]R(t) dt dF = c[−1 + dR(t)]F (t) dt with a, b, c, d positive constants. If one looks carefully at this, then one can see how it is built from the logistic equation. In some cases, this is a very useful model and agrees with physical experiments. 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 > Introduction > Science 1. Introduction, Matlab notes Math 1070 > Introduction > Science Newton’s second law Newtons second law states that the force acting on an object is directly proportional to the product of its mass and acceleration, F ∝ ma With a suitable choice of physical units, we usually write this in its scalar form as F = ma Newtons law of gravitation for a two-body situation, say the earth and an object moving about the earth is then m d2 r(t) Gm me r(t) =− · 2 dt |r(t)|2 |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 > Introduction > Science This is an accurate model for many purposes. But what are some physical situations under which it will fail? When the object is very close 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 m d2 r(t) = −mgk dt2 with k the unit vector directly upward from the earths surface at the location 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 > 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 in it. It is a procedural language, not an object-oriented language. It has facilities for working with both Fortran and C language programs. 1. Introduction, Matlab notes Math 1070 > 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 the many available demos. To seek help on any command, simply type help command or use the online Help command. To seek information on Matlab commands that involve a given word in their description, type lookfor word Look at the various online manuals available thru the help page. 1. Introduction, Matlab notes Math 1070 > Matlab notes MATLAB is an interactive computer language. For example, to evaluate y = 6 − 4x + 7x2 − 3x5 + 3 x+2 use y = 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 (16 decimal 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 > Matlab notes The default output to the screen is to have 4 digits to the right of the decimal point. To control the formatting of output to the screen, use the command format. The default formatting is obtained using format short To obtain the full accuracy available in a number, you can use format long The commands format short e format long e will use ‘scientific notation’ for the output. Other format options are also available. 1. Introduction, Matlab notes Math 1070 > Matlab notes SEE plot trig.m MATLAB works very efficiently with arrays, and many tasks are best done with arrays. For example, plot sin x and cos x on the interval 0 ≤ x ≤ 10. t = 0 : .1 : 10; x = cos(t); y = sin(t); plot(t, x, t, y, ’LineWidth’, 4) 1 0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 0 1. Introduction, Matlab notes 1 2 3 4 5 6 7 8 9 10 Math 1070 > Matlab notes The statement t = 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 vector n = [1, 2, 3, 4, 5] 1. Introduction, Matlab notes Math 1070 > 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 123 A= 456 789 Spaces or commas can be used as delimiters in giving the components of an array; and a semicolon will separate the various rows of a matrix. For a column vector, b = [1 3 − 6]0 1 results in the column vector 3 −6 1. Introduction, Matlab notes Math 1070 > 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 3 5 0 C= 0 −4 −1 Multiplication by a constant: Multiply the constant times each component of the array. D = 2 ∗ A; results in the answer 1. Introduction, Matlab notes 2 4 6 −4 D= −12 2 Math 1070 > 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 4 −5 −3 1 −2 2 −1 3 3 = 5 −4 G = 2 −1 −1 2 3 −8 7 −3 −3 2 A nonstandard notation: H = 3 + F; results in the computation 1 1 1 2 −1 3 5 2 6 H=3 + = 1 1 1 −1 2 3 2 5 6 1. Introduction, Matlab notes Math 1070 > Matlab notes Componentwise operations Matlab also has component-wise operations for multiplication, division and exponentiation. These three operations are denoted by using a period to precede 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 3 x+2 can be evaluated at all of the elements of an array x using the command y = 6 − 4x + 7x2 − 3x5 + 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 > Matlab notes Special arrays A = zeros(2, 3) produces an array with 2 rows and 3 columns, with all components set to zero, 0 0 0 0 0 0 B = ones(2, 3) produces an array with 2 rows and 3 columns, with all components set to 1, 1 1 1 1 1 1 eye(3) results in the 3 × 3 identity matrix, 1 0 0 0 1 0 0 0 1 1. Introduction, Matlab notes Math 1070 > Matlab notes Array functions There are many MATLAB commands that operate on arrays, we include only a very few here. For a vector x, row or column, of length n, we have the following functions. max(x) = maximum component of x min(x) = minimum component of x abs(x) = vector of absolute values of components of x sum(x) = sum p of the components of x norm(x)= |x1 |2 + · · · + |xn |2 1. Introduction, Matlab notes Math 1070 > 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 the command plot trig The variables used in the script file will be stored locally, and parameters given locally are available for use by the script file. 1. Introduction, Matlab notes Math 1070 > Matlab notes Functions To create a function, we proceed similarly, but now there are input and output parameters. Consider a function for evaluating the polynomial p(x) = a1 + a2 x + a3 x2 + · · · + an xn−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 named coeff, and the polynomial is to be evaluated at all of the components of the array x. 1. Introduction, Matlab notes Math 1070 > 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:1 value = coeff(i) + x.*value; end >> polyeval(3,[1,2]) yields n=2 ans = 7 1. Introduction, Matlab notes Math 1070
© Copyright 2024 Paperzz