cee203 numerical methods in electrical engineering assist. prof. dr. Çağatay uluiŞik...
TRANSCRIPT
CEE203NUMERICAL METHODS
IN ELECTRICAL ENGINEERING
Assist. Prof. Dr. Çağatay ULUIŞIK
2
OUTLINE
Introducing MatLab
Fundamental Terms, Basic Definitions
Solving Nonlinear Equations – Root Finding
Solving Systems of Linear Equations
Taylor Series
Numerical Differentiation
Numerical Integration
Curve Fitting and Interpolation
Ordinary Differential Equations
3
INTRODUCING MATLAB
Vectors, Arrays and Matrices
Assign a value to a variable (observe the effect of the semicolon):
Creating a row vector of size (14) Creating a column vector of size (41)
Creating an array of size (19) using the colon sign (:)
4
INTRODUCING MATLAB
Creating an array of size (19) using the for loop :
Creating a matrix of size (33) : Creating a matrix of size (33) using 2 for loops :
5
BASIC MATRIX OPERATIONS IN MATLAB
6
GRAPHIC UTILITIES OF MATLAB
7
GRAPHIC UTILITIES OF MATLAB
8
COMPLEX NUMBERS IN MATLAB
Addition : Subtraction : Multiplication : Division :
Conjugate : Magnitude : Angle in Radians : Angle in Degrees :
Defining a Complex Number :
9
LOADING DATA FROM A FILE AND SAVING DATA TO A FILE
10
STATISTICS IN MATLAB
Mean Value : Variance : Standard Deviation :
Generation of Uniformly Distributed Random Numbers :
Generation of Uniformly Distributed Random Integers :
11
INTRODUCING MATLAB
432)( 23 xxxxP
Roots of a Polynom :
Symbolic Integration : Numeric Integration :
clc : To clear the command windowclear all : To clear all the variables in memory
12
FORMAT OF DATA
roundRound towards nearest integer
floorRound towards minus infinity
ceilRound towards plus infinity
13
EXAMPLES OF MATLAB SCRIPTSExample 1 : Calculating of the sum of integers from 1 to a user-specifed value n
a) using a formula b)using iterations
Example 2 : Creating and Calling a function
14
EXAMPLES OF MATLAB SCRIPTS
Example 3 : Error of the first n terms in the Taylor Series of the function ex
a) Calculating the error for a given n value
b) Calculating the minimum required number of terms n for a given error.
a) b)
15
Accuracy: Closeness of agreement between a measured / computed value and a true value.
Measurement accuracy: the ability of an instrument to measure the true value to within some stated error specifications.
Numerical accuracy: the degree to which the numerical solution to the approximate physical problem approximates the exact solution to the approximate physical problem.
FUNDAMENTAL TERMS
16
Comparison of a measured value with the real value
Meas. ValueM
eas.
freq
uenc
yMean
Value interval
Accuracy = (%)True val. – Meas. val
True value
True value ?
MEASUREMENT ACCURACY
17
It is the difference between a measured or calculated value of a quantity and its exact value.
Systematic error plagues experiments or calculations caused by negative factors. For example, a DC voltage component, which unintentionally is present, e.g., because of a failure on the blockage capacitor, is a systematic error. These can be removed once understood/discovered via controls and calibration.
Random error is an error which is always present, but varies unpredictably in size and direction. They are related to the scatter in the data obtained under fixed conditions which determine the repeatability (precision) of the measurement and follow well-behaved statistical rules.
ERROR
18
Absolute error: It is an error that is expressed in physical units. It is the absolute value of the difference between the measured value and the true value (or the average value if the true value is not known) of a quantity.
Relative error: An error expressed as a fraction of the absolute error to the true (or average) value of a quantity. It is always given as a percentage.
ERROR
19
A number is represented with a finite (fixed) number of digits called word length.
Precision of a measurement or the accuracy of a computation bounds the number of significant digits. All non-zero digits beyond the number of significant digits at the right of a number are removed in one of two ways; truncation or round-off.
For example, 53.0534 has 6 significant digits. If it is going to be represented only by 4 significant digits both truncation and round-off processes yield 53.05. On the other hand, if the number of significant digits will be 3, then truncation and round-off processes yield, 53.0 and 53.1, respectively.
ERROR
20
A measurement/calculation result should be given as (a a), which means the value may be anything between (a - a) and (a + a).
For example, if the measured speed is 98 3 km/hr, then the real value may be anything between 95 and 101 km/hr.
Total error is the sum of individual errors for the arithmetic combination of two measurements.
Total relative error is the sum of individual relative errors for the mulitlicative combination of two measurements.
ERROR CALCULATIONS
21
A parameter (say “A”) is going to be estimated / calculated from two measurements (say “B” and “C”), with the measurement errors of “b” and “c”, respectively.
If A = B + C then total error will be
If A = B C then total error will be
cba
C
c
B
bAa
ERROR CALCULATIONS
22
In general, the total (propagated) error is obtained from these two properties as
,...),,( 321 xxxfy
B
bn
A
am
C
cBAC nm
...33
22
11
xx
fx
x
fx
x
fy
For a multi-variable function, the total error is calculated from
ERROR CALCULATIONS
23
Uncertainty is a range that is likely to contain the true value of a quantity being measured or calculated. Uncertainty can be expressed in absolute or relative terms.
Modeling uncertainty is defined as the potential deficiency due to a lack of information.
Modeling error is the recognizable deficiency not due to a lack of information but due to the approximations and simplifications made there.
Measurement error is the difference between the measured and true values, while measurement uncertainty is an estimate of the error in a measurement.
UNCERTAINTY
24
Modeling and simulation uncertainties occur during the phase of
- Conceptual modeling of the physical system- Mathematical modeling of the conceptual model- Discretization - Computer modeling of the mathematical model- Computer modeling of the discrete model- Numerical solution
Numerical uncertainties occur during computations due to round-off, turncation, non-convergence, artificial dissipations, etc.
UNCERTAINTY
25
Digital Voltmeter Accuracy:
(0.25 % Reading + 2 digit)
Meaning; multiply the value you read on the multimeter by 0.25 % (i.e., by 0.0025) . This is called scaling error.
Add 2 times the value of the least significant digit to the result.This is called quantization error.
Example: You read 15.00 V from a 20 V scale.• Measurement error : 0.0025 15.00
= 0.0375 V• Quantization error : 2 0.01 = 0.02
V• Total error : 0.0375 +
0.02 = 0.0575 V
ERROR / UNCERTAINTY
26
Truncation error is also defined for mathematical modeling. For example, Taylor’s expansion or a Fourier-series representation are used to replace a function in terms of an infinite summation.
Taking only a given number of low-order terms, and neglecting the rest of the higher-order terms, introduces a truncation error.
For example, the Taylor expansion of the exponent function:
yields (for n=3)
2 3 4
1 .... ...1! 2! 3! 4! !
nx x x x x x
en
4 !
n
n
xAbsolute error
n
MODELING ERROR
27
Derivative
Mathematical definition
x
xfxxfLimxf
x x
)()()(
0
Finite difference approximation
x
xfxxfxf
x
)()(
)(
x
xxfxxfxf
x
2
)()()(
+ O(∆x)
+ O(∆x2)
2Pt
x
f(x)
FDTD MODELING ERROR
3Pt
28
SOLVING NONLINEAR EQUATIONS
Fixed Point Iteration Method
Bisection Method
Secant Method
Newton-Raphson Method
MatLab Built-in Functions
29
FIXED POINT ITERATION METHOD
The equation f(x)=0 is rewritten in the form :
The intersection point of the graphs of the functions y=x and y=g(x) is the
solution and is called the fixed point .
The numerical solution is obtained by an iterative process.
First a xi value near the fixed point is chosen and substituted into g(x).
The solution is substituted back into g(x). So the iteration formula is given by:
)x(gx
)x(gx ii 1
30
FIXED POINT ITERATION METHOD
x1x2=
g(x1)
x
g(x1)
g(x2)
x3=
g(x2)
g(x3)
x4=
g(x3)
g(x4)
x5=
g(x4)
g(x5)
x6=
g(x5)
x7=
g(x6)
g(x6)
g(x7)
g(x)
y=x
31
FIXED POINT ITERATION METHOD
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 60
1
2
3
4
5
6
1
6
1
6
1
i
ii x)x(gx
x)x(g
13
2
12
62
1
6
2
2
)()(g
)x()x(g
i xi
1 6.0000
2 0.8571
3 3.2308
4 1.4182
5 2.4812
6 1.7235
7 2.2030
8 1.8732
9 2.0882
10 1.9429
11 2.0388
g(x)=6/(x+1)
y=x
Convergence Test: Example:
)x/(x
)x(x
xx
16
61
062
32
FIXED POINT ITERATION METHOD
Choosing the appropriate iteration function g(x)
The fixed point iteration method converges if in the neighborhood of
the fixed point the derivative of g(x) has an absolute value smaller
than 1. (also called Lipschitz continious) 1 )x(g
052150 x.ex x.Example:
0 0.5 1 1.5 2 2.5 3-5
0
5
10
15
x
f(x)=xe0.5x+1.2x-5
The plot of the function shows that the
equation has a solution between 1 and 2.
The actual solution is at x=1.5050 .
33
FIXED POINT ITERATION METHODCase A Case B Case C
Divergent Convergent Divergent
21
550 .e
x)x(gx.
x.e
x.x)x(g
50
215
21
5 50
.
exx)x(g
x.
21
50150
.
)x.(e)x(g
x.
250
50
212
5
).e(
e)x(g
x.
x.
x.e
x..)x(g
50
6073
0609221
5011
50.
.
).(e)(g
.
5305421
112 .
.
)(e)(g
50790212
51
250
50.
).e(
e)(g
.
.
44260212
52
2.
).e(
e)(g
880216073
150
.e
..)(g
.
9197026073
2 .e
..)(g
i xi
1 1.00002 2.79273 -5.23674 4.48495 -31.02626 4.16677 -23.71958 4.16689 -23.7223
10 4.166811 -23.7223
i xi
1 1.00002 1.75523 1.38694 1.56225 1.47766 1.51827 1.49878 1.50809 1.503510 1.505711 1.5046
i xi
1 1.00002 2.30483 0.70574 2.91835 0.34826 3.85007 0.05548 4.79869 -0.068810 5.260611 -0.0946
34
BISECTION METHOD
STEP 1: Choose two points a and b such that a solution exists between them :
f(a) f(b) < 0
STEP 2: Find c=(a+b)/2
STEP 3: Determine whether the true solution is between a and c or between c and b.
Then select the subinterval that contains the true solution
If f(a) f(c) < 0 , solution is between a and c. anew=a, bnew=c
If f(b) f(c) < 0 , solution is between c and b. anew=c, bnew=b
STEP 4: Repeat steps 2 and 3 until (b-a) is less than a specified tolerance
35
BISECTION METHOD
5 6 7 8 9 10 11 12 13-20
0
20
40
60
80
a bc
f(a)
f(b)
5 6 7 8 9 10 11 12 13-20
0
20
40
60
80
a bc
f(b)
f(a)
5 6 7 8 9 10 11 12 13-20
0
20
40
60
80
ab
f(b)
f(a)
5 6 7 8 9 10 11 12 13-20
0
20
40
60
80
a bc
f(a)
f(b)
f(a) f(b) < 0
c=(a+b) /2
True Root
36
BISECTION METHOD
a b c f(c) abs(b-a)
5.0000 12.0000 8.5000 18.7500 7.0000
5.0000 8.5000 6.7500 -2.6875 3.5000
6.7500 8.5000 7.6250 7.2656 1.7500
6.7500 7.6250 7.1875 2.0977 0.8750
6.7500 7.1875 6.9688 -0.3428 0.4375
6.9688 7.1875 7.0781 0.8655 0.2188
6.9688 7.0781 7.0234 0.2584 0.1094
6.9688 7.0234 6.9961 -0.0430 0.0547
6.9961 7.0234 7.0098 0.1075 0.0273
6.9961 7.0098 7.0029 0.0322 0.0137
6.9961 7.0029 6.9995 -0.0054 0.0068
6.9995 7.0029 7.0012 0.0134 0.0034
6.9995 7.0012 7.0004 0.0040 0.0017
2832 xx)x(f
37
NEWTON RAPSON METHOD
)x(f
)x(fxx
i
iii 1
x1
f(x1)
f(x2)
f(x)
x2x3x4x5
x
Slope: f’(x1)
Slope: f’(x2)Slope:
f’(x3)Slope: f’(x4)
Exact Root
38
NEWTON RAPSON METHOD
)x(f
)x(fxx
i
iii 1
xi f(xi) xi -xi-1
6.00000000 31.00000000
4.60238918 11.14583002 1.3976
3.27847524 3.85164919 1.3239
2.13314198 1.19335902 1.1453
1.34820293 0.27297398 0.7849
1.03883431 0.02728345 0.3094
1.00051801 0.00035912 0.0383
1.00000009 0.00000006 0.0005
1250 x.)x(f
39
SECANT METHOD
)x(f)x(f)x(f
xxxx i
ii
iiii
1
11
x1
f(x1)
f(x2)
f(x)
x2x3x4x5
x
Exact Root
x6
f(x3)
f(x4)f(x5)
40
SECANT METHOD
xi f(xi) xi -xi-1
6.00000000 31.00000000
5.00000000 15.00000000 1.0000
4.06250000 7.35419026 0.9375
3.16075727 3.47149501 0.9017
2.35451439 1.55711029 0.8062
1.69873759 0.62308391 0.6558
1.26127246 0.19853535 0.4375
1.05669682 0.04008167 0.2046
1.00494836 0.00343583 0.0517
1.00009654 0.00006692 0.0049
1.00000017 0.00000011 0.0001
1250 x.)x(f
)x(f)x(f)x(f
xxxx i
ii
iiii
1
11
41
MATLAB BUILT-IN FUNCTIONS
The roots(p) command finds the roots of a polynomial, whose coefficients
are defined in the row vector p. The following MatLab script find the roots of
the polynom P(x)=x3-3x2-13x+15
>> coef=[1 -3 -13 15];
>> xi=roots(coef)xi =
5.0000-3.00001.0000
>> x=fzero('x^3-3*x^2-13*x+15',7)x = 5
>> x=fzero('x^3-3*x^2-13*x+15',3)x = 1
The fzero(‘function’,x0) command finds the roots of the ‘function’ near x0.
The following MatLab script find the roots of the function f(x)=x3-3x2-13x+15
42
SYSTEMS OF LINEAR EQUATIONS
Gauss Elimination Method
Gauss-Jordan Elimination Method
LU Decomposition Method
43
GAUSS ELIMINATION METHOD
1525
22524
42
321
321
321
xxx
xxx
xxx
15125
225-24
4112
15125
225-24
250501 ..
15125
307-40
250501 ..
2553540
30740
250501
..
..
2553540
7.51.7510
250501
..
..
8.754.37500
7.51.7510
250501 ..
2100
7.51.7510
250501 ..
2
4
1
3
2
1
x
x
x
12250450
4572751
57751
2
11
22
32
3
x)(..x
x.)(.x
.x.x
x
Backward Substitution :
44
LU DECOMPOSITION METHOD
bLy
yxU
bxLU
LUA
bxA
• A is the coefficients matrix.
• U is an upper triangular matrix and is obtained at the end of the Gauss
elimination procedure.
• L is a lower triangular matrix which has all 1’s on the diagonal and the
elements below the diagonal are the multipliers mij that multiply the pivot
equation when it is used to eliminate the elements below the pivot
coefficient at the Gauss elimination procedure.
• L is a lower triangular matrix and y is obtained from Ly=b using forward
substitution.
• U is an upper triangular matrix and x is obtained from Ux=y using
backward substitution.
45
LU DECOMPOSITION METHOD
1525
22524
42
321
321
321
xxx
xxx
xxx
A
125
5-24
112
125
7-40
112
53540
740
112
..
4.37500
740
112
U
2
4
1
3
2
1
x
x
x
m21 =2 m31 =2.5 m32 =1.125
11.1252.5
012
001
1mm
01m
001
3231
21L
byL
y
y
y
15
22
4
11.1252.5
012
001
3
2
1
75815301.125452
151.12552
302242
222
4
33
321
22
21
1
.yy)(.
yyy.
yy)(
yy
y
yxU
.x
x
x
758
30
4
4.37500
740
112
3
2
1
14242
42
430)2(-74
3074
27584.375
11
321
22
32
33
xx
xxx
xx
xx
x.x
LUA
Forward Substitution : Backward Substitution :
46
GAUSS-JORDAN ELIMINATION METHOD
1525
22524
42
321
321
321
xxx
xxx
xxx
15125
225-24
4112
15125
225-24
250501 ..
15125
307-40
250501 ..
2553540
30740
250501
..
..
2553540
7.51.7510
250501
..
..
8.754.37500
7.51.7510
250501 ..
2100
7.51.7510
250501 ..
2100
4010
250501 ..
2100
4010
10501 .
2100
4010
1001
2
4
1
3
2
1
x
x
x
47
GAUSS-JORDAN ELIMINATION METHOD
221-43-2-
39-57-43
31-462
163-21-1
221-43-2-
39-57-43
29-5080
163-21-1
221-43-2-
87-1413-70
29-5080
163-21-1
547-85-0
87-1413-70
29-5080
163-21-1
547-85-0
87-1413-70
3.625-0.625010
163-21-1
547-85-0
61.625-9.62513-00
3.625-0.625010
163-21-1
35.8753.875-800
61.625-9.62513-00
3.625-0.625010
163-21-1
35.8753.875-800
4.74040.74038-100
3.625-0.625010
163-21-1
2.0481-2.0481000
4.74040.74038-100
3.625-0.625010
163-21-1
1-1000
4.74040.74038-100
3.625-0.625010
163-21-1
1-1000
40100
3.625-0.625010
163-21-1
1-1000
40100
3-0010
163-21-1
1-1000
40100
3-0010
13021-1
1-1000
40100
3-0010
5001-1
1-1000
40100
3-0010
20001
22432
395743
3462
1632
4321
4321
4321
4321
xxxx
xxxx
xxxx
xxxx
-1
4
-3
2
4
3
2
1
x
x
x
x
48
GAUSS-JORDAN ELIMINATION METHODMATLAB SCRIPTA=[2 -1 1 -4; 4 2 -5 22; 5 2 -1 15];[n,col]=size(A);%-------------------------------------------------------------------for i=1:n A(i,:)=A(i,:)/A(i,i); disp('A ='); disp(A); pause; for k=i+1:n dd=A(k,i); A(k,:)=A(k,:)-dd*A(i,:); A pause; endend%-------------------------------------------------------------------for i=n:-1:2 for k=i-1:-1:1 dd=A(k,i); A(k,:)=A(k,:)-dd*A(i,:); A pause; end endx=A(:,col);disp(' The unknowns are :'); disp(x);rref(A) % MatLab built-in function
49
TAYLOR SERIES
40
04
30
020
0000 !4!3!2
)xx()x(f
)xx()x(f
)xx()x(f
)xx)(x(f)x(f)x(f)(
44
32
!4
0
!3
0
!2
000 x
)(fx
)(fx
)(fx)(f)(f)x(f
)(
Taylor Series :
Maclaurin Series :
Example :
)xcos()x(f
)xsin()x(f
)xcos()x(f
)xsin()x(f
)xcos()x(f
)xsin()x(f
)xcos()x(f
)xsin()x(f
)xcos()x(f
)(
)(
)(
)(
)(
8
7
6
5
4
1
00
10
00
10
00
10
00
10
8
7
6
5
4
)x(f
)(f
)(f
)(f
)(f
)(f
)(f
)(f
)(f
)(
)(
)(
)(
)(
12108642
!12
1
!10
1
!8
1
!6
1
!4
1
2
11 xxxxxx)xcos(
50
TAYLOR SERIES
-8 -6 -4 -2 0 2 4 6 8-2
-1.5
-1
-0.5
0
0.5
1
1.5
2y=cos(x)2 Terms3 Terms4 Terms5 Terms6 Terms7 Terms8 Terms
51
NUMERICAL DIFFERENTIATION
Two-Point Forward Difference Method
Two-Point Backward Difference Method
Two-Point Central Difference Method
Three-Point Forward Difference Method
Three-Point Backward Difference Method
52
NUMERICAL DIFFERENTIATION
xixi-1
f(xi)
h
f(xi-1)
True derivative
Approximated derivative
f(x)
xi xi+1
f(xi)
h
f(xi+1)
True derivative
Approximated derivative
f(x)
True derivative
xi+1xi-1
f(xi+1)
2h
f(xi-1)
Approximated derivative
f(x)
Forward Difference Backward Difference
Central Difference
ii
ii
xx xx
)x(f)x(f
dx
df
i
1
1
1
1
ii
ii
xx xx
)x(f)x(f
dx
df
i
11
11
ii
ii
xx xx
)x(f)x(f
dx
df
i
Forward Difference:
Backward Difference:
Central Difference:
53
NUMERICAL DIFFERENTIATION
h
)x(f)x(f
dx
df ii
xx i
1
h
)x(f)x(f
dx
df ii
xx i2
11
2 Point Forward Difference:
2 Point Backward Difference:
2 Point Central Difference:
h
)x(f)x(f
dx
df ii
xx i
1
3 Point Forward Difference:
3 Point Backward Difference:
h
)x(f)x(f)x(f
dx
df iii
xx i2
43 21
h
)x(f)x(f)x(f
dx
df iii
xx i2
34 12
hxxxx iiii 11
)h(O
)h(O
)h(O 2
)h(O 2
)h(O 2
Truncation Error:
54
NUMERICAL DIFFERENTIATION
Example : For f(x)=ln(x) estimate the value of f’(0.2) taking h=0.01.
xi-2 0.18
xi-1 0.19
xi 0.2
xi+1 0.21
xi+2 0.22
f(xi-2) ln(0.18)
f(xi-1) ln(0.19)
f(xi) ln(0.2)
f(xi+1) ln(0.21)
f(xi+2) ln(0.22)
12935010
1902020 1 .
.
).ln().ln(
h
)x(f)x(f).(f ii
00425020
190210
220 11 .
.
).ln().ln(
h
)x(f)x(f).(f ii
8794010
2021020 1 .
.
).ln().ln(
h
)x(f)x(f).(f ii
99254020
2202104203
2
4320 21 .
.
).ln().ln().ln(
h
)x(f)x(f)x(f).(f iii
99064020
2031904180
2
3420 12 .
.
).ln().ln().ln(
h
)x(f)x(f)x(f).(f iii
520
1
).(f
x)x(f
Method Used f’(0.2)Abs.
Error
2 Point Forward Difference 4.879 0.121
2 Point Backward Difference
5.1293 0.1293
2 Point Central Difference 5.0042 0.0042
3 Point Forward Difference 4.9925 0.0075
3 Point Backward Difference
4.9906 0.0094
55
NUMERICAL DIFFERENTIATIONh f’(0.8) Abs. Error
0.7500 0.8819 0.36810.5000 0.9710 0.27900.4000 1.0137 0.23630.3000 1.0615 0.18850.2000 1.1157 0.13430.1000 1.1778 0.07220.0500 1.2125 0.03750.0100 1.2423 0.00770.0050 1.2461 0.00390.0010 1.2492 0.00080.0005 1.2496 0.00040.0001 1.2499 0.0001
h f’(0.8) Abs. Error0.7500 3.6968 2.44680.5000 1.9617 0.71170.4000 1.7329 0.48290.3000 1.5667 0.31670.2000 1.4384 0.18840.1000 1.3353 0.08530.0500 1.2908 0.04080.0100 1.2579 0.00790.0050 1.2539 0.00390.0010 1.2508 0.00080.0005 1.2504 0.00040.0001 1.2501 0.0001
h f’(0.8) Abs. Error0.7500 2.2893 1.03930.5000 1.4663 0.21630.4000 1.3733 0.12330.3000 1.3141 0.06410.2000 1.2771 0.02710.1000 1.2566 0.00660.0500 1.2516 0.00160.0100 1.2501 6.51e-0050.0050 1.2500 1.63e-0050.0010 1.2500 6.51e-0070.0005 1.2500 1.63e-0070.0001 1.2500 6.51e-009
25180
1
.).(f
x/)x(f
)xln()x(f
h f’(0.8) Abs. Error0.7500 1.0597 0.19030.5000 1.1311 0.11890.4000 1.1609 0.08910.3000 1.1903 0.05970.2000 1.2178 0.03220.1000 1.2399 0.01010.0500 1.2472 0.00280.0100 1.2499 0.00010.0050 1.2499679 3.21e-0050.0010 1.2499987 1.298e-0060.0005 1.24999967 3.25e-0070.0001 1.249999987 1.30e-008
2 Point Forward
Difference:
2 Point Backward Difference:
2 Point Central
Difference:
3 Point Forward Difference:
56
NUMERICAL DIFFERENTIATION
1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.71
1.2
1.4
1.6
1.8
2
2.2
2.4
2.6
2.8
3
151
42
242
).(fSlope
x)x(f
xx)x(f
57
NUMERICAL DIFFERENTIATION
h f’(1.5) Abs. Error
1.0000 0 1.0000
0.8000 0.2000 0.8000
0.5000 0.5000 0.5000
0.2000 0.8000 0.2000
0.1000 0.9000 0.1000
0.0500 0.9500 0.0500
0.0100 0.9900 0.0100
0.0050 0.9950 0.0050
0.0010 0.9990 0.0010
151
42
242
).(f
x)x(f
xx)x(f
58
NUMERICAL INTEGRATION
Rectangle Methods
Midpoint Method
Trapezoidal Method
Simpsons MethodsSimpsons 1/3 Method
Simpsons 3/8 Method
59
NUMERICAL INTEGRATION
a
f(a)
b
f(b)f(x)
a
f(a)
b
f(b)f(x)
a
f(a)
b
f(b)f(x)
a
f(a)
b
f(b)f(x)
1. Rectangle Method 2. Rectangle Method
Midpoint Method Trapezoidal Method
)a(f)ab(dx)x(fb
a
b
a)
ba(f)ab(dx)x(f
2
b
a)b(f)ab(dx)x(f
b
a
)b(f)a(f)ab(dx)x(f
2
60
NUMERICAL INTEGRATION
N
ii
b
a
N
ii )x(fhIdx)x(f
11
x1
ax2 xi xi+1 xN-1 xN xN+1
bx3
f(x1)
I1 I2 I3 Ii Ii+1 IN-1 IN
h h h h h h h
f(x2)
f(x)1. Rectangle Method
N
abh
61
NUMERICAL INTEGRATION
x1
ax2 xi xi+1 xN-1 xN xN+1
bx3
f(x1)
I1 I2 I3 Ii Ii+1 IN-1 IN
h h h h h h h
f(x2)
f(x)
1
21
N
ii
b
a
N
ii )x(fhIdx)x(f
2. Rectangle Method
62
NUMERICAL INTEGRATION
x1
ax2 xi xi+1 xN-1 xN xN+1
bx3
I1 I2 I3 Ii Ii+1 IN-1 IN
h h h h h h h
f(x)
N
i
iib
a
N
ii
xxfhIdxxf
1
1
1 2)(
221 xx
f
232 xx
f
(x1+x2)2
(x2+x3)2
Midpoint Method
63
NUMERICAL INTEGRATION
x1
ax2 xi xi+1 xN-1 xN xN+1
bx3
f(x1)
I1 I2 I3 Ii Ii+1 IN-1 IN
h h h h h h h
f(x2)
f(x)
N
iii
b
a
N
ii )x(f)x(f
hIdx)x(f
11
12
Trapezoidal Method
64
Simpson 1/3 MethodA second order polynomial g(x) is used to approximate the integrand f(x)
NUMERICAL INTEGRATION
x1
ax2 x3
b
f(x1)
h/2=k
f(x)
h/2=k
)()
2(4)(
3)( bf
bafaf
kdxxf
b
a
g(x)
65
NUMERICAL INTEGRATION
x1
ax2 x4
b
f(x1)
h/3=k
x3
h/3=k h/3=k
Simpson 3/8 MethodA third order polynomial g(x) is used to approximate the integrand f(x)
f(x)
g(x)
)()(3)(3)(
8
3)( 32 bfxfxfaf
kdxxf
b
a
66
NUMERICAL INTEGRATION
0 1 2 3 4 5 6 7 8 9 10 11 120
50
100
150
200
0 1 2 3 4 5 6 7 8 9 10 11 120
50
100
150
200
0 1 2 3 4 5 6 7 8 9 10 11 120
50
100
150
200
0 1 2 3 4 5 6 7 8 9 10 11 120
50
100
150
200
96x18x-x 23 )x(f
52.h 2h
1h 50.h
67
NUMERICAL INTEGRATION
1440dx96x18x-x11
123
1. Rectangular Method
h Num Int.
Abs. Error
Rel. Error %
5 1115 325 22.5692.5 1277.5 162.5 11.2852 1310 130 9.02781 1375 65 4.51390.5 1407.5 32.5 2.25690.1 1433.5 6.5 0.451390.05 1436.8 3.25 0.225690.01 1439.3 0.65 0.0451390.005 1439.7 0.325 0.0225690.001 1439.9 0.065 0.00451390.0005 1440 0.0325 0.00225690.0001 1440 0.0065 0.00045139
2. Rectangular Method
h Num Int.
Abs. Error Rel. Error %
5 1765 325 22.5692.5 1602.5 162.5 11.2852 1570 130 9.02781 1505 65 4.51390.5 1472.5 32.5 2.25690.1 1446.5 6.5 0.451390.05 1443.3 3.25 0.225690.01 1440.6 0.65 0.0451390.005 1440.3 0.325 0.0225690.001 1440.1 0.065 0.00451390.0005 1440 0.0325 0.00225690.0001 1440 0.0065 0.00045139
68
NUMERICAL INTEGRATION
389.333
1168dx40-x7316x-x
10
223
1. Rect. Method 2. Rect. Method
h Num Int.
Abs. Error
Num Int.
Abs. Error
8 400 10.667 720 330.674 352 37.333 512 122.672 360 29.333 440 50.6671 372 17.333 412 22.6670.5 380 9.3333 400 10.6670.1 387.36 1.9733 391.36 2.02670.05 388.34 0.99333 390.34 1.00670.01 389.13 0.19973 389.53 0.200270.005 389.23 0.099933 389.43 0.100070.001 389.31 0.019997 389.35 0.0200030.0005 389.32 0.009999 389.34 0.0100010.0001 389.33 0.002 389.34 0.002
Midpoint Method
h Num Int.
Abs. Error
8 304 85.3334 368 21.3332 384 5.33331 388 1.33330.5 389 0.333330.1 389.32 0.0133330.05 389.33 0.00333330.01 389.33 0.000133330.005 389.33 3.3333e-0050.001 389.33 1.3333e-0060.0005 389.33 3.3333e-0070.0001 389.33 1.3332e-008
Trapezoidal Method
h Num Int.
Abs. Error
8 560 170.674 432 42.6672 400 10.6671 392 2.66670.5 390 0.666670.1 389.36 0.0266670.05 389.34 0.00666670.01 389.33 0.000266670.005 389.33 6.6667e-0050.001 389.33 2.6667e-0060.0005 389.33 6.6667e-0070.0001 389.33 2.6663e-008
69
NUMERICAL INTEGRATION
(h=0.1)Numerical
Integration
Absolute
Error
1. Rectangle Method 387.36 1.9733
2. Rectangle Method 391.36 2.0267
Midpoint Method 389.32 0.013333
Trapezoidal Method 389.36 0.026667
Simpsons 1/3 Method 389.33 5.6843e-014
Simpsons 3/8 Method 389.33 5.6843e-014
389.333
1168dx40-x7316x-x
10
223
70
CURVE FITTING WITH A LINEAR FUNCTION
Curve Fitting with a Linear Function
x1 x2 xi xN
y1
y2
yi
yN
r1 r2
ri
rN
(x1 ,y1)
(x2 ,y2)
(xi ,yi)
(xN ,yN)
f(x)=a1x+a0
A linear function f(x)=a1x+a0 is used to best fit the given data points (xi ,yi).
A residual ri at point (xi , yi) is the difference between the value yi of the data point
and the value of the function f(xi) used to approximate the data points : ri= yi -f(xi)
71
The overall error E is defined as the sum of the squares of the residuals :
N
iii
N
ii axayrE
1
201
1
2
Linear least-squares regression :The coefficients a1 and a0 of the linear function f(x)=a1x+a0 are determined such that the
error E has the smallest possible value. E is minimum if:
00
a
E0
1
a
E
The coefficients a1 and a0 are found as:
N
iix xS
1
N
iiy yS
1i
N
iixy yxS
1
N
iixx xS
1
2
21xxx
yxxy
SnS
SSnSa
20xxx
xxyyxx
SnS
SSSSa
CURVE FITTING WITH A LINEAR FUNCTION
72
x 1 2 3 4 5 6 7 8 9 10
y 6 9.2 13 14.7 19.7 21.8 22.8 29.1 30.2 32.2
Example : Use linear least-squares regression to determine the coefficients a1 and a0 in the function f(x)=a1x+a0 that best fits the data given below.
55109876543211
N
iix xS
198.732.2 30.2 29.1 22.8 21.8 19.7 14.7 13 9.2 61
N
iiy yS
1337.732.21030.2929.18 22.8721.8619.7514.741339.22611
i
N
iixy yxS
38510987654321 2222222222
1
2
N
iixx xS
2.9679
5538510
7198557133710221
..
SnS
SSnSa
xxx
yxxy
3.5467
5538510
55713377198385220
..
SnS
SSSSa
xxx
xxyyxx
CURVE FITTING WITH A LINEAR FUNCTION
73
0 1 2 3 4 5 6 7 8 9 10 110
5
10
15
20
25
30
35
40
f(x)=2.9679x+3.5467
yi f(xi) ri= yi -f(xi) (ri)2
6.0000 6.5145 -0.5145 0.2648
9.2000 9.4824 -0.2824 0.0798
13.0000 12.4503 0.5497 0.3022
14.7000 15.4182 -0.7182 0.5158
19.7000 18.3861 1.3139 1.7264
21.8000 21.3539 0.4461 0.1990
22.8000 24.3218 -1.5218 2.3159
29.1000 27.2897 1.8103 3.2772
30.2000 30.2576 -0.0576 0.0033
32.2000 33.2255 -1.0255 1.0516
E= 9.736
+
CURVE FITTING WITH A LINEAR FUNCTION
74
x 2 5 6 8 9 13 15
y 7 8 10 11 12 14 15
Example : Use linear least-squares regression to determine the coefficients a1 and a0 in the function f(x)=a1x+a0 that best fits the data given below.
581513 986521
N
iix xS
7715 14 12 11 10 8 71
N
iiy yS
17715151413 12911810685721
i
N
iixy yxS
6041513 98652 2222222
1
2
N
iixx xS
0.64
586047
77587177221
xxx
yxxy
SnS
SSnSa
5.6968
586047
5871777604220
xxx
xxyyxx
SnS
SSSSa
CURVE FITTING WITH A LINEAR FUNCTION
75
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 164
6
8
10
12
14
16
yi f(xi) ri= yi -f(xi) (ri)2
7.0000 6.9769 0.0231 0.00058.0000 8.8970 -0.8970 0.8046
10.0000 9.5370 0.4630 0.214311.0000 10.8171 0.1829 0.033412.0000 11.4572 0.5428 0.294714.0000 14.0174 -0.0174 0.000315.0000 15.2975 -0.2975 0.0885
E= 1.4363+
f(x)=0.64x+5.6968
CURVE FITTING WITH A LINEAR FUNCTION
76
x -8 -7 -5 -1 0 2 4 5 6
y m 15 12 5 2 0 n -5 -9
Example : Use linear least-squares regression to determine the coefficients a1 and a0 in the function f(x)=a1x+a0 that best fits the data given below. Use f(x) to determine the interpolated value n for x=4 and the extrapolated value m for x= -8.
06 5 2 0 1- 5- -71
N
iix xS
029- 5- 0 2 5 12 151
N
iiy yS
-249(-9)6 (-5)5 02 20 5(-1)12(-5)15(-7)1
i
N
iixy yxS
1406 5 2 0 (-1)(-5)(-7) 2222222
1
2
N
iixx xS
1.7786-
01407
200(-249)7221
xxx
yxxy
SnS
SSnSa
2.8571
01407
0(-249)20140220
xxx
xxyyxx
SnS
SSSSa
CURVE FITTING WITH A LINEAR FUNCTION
77
-10 -8 -6 -4 -2 0 2 4 6 8-10
-5
0
5
10
15
20
f(x)=1.7786x+ 2.8571
Interpolation : Using the data points for estimating the expexted values between the known points.
Extrapolation : Using the data points for estimating the expexted values beyond the known points.
17.08572.85718)(778618 .)(fm
4.25712.8571)4(778614 .)(fn
m
n
CURVE FITTING WITH A LINEAR FUNCTION
78
LAGRANGE POLYNOMIALS
)x(Ly)x(Ly)x(Ly)x(Ly)x(Ly)x(P nnkk
n
kkkn
1100
0
)xx).....(xx)(xx).....(xx)(xx(
)xx).....(xx)(xx).....(xx)(xx()x(L
nkkkkkkk
nkkk
1110
1110
ki
ki,)x(L ik 0
1
iin y)x(P
For n+1 data points (x0 , y0), (x1 , y1), (x2 , y2), ….. (xk , yk), ….. (xn , yn) a unique polynomial of
order n can be found, which passes through these n data points. A Lagrange interpolating
polynomial Pn(x) that passes through n points is defined as :
79
)x)(.x())(.(
)x)(.x(
)xx)(xx(
)xx)(xx()x(L 452
42522
452
2010
210
3
522
52424
522
1202
102
).x)(x(
).)((
).x)(x(
)xx)(xx(
)xx)(xx()x(L
1514250050
55412
186
3
61105650
3
522250
43
424045250
2
222
221100
2
02
.x.x.
)x.x()xx(.
)x.x(.
).x)(x(.
/
)x)(x(.)x)(.x(.
)x(Ly)x(Ly)x(Ly)x(Ly)x(Pk
kk
LAGRANGE POLYNOMIALS
x 2 2.5 4
y 0.5 0.4 0.25
Example : Find the Lagrange interpolation polynomial that passes through the points :
2504
4052
502
22
11
00
.y,x
.y,.x
.y,x
43
42
452252
42
2101
201 /
)x)(x(
).)(.(
)x)(x(
)xx)(xx(
)xx)(xx()x(L
MATLAB SCRIPT
x=[2 2.5 4]; y=[0.5 0.4 0.25];polyfit(x,y,2)
80
1514250050 22 .x.x.)x(P
LAGRANGE POLYNOMIALS
x 2 2.5 4
y 0.5 0.4 0.25The Lagrange interpolation polynomial that passes through the points :
0 1 2 3 4 5 60
0.2
0.4
0.6
0.8
1
1.2
81
)x)(x(.))((
)x)(x(
)xx)(xx(
)xx)(xx()x(L 2150
2010
21
2010
210
)x(x.))((
)x)(x(
)xx)(xx(
)xx)(xx()x(L 150
1202
10
1202
102
12
5353422350
15072221501
2
222
221100
2
02
xx
x.x.xx)xx(.
)x(x.)x(x)x)(x(.
)x(Ly)x(Ly)x(Ly)x(Ly)x(Pk
kk
LAGRANGE POLYNOMIALS
x 0 1 2
y -1 2 7
Example : Find the Lagrange interpolation polynomial that passes through the points :
72
21
10
22
11
00
y,x
y,x
y,x
)x(x))((
)x)(x(
)xx)(xx(
)xx)(xx()x(L 2
2101
20
2101
201
82
LAGRANGE POLYNOMIALS
The Lagrange interpolation polynomial that passes through the points : x 0 1 2
y -1 2 7
1222 xx)x(P
-2 -1 0 1 2 3 4-5
0
5
10
15
20
25
83
)x)(x)(x())()((
)x)(x)(x(
)xx)(xx)(xx(
)xx)(xx)(xx()x(L 431
12
1
403010
431
302010
3210
)x(L2
3311313
4434
115413
4
1
314
5431
4
1
232
2
332021
0100
3
03
xxx)x)(x)(x()x)(x(
)x)(x()x(x)x)(x()x(
)x)(x(x)x)(x)(x(
)x(Ly)x(Ly)x(Ly)x(Ly)x(Ly)x(Pk
kk
LAGRANGE POLYNOMIALS
x 0 1 3 4
y 3 0 0 15
Example : Find the Lagrange interpolation polynomial that passes through the points :
154
03
01
30
33
22
11
00
y,x
y,x
y,x
y,x
)x(L1
)x)(x(x))()((
)x)(x)(x(
)xx)(xx)(xx(
)xx)(xx)(xx()x(L 31
12
1
341404
310
231303
2103
84
LAGRANGE POLYNOMIALS
The Lagrange interpolation polynomial that passes through the points : x 0 1 3 4
y 3 0 0 15
33 233 xxx)x(P
-2 -1 0 1 2 3 4 5-15
-10
-5
0
5
10
15
20
25
30
85
)x)(x)(x())()((
)x)(x)(x(
)xx)(xx)(xx(
)xx)(xx)(xx()x(L 543
60
1
504030
543
302010
3210
)x(L2
825120307515
15
1
3241892720090101209424215
1
43275410543215
1
4310
1854
6
4543
60
8
2323
232323
332021100
3
03
xxxxxx
xxxxxxxxx
)x)(x(x)x)(x(x)x)(x)(x(
)x)(x(x)x)(x(x)x)(x)(x(
)x(Ly)x(Ly)x(Ly)x(Ly)x(Ly)x(Pk
kk
LAGRANGE POLYNOMIALS
x 0 3 4 5
y 8 -4 0 18
Example : Find the Lagrange interpolation polynomial that passes through the points :
18,5
0,4
4,3
8,0
33
22
11
00
yx
yx
yx
yx
)x)(x(x))()((
)x)(x)(x(
)xx)(xx)(xx(
)xx)(xx)(xx()x(L 43
10
1
453505
430
231303
2103
)x)(x(x))()((
)x)(x)(x(
)xx)(xx)(xx(
)xx)(xx)(xx()x(L 54
6
1
534303
540
312101
3201
86
LAGRANGE POLYNOMIALS
The Lagrange interpolation polynomial that passes through the points : x 0 3 4 5
y 8 -4 0 18
825 233 xxx)x(P
-1 0 1 2 3 4 5 6-5
0
5
10
15
20
25
30
35
87
ORDINARY DIFFERENTIAL EQUATIONS
A differential equation is an equation that contains derivatives of an unknown
function.
A differential equation that has one independent variable is called an
ordinary differential equation (ODE).
If an ODE involves only first derivatives of the dependent variable (y) with
respect to the independent variable (x), it is a first – order ordinary
differential equation.
A first –order ODE is linear, if it is a linear function of y and dy/dx.
)nonlinear(ybayxdx
dy
)linear(byaxdx
dy
0
02
88
EULER’S METHOD
xixi+1
yi
yi+1
h
Slope =f(xi, yi)
y(x)
Numerical Solution
Exact Solution
)y,x(fhyy
hxx
)y,x(fdx
dySlope
)y,x(fdx
dy
iiii
ii
iixx i
1
1
89
EULER’S METHOD Example : Use Euler’s method to solve the Ordinary Differential Equation below from x=0 to x=1
with the initial conditions x=0 and y=2. (Take h=0.2) Compute the errors in each step for the exact solution y=3ex-x-1
2010 )(yxyxdx
dy
)y,x(fhyy
hxx
yxyx)y,x(f
iiii
ii
1
1
00 20
422020220202
20200
0001
01
..),(f.)y,x(fhyy
..hxx
9224220204242202042
402020
1112
12
.....).,.(f..)y,x(fhyy
...hxx
584392240209229224020922
602040
2223
23
.....).,.(f..)y,x(fhyy
...hxx
42084584360205843584360205843
802060
3334
34
.....).,.(f..)y,x(fhyy
...hxx
46554208480204208442084802042084
12080
4445
45
.....).,.(f..)y,x(fhyy
..hxx
90
EULER’S METHOD
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 2.0000 2.0000 00.2500 2.5000 2.6021 0.10210.5000 3.1875 3.4462 0.25870.7500 4.1094 4.6010 0.49161.0000 5.3242 6.1548 0.8306
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 2.0000 2.0000 00.2000 2.4000 2.4642 0.06420.4000 2.9200 3.0755 0.15550.6000 3.5840 3.8664 0.28240.8000 4.4208 4.8766 0.45581.0000 5.4650 6.1548 0.6899
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 2.0000 2.0000 00.1000 2.2000 2.2155 0.01550.2000 2.4300 2.4642 0.03420.3000 2.6930 2.7496 0.05660.4000 2.9923 3.0755 0.08320.5000 3.3315 3.4462 0.11460.6000 3.7147 3.8664 0.15170.7000 4.1462 4.3413 0.19510.8000 4.6308 4.8766 0.24590.9000 5.1738 5.4788 0.30501.0000 5.7812 6.1548 0.3736
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 2.0000 2.0000 00.0500 2.1000 2.1038 0.00380.1000 2.2075 2.2155 0.00800.1500 2.3229 2.3355 0.01260.2000 2.4465 2.4642 0.01770.2500 2.5788 2.6021 0.02320.3000 2.7203 2.7496 0.02930.3500 2.8713 2.9072 0.03590.4000 3.0324 3.0755 0.04310.4500 3.2040 3.2549 0.05100.5000 3.3867 3.4462 0.05950.5500 3.5810 3.6498 0.06870.6000 3.7876 3.8664 0.07880.6500 4.0069 4.0966 0.08970.7000 4.2398 4.3413 0.10150.7500 4.4868 4.6010 0.11420.8000 4.7486 4.8766 0.12800.8500 5.0261 5.1689 0.14290.9000 5.3199 5.4788 0.15900.9500 5.6309 5.8071 0.17631.0000 5.9599 6.1548 0.1950
h=0.25
h=0.2
h=0.1 h=0.05
91
EULER’S METHOD
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 12
2.5
3
3.5
4
4.5
5
5.5
6
6.5
92
EULER’S METHOD Example : Use Euler’s method to solve the Ordinary Differential Equation below from x=0 to x=1.8 with the initial conditions x=0 and y=1. (Take h=0.6) Compute the errors in each step for the exact solution.
21081025023 x.
exact exy)(y.xxyxdx
dy
)y,x(fhyy
hxx
yxxyx)y,x(f
iiii
ii
1
1
003 10
100160110601
60600
30001
01
.),(f.)y,x(fhyy
..hxx
1.230460601601160601
216060
31112
12
...),.(f.)y,x(fhyy
...hxx
1.0795212123041602304123041216023041
816021
32223
23
.....).,.(f..)y,x(fhyy
...hxx
93
EULER’S METHOD
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 1.0000 1.0000 00.6000 1.0000 1.1628 0.16281.2000 1.2304 1.3856 0.15521.8000 1.0795 0.1869 0.8926
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 1.0000 1.0000 00.3000 1.0000 1.0440 0.04400.6000 1.0819 1.1628 0.08090.9000 1.2118 1.3107 0.09891.2000 1.3203 1.3856 0.06521.5000 1.2773 1.1698 0.10751.8000 0.8395 0.1869 0.6526
xi yi
(Euler)
yi
(Exact)
Abs.Error
0 1.0000 1.0000 00.1000 1.0000 1.0050 0.00500.2000 1.0099 1.0198 0.00990.3000 1.0293 1.0440 0.01470.4000 1.0575 1.0767 0.01920.5000 1.0934 1.1169 0.02350.6000 1.1355 1.1628 0.02720.7000 1.1821 1.2124 0.03030.8000 1.2305 1.2629 0.03230.9000 1.2778 1.3107 0.03291.0000 1.3199 1.3513 0.03141.1000 1.3518 1.3787 0.02691.2000 1.3675 1.3856 0.01811.3000 1.3587 1.3620 0.00331.4000 1.3157 1.2955 0.02011.5000 1.2255 1.1698 0.05571.6000 1.0718 0.9634 0.10841.7000 0.8337 0.6481 0.18551.8000 0.4841 0.1869 0.2972
h=0.6
h=0.3
h=0.1
94
EULER’S METHOD
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.80
0.2
0.4
0.6
0.8
1
1.2
1.4