Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lectures 1 2 L t 1-2
Logistics
Lectures: 09:00-11:00 Mon-Tue and Thu-Fri
announced the day before!!) (location will be
Lab Sessions: Tue 11:00 13:00 (Lab Instructor : Marianna Papadomanolaki) Textbook: Numerical Methods for Engineers by Steven C. Chapra and
Raymond P Canale McGraw Hill N Y 5th edition 2006 P. Canale, Hill, N.Y., edition, 2006.
Course Evaluation (under negotiation!): Quizzes (30%), Assignments (25%) and a Final Exam (45%) (closed books closed notes) adelis@science.tuc.gr
Dr A.I. Delis TUC 2010
Dr A.I. Delis TUC 2010
Motivation
Ordinary differential equations: very important in engineering practice, because many physical laws are couched in terms of the rate of change of a quantity rather than the magnitude of the q g q y g quantity itself. Initial value and boundary value y y problems. Partial differential equations: used to characterize engineering systems where the behavior of a physical quantity is couched in terms of the rate of change with respect to two or more independent variables. Examples: steady-state distribution of temperature of a heated plate (two spatial dimensions) or the time-variable temperature of a heated rod (time and one spatial dimension), fluid flows etc.. Dr A.I. Delis TUC 2010
Computers are great tools, however, without fundamental understanding of engineering problems, g g g p , they will be useless.
Dr A.I. Delis TUC 2010
Dependent Variable =f
Dependent variable: Characteristic that usually reflects the state of the system I d Independent variables: Dimensions s ch as time and space d t i bl : such along which the systems behavior is being determined Parameters: reflect the systems properties or composition system s Forcing functions: external influences acting upon the system
10
11
F Formulation of N t 2nd l l ti f Newtons law h several has l characteristics that are typical of mathematical models of the physical world:
It describes a natural process or system in p y mathematical terms It represents an idealization and simplification of reality (focuses on its essential manifestations). Finally it yields reproducible results consequently Finally, results, consequently, can be used for predictive purposes, e.g. a = m/F /F
12
Some mathematical models of physical phenomena may be much more complex. Complex models may not be solved exactly or Co p e ode s y o so ved e c y o require more sophisticated mathematical techniques than simple algebra for their solution. p g
Example modeling of a falling parachutist: Example,
13
dv F d = dt m F = FD + FU FD = mg FU = cv dv mg cv g = dt m
Dr A.I. Delis TUC 2010
v=t terminal velocity ( / ) i l l it (m/s) t = time (s) m = mass of the object ( g) j (kg)
g = gravitational constant (9.81 m/s2)
14
dv c =g v dt m
This is a differential equation and is written in terms of the differential rate of change dv/dt of the variable that we are interested in predicting. If the parachutist is initially at rest (v=0 at t=0), using calculus we get an analytical (or exact) solution
Independent variable
Parameters
15
Solution for differential equation cannot always be solved analytically using simple algebraic solution solution. The exact solution for differential equation can be solved using calculus or by g y approximation using numerical methods.
16
We have
thus,
dv =0 dt
mg v= c
17
18
Fig 1.6
For steady-state incompressible fluid flow in pipes: Flow in = Flow out or 100 + 80 = 120 + Flow4 Flow4 = 60
In general we are interested in conservation of mass, momentum and energy
Dr A.I. Delis TUC 2010
19
Sensitivity Analysis: how a system responds to different Design: determine a systems parameters to a required performance.
Dr A.I. Delis TUC 2010
20
Objective is how to use the computer as a tool g to obtain numerical solutions to a given engineering model. There are two ways in g p using computers:
Use available software Or write computer programs to extend the Or, capabilities of available software, such as Excel and Matlab.
Engineers should not be tool limited, it is important that they should be able to do both!
Dr A.I. Delis TUC 2010
21
Computer programs are set of instructions that direct the p p g computer to perform a certain task. To be able to perform engineering-oriented numerical calculations, you should b f ili l l ti h ld be familiar with th f ll i ith the following programming topics:
Simple information representation (constants, variables, and type p f p ( , , yp declaration) Advanced information representation (data structure, arrays, and records) Mathematical formulas (assignment, priority rules, and intrinsic functions) Input/Output Logical representation (sequence, selection, and repetition) Modular programming (functions and subroutines)
We will focus the last two topics, assuming that you have some prior exposure to programming.
Dr A.I. Delis TUC 2010
22
Structured Programming
Structured programming is a set of rules that prescribe god style habits for programmer.
An organized well structured code organized, Easily sharable Easy to debug and test Requires shorter time to develop, test, and update
Th k id i th t any numerical algorithm can The key idea is that i l l ith be composed of using the three fundamental structures: t t
Sequence, selection, and repetition
Dr A.I. Delis TUC 2010
23
24
Sequence: Computer code must be implemented one p instruction at a time, unless you instruct y otherwise. The structure can be expressed as a flowchart or pseudocode.
25
Selection: Splits the programs flow into branches based on b d outcome of a logical condition condition.
26
27
Modular Programming
The computer programs can be divided into subprograms, or modules, that can be developed and tested separately. Modules should b as i d d l h ld be independent and self contained d d lf i d as possible. Advantages to modular d i are: d d l design
It is easier to understand the underlying logic of smaller modules They are easier to debug and test Facilitate program maintenance and modification Allow you to maintain your own library of modules for later use
Part 1 (Chapters 1-4) 28
29
How to do it??
1) Install MATLAB software in your notebook. 2) Explore yourself the Appendix B in Chapra ( ) and Canale (2006). 3) Try any other exercises. 4) Check th answers MATLAB gave Ch k the and make sure you understand them.
Dr A.I. Delis TUC 2010
30
31
How confident we are in our approximate result? The question is how much error is present in our calculation and is it tolerable? tolerable?
Dr A.I. Delis TUC 2010
32
Accuracy. How close is a computed or measured value to the true value Precision (or reproducibility). How close is a computed or measured value to previously computed or measured values. p Inaccuracy (or bias). A systematic deviation from the actual value value. Imprecision (or uncertainty). Magnitude of scatter.
Dr A.I. Delis TUC 2010
33
Accuracy?
Inaccuracy?
Precision? Imprecision? I i i ?
34
Numerical error equal to discrepancy between pp the truth and approximation: true error
E t = t r u e v a l u e a p p r o x i m a ti o n
True percent relative error,
t r u e e r r o r 100% t = tr u e v a l u e
Dr A.I. Delis TUC 2010
35
Error Definitions
True Value = Approximation + Error Et = True value Approximation ( / ) (+/-)
True error
true error True fractional l ti T f ti l relative error = true value true error 100% True percent relative error, t = true value
Dr A.I. Delis TUC 2010
36
If we cannot solved the problem analytically to get the true value, how to calculate its true error? We normalize the error to approximate value. Numerical methods use iterative approach to compute answers A present approximation is answers. made on the basis of a previous approximation. Percent relative error, a
c u r r e n t a p p r o x .- p r e v i o u s a p p r o x .100% a = c u rr e nt a p p r o x.
Dr A.I. Delis TUC 2010
37
For numerical methods, the true value will be known only when we deal with functions that y can be solved analytically (simple systems). In real world applications, we usually not know the pp , y answer a priori. Then
38
The a may be in + or signs. But, the most important is its absolute value value. The Th calculation should proceed until th l l ti h ld d til the absolute value of a lower than percent tolerance given, s .
a < s
Dr A.I. Delis TUC 2010
39
Result is correct/almost exact after the iteration to at least n significant figures. , , n= 1,2,3.
s = 0.510
2 n
)%
40
Use absolute value. Computations are repeated until stopping criterion is satisfied. satisfied
a s
s = (0.5 10(2-n) )%
you can be sure that the result is correct to at least n significant figures.
Dr A.I. Delis TUC 2010
41
Significant Figures g g
Number of significant figures indicates precision. Significant digits of a number are those that can be used with confidence, e.g., the number of certain di i plus one estimated di i i digits l i d digit. 53,800 How many significant figures? 5.38 x 104 5.380 x 104 5.3800 x 104 3 4 5
Zeros are sometimes used to locate the decimal point not significant figures. 0.00001753 0 00001753 0.0001753 0.001753
Dr A.I. Delis TUC 2010
4 4 4
Part 1 (Chapters 1-4) 42
m be
mantissa
43
Figure 3.3
44
Figure 3.4
45
Figure 3.5
46
156.78
Normalized to remove the leading zeroes. g Multiply the mantissa by 10 and lower the exponent by 1 p y 0.2941 x 10-1 Additional significant figure
is retained
Part 1 (Chapters 1-4) 47
1 m <1 b
Therefore for base-10 system f ab 10 t 0.1 m<1 01 1 for a base-2 system 0.5 m<1 See example 3.4
Floating point representation allows both fractions and very l large numbers t b expressed on th computer. b to be d the t However, Floating point numbers take up more room room. Take longer to process than integer numbers. Round-off errors are introduced because mantissa Round off holds only a finite number of significant figures.
48
Significance of floating point representation Strange behavior of the machine numbers 1+x 1 = 1 !!! E = For F
x b1- t
b1-t
Limited range of quantities that may be represented g q y p (overflow and underflow may occur) Fi it number of quantities can b represented within th Finite b f titi be t d ithi the range (precision is limited e.g. irrational numbers)
49
50
Types of errors: T T f Truncation error ti Truncation errors are th T ti those th t result f that lt from using an approximation in place of an exact mathematical procedure.
51
The T l 4 i Th Taylor series Chapter A mathematical formulation that used widely in numerical methods to predict a function value i approximate f hi f ti l in i t fashion. Why it is called in series? Its build term by term, started with zero-order y , approximation. The higher the order of approximation applied the lower the applied, truncation error.
Dr A.I. Delis TUC 2010
Taylor Series y
52
Non-elementary functions such as trigonometric, y g , exponential, and others are expressed in an approximate fashion using Taylor series when their pp g y values, derivatives, and integrals are computed. Any smooth function can be approximated as a polynomial. polynomial Taylor series provides a means to predict the value of a function at one point in terms of the function value and its derivatives at another point.
53
(xi+1-xi)= h
( n +1)
f ( ) ( n +1) Rn = h (n + 1)!
R i d term, Rn, accounts for all terms Reminder t t f ll t from (n+1) to infinity.
Dr A.I. Delis TUC 2010
54
55
Zero-order approximation:
f ( xi +1 ) f ( xi )
Additional terms of the Taylor series are required to provide a better estimate. First order First-order approximation:
f ( xi +1 ) f ( xi ) + f ' ( xi )( xi +1 xi )
The additional term consist of a slope multiply the distance between x i and x i + 1 .
Dr A.I. Delis TUC 2010
56
Second-order approximation:
f ' ' (xi ) 2 f (xi+1) f (xi ) + f ' (xi )(xi+1 xi ) + (xi+1 x1) 2!
and so on d
f ' ' (xi ) 2 f (xi+1) f (xi ) + f ' (xi )(xi+1 xi ) + (xi+1 xi ) 2! (3) (n) f (xi ) f (xi ) n 3 + (xi+1 xi ) +.......... + (xi+1 xi ) + Rn 3! n! where Rn is the remainder term
Dr A.I. Delis TUC 2010
57
Simplifying h = ( x i + 1 x i ) , hence,
58
i not k is known exactly, li l lies somewhere h between xi+1> >xi . Need to determine f n+1(x), to do this you need ( ) f'(x). If we knew f(x), there wouldnt be any need to perform the Taylor series expansion expansion. However, R=O(hn+1), (n+1)th order, the order of truncation error i hn+1. t ti is O(h), halving the step size will halve the error. O(h2), halving the step size will quarter the error.
Dr A.I. Delis TUC 2010
59
x2 x4 x6 cos x = 1 + + 2! 4! 6!
If x=0.5 ( ) cos(0.5) =1-0.125+0.0026041-0.0000127+ =0.877582 From the supporting theory, for this series the error theory series, is no greater than the first omitted term.
x8 8!
Dr A.I. Delis TUC 2010
for
x = 0.5 = 0.0000001
Part 1 (Chapters 1-4) 60
Fig 4 2 4.2
61
How to solve the derivatives of an equation given using Taylor series? We use an approximation using numerical differentiation with: a) F ) Forward divided diff d di id d difference b) Backward divided difference c) Centered divided difference They Th are d developed f l d from th T l series the Taylor i to approximate derivatives numerically.
Dr A.I. Delis TUC 2010
62
where h
O (h) =
(2)
( xi )( xi +1 xi ) 2!
O (h ) is an truncation error.
63
f ( xi ) f ( xi 1 ) f ' ( xi ) = xi ( xi 1 )
c) Centered divided difference (1st derivative) f ( xi +1 ) f ( xi 1 ) f ' ( xi ) = + O(h 2 ) 2( xi +1 xi ) where h
Dr A.I. Delis TUC 2010
f ( 3) ( xi )( xi +1 xi ) 2 O (h 2 ) = 3!
Part 1 (Chapters 1-4) 64
Fig 4 6 4.6
65
Error Propagation
fl(x) refers to the floating point (or computer) p representation of the real number x. Because a computer can hold a finite number of significant figures for a given number there number, may be an error (round-off error) associated with th fl ti ith the floating point representation. Th i t t ti The error is determined by the precision of the computer ().
Dr A.I. Delis TUC 2010
66
Suppose that we have a function f(x) that is dependent on a single independent variable x. fl(x) is an approximation of x and we would like to estimate the effect of discrepancy between x and fl(x) on the value of the function:
f ( x fl ) = f ( x) f ( x fl ) both f(x) and xfl are unknown
Employ Taylor series to compute f(x) near f(x fl ), dropping the second and higher order terms f ( x) f ( x fl ) f ( x fl )( x x fl )
67
Fig 4 7 4.7
68
Also, let t, the fractional relative error, be the error associated with fl(x). Then
fl ( x) x x
= t
where t
Rearranging, we get
fl ( x) x = t x f fl ( x) = x( t + 1)
Dr A.I. Delis TUC 2010
69
Case 1: Addition of x1 and x2 with associated errors t1 and t2 yields the following result: d i ld th f ll i lt fl(x1)=x1(1+t1) fl(x2)=x2(1+t2) fl(x1)+fl(x2)=t1 x1+t2 x2+x1+x2
fl ( x1 ) + fl ( x2 ) ( x1 + x2 ) t1 x1 + t 2 x2 = = 100% x1 + x2 x1 + x2
A large error could result from addition if x1 and x2 are A almost equal magnitude but opposite sign, therefore one should avoid subtracting nearly equal numbers. numbers
Dr A.I. Delis TUC 2010
70
Generalization: Suppose the numbers fl(x1) fl(x2) fl(x3) , fl(xn) are ), ), ), approximations to x1, x2, x3, ,xn and that in each case the maximum possible error is E. E Eti E fl(xi)-E xi fl(xi)+E It f ll follows by addition that b dditi th t
fl ( x ) nE x fl ( x ) + nE
i i i
So that
nE xi fl ( xi ) nE
Therefore, the maximum possible error in the sum of fl(xi) is nE .
Dr A.I. Delis TUC 2010
71
Case 2: Multiplication of x1 and x2 with associated errors et1 and et2 results in:
fl ( x1 ) fl ( x2 ) = x1 (1 + t1 ) x2 (1 + t 2 ) fl ( x1 ) fl ( x2 ) = x1 x2 ( t1 t 2 + t1 + t 2 + 1) fl ( x1 ) fl ( x2 ) x1 x2 = = t1 t 2 + t1 + t 2 100% x1 x2
72
Since t1, t2 are both small, the term t1t2 should be small relative to t1+t2. Thus the magnitude of the error associated with one multiplication or division step should be t1+t2. t1 (upper bound) Although error of one calculation may not be significant, if 1000 calculations were done, the error g , , is then approximately 1000. The magnitude of error associated with a calculation is directly proportional to the number of multiplication steps. Refer to Table 4.3
Dr A.I. Delis TUC 2010
73
Overflow: Any number larger than the largest number that can be e pressed on a comp ter will res lt in an o erflo expressed computer ill result overflow. Underflow (Hole) : Any positive number smaller than the smallest number that can be represented on a computer will result an underflow. Stable Algorithm: In extended calculations, it is likely that many round-offs will be made. Each of these plays the role of an input error for the remainder of the computation, impacting the eventual output. Algorithms for which the cumulative effect of all such errors are limited, so that a useful result is generated, are called stable algorithms. When accumulation is devastating and th solution i overwhelmed b th error, i d t ti d the l ti is h l d by the such algorithms are called unstable.
74
and can be used to compute the exact value as f ' ( x) =-0.9125. (x 0.9125. a) Use forward, backward and centered difference approximations t estimate the first derivative. i ti to ti t th fi t d i ti b) Calculate the true percent relative error, t .
Dr A.I. Delis TUC 2010
75
HOMEWORK 1
Chapra and Canale (2006): Problems 3.1,3.2,3.6,3.7,3.10 Problems 4.1,4.2,4.4,4.6,4.7
76
Trade-offs Type of Mathematical Problem (Model) Type, Availability, Cost, and Speed of Computer Program Development Cost vs Software Cost Characteristics of the Numerical Method:
1. 2. 3. 4. 5. 6. 7. Number of Initial Guesses Rate of Convergence g Stability (of numerical algorithm) Accuracy and Precision Breadth of Application Special Requirements Programming Effort
Maintenance (of programming codes) Ease of Application (user-friendly?) Mathematical Behavior:(Ill or Well-) Conditioned (function, data ..)
Dr A.I. Delis TUC 2007
77