Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 164

NOTES ON NUMERICAL ANALYSIS.

Prof. Alberto Angarita.


Department of Basic Sciences,
Technological Units of Santander.

P(x)=P1(x)⊕P2(x)⊕P3(x)⊕···⊕Pn(x)

University Texts
Department of Basic Sciences 2013
Content

Content.............................................................................................................................................
Introduction..................................................................................................................................
Errors and Floating Point Representation........................................................................................
Mistakes...................................................................................................................................
1.1.1 Numerical approximation and error theory...............................................................
Definition 1.3........................................................................................................................
Definition 1.6........................................................................................................................
1.1.2 Significant numbers............................................................................................
Definition 1.7........................................................................................................................
Example 1.1..........................................................................................................................
Rules for operations with significant figures........................................................................
1.1.3 Precision and accuracy......................................................................................
Definition 1.9........................................................................................................................
high Low................................................................................................................................
1.1.4 Types of Rounding..............................................................................................
Example 1.2..........................................................................................................................
1.2.1 Normalization of a Number................................................................................
Definition 1.12......................................................................................................................
Example 1.5..........................................................................................................................
1.2.2 IEEE 754 Standard.............................................................................................
Example 1.6......................................................................................................................
1, 011100101011101000011111000011111000100112 ×25............................................
The exponent, in excess of 2 n -1 - 1 , will be: 5 10 + (28 - 1
- 1) 10 = 5 10 + (2 7 - 1) 10 = 5 10 + (128 - 1) 10 = 132 10 ≡
10000100 2.......................................................................................................................
1, 0111001010111000000111.......................................................................................
01110010101110100001111.............................................................................................
0100 0010 0011 1001 0101 1101 0000 1111....................................................................

Department of Basic Sciences


101110, 010101110100001111100001111100010011 2
≈42395 D 0 F HEX...........................................................................................................
Example 1.7......................................................................................................................
1. Convert 3E40000016 to base 2:.................................................................................
2. Get the bits of the sign, the mantissa and the exponent:............................................
3. Convert the exponent to base 10:..............................................................................
011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 -
1)10 = 12410?12710 = -3................................................................................................
1,1 ×2-3..............................................................................................................................
1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0,
187510..............................................................................................................................
1,1×2-3=((20+2-1)×2-
3
)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510........................................................
3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10.......................................................
Example 1.8......................................................................................................................
0.5625×2= 1.125 I —, d 1 = 1...........................................................................................
0.125×2=0.25 I —• d 2 = 0...............................................................................................
0.25×2=0.5 I —, d 3 = 0....................................................................................................
0.5×2= 1............................................................................................................................
19,562510≡ 10011,10012.................................................................................................
10011,10012= 1,00111001 ×24.........................................................................................
410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 =
410 + 102310 = 102710 ≡ 100000000112......................................................................
00111001000000000000000000000000000000000000000000
00.......................................................................................................................................
Example 1.8 (Continued)..................................................................................................
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A
00000000000 HEX............................................................................................................
Example 1.9......................................................................................................................
Example 1.9 (Continued)..................................................................................................
-0, 10111 × 2-126.................................................................................................................
-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)..........................................................
= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)............................................................
≈ [-0.71875 × 1.1754943508222875079687365372222](10)..........................................
≈ -8.44886564653519146352938612849 × 10-39..............................................................
[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )...............................................................
[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )....................................................
Solution of Equations No................................................................................................................
Linear...............................................................................................................................................
Theorem 2.1..........................................................................................................................
Example 2.1......................................................................................................................
f (0) = -1 < 0......................................................................................................................
f (1)=1>0...........................................................................................................................
Methods for Solving Nonlinear Equations in One Variable....................................................
2.2.1 Bisection Method.......................................................................................................
Example 2.2......................................................................................................................
x 1 = 1 x2 = 2.................................................................................................................
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578................................................................................
f ( x3 ) = f ( 1.5 ) = -0.1823...............................................................................................
Example 2.2 (Continued)..................................................................................................
× 100=...............................................................................................................................
1.5-1..................................................................................................................................
1.5......................................................................................................................................
× 100 = 33.33%.................................................................................................................
x1 + x3 1 + 1.5..............................................................................................................
= 1.25................................................................................................................................
f ( x4 ) = f ( 1.25 ) = 0.0634...............................................................................................
× 100=...............................................................................................................................
1.5 - 1.25...........................................................................................................................
1.5......................................................................................................................................
× 100= 16.66%..................................................................................................................
x 4 + x 3 1.25 + 1.5........................................................................................................
= 1.375..............................................................................................................................
f ( x5 ) = f ( 1.375 ) = -0.0656...........................................................................................

Department of Basic Sciences


2.2.2 False Position Method...............................................................................................
f ( xb ) -f ( xa ).....................................................................................................................
x1 = 0................................................................................................................................
x2 = 1................................................................................................................................
f ( x1 ) = -1.........................................................................................................................
f ( x2 ) = 0.7854...............................................................................................................
x 3= x 1 -
f ( x 2 ) - f ( x 1 ) = 0...............................................................................................
f (0)(1-0) f (1)- f (0) = 0......................................................................................................
(-1)(1 - 0)...........................................................................................................................
0.7854 - (-1) 1.7854........................................................................................................
= 0.5601............................................................................................................................
f ( x3 ) = f ( 0.5601 ) = 0.0707...........................................................................................
× 100=...............................................................................................................................
0.5601 - 1..........................................................................................................................
0.5601................................................................................................................................
× 100 = 78.54%.................................................................................................................
(-1)(0.5601 - 0) 0.5601...................................................................................................
0.0707 - (-1) = 1.07070=0.5231..........................................................................................
f ( x4 ) = f ( 0.5231 ) = 0.0051...........................................................................................
Example 2.3 (Continued)......................................................................................................
Definition 2.3........................................................................................................................
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )..............................................................................................
2.2.4 Newton-Raphson method.......................................................................................
0
f ( x ) =.............................................................................................................................
f ( x i ) - 0 x i - x i + 1........................................................................................................
f 0 ( x i )..............................................................................................................................
g ( x ) = x -.........................................................................................................................
f ( x ) f0 ( x )........................................................................................................................
Example 2.5......................................................................................................................
e - x i + x i - 2......................................................................................................................
- e - x i + 1...........................................................................................................................
e - x 0 + x 0.........................................................................................................................
-2.......................................................................................................................................
- e - x 0 + 1..........................................................................................................................
1-.......................................................................................................................................
e -(1) +(1)-2.........................................................................................................................
e +( )
= 1.843482357250334........................................................................................
-(1)....................................................................................................................................
e - x 1 + x 1 - 2....................................................................................................................
- e - x 1 + 1.........................................................................................................................
1-.......................................................................................................................................
e -(1.843482357250334) + (1.843482357250334) - 2.....................................................................
= 1.841406066157926......................................................................................................
e - x 2 + x 2 - 2....................................................................................................................
- e - x 2 + 1.........................................................................................................................
1.841405660436976..........................................................................................................
e - x 3 + x 3 - 2....................................................................................................................
- e - x 3 + 1.........................................................................................................................
1.841405660436961..........................................................................................................
e - x 4 + x 4 - 2....................................................................................................................
- e - x 4 + 1..........................................................................................................................
1.841405660436961..........................................................................................................
2.2.5 Secant Method.......................................................................................................
f ( x2 ) -f ( x1 )...................................................................................................................
f 0 ( x i )..............................................................................................................................
f 0 ( x i )..............................................................................................................................
xi +1= xi -
f ( x i ) - f ( x i - 1 )................................................................................................
Example 2.6......................................................................................................................
Yo
f ( x i )( x i - x i - 1 ) = x - f ( x i )- f ( x i - 1 ) ............................................................
x i
+ x i -2)( x i - x i - 1 ).....................................................................................................
( e - x i + x i -2) - ( e - x i......................................................................................................
1 + x i - 1 - 2)....................................................................................................................

Department of Basic Sciences


( e -(1) +(1)-2)(1-0).............................................................................................................
( e -(1) +(1)-2)-( e -(0) +(0)-2)...............................................................................................
= 2.718281828459045......................................................................................................
Interpolation and curve fitting.........................................................................................................
f ( x k ) = y k , k = 1,..., n...................................................................................................
3.1 NEWTON'S INTERPOLATING POLYNOMIAL..........................................................................
P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · +
b n ( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )...........................................................................
P n ( x ) = b 0 +∑ b i ∏( x - x j )....................................................................................
f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ] x i + j + 1 - x i
...........................................................................................................................................
P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]
( x - x 0 )( x - x 1 ) +·· · + f [ x 0 , x 1 ,..., x n ]( x - x 0 )( x - x 1
) · · · ( x - x n - 1 ).............................................................................................................
Example 3.1......................................................................................................................
Level b 1 :.........................................................................................................................
2-9 -7 (-1)-2 -3 (0 - (-1) 1.............................................................................................
=
5-(-3) 8 7-5 = 2 8-7 = 1..................................................................................................
3.2.................................................................................................................................................
Lagrange Interpolating Polynomial..........................................................................................
Example 3.2......................................................................................................................
P ( x ).................................................................................................................................
9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0 L 3 ( x )..........................................................
,
..........................................................................................................................................
Example 3.2 (Continued)..................................................................................................
L 0 ( x ) = ∏ x - xk............................................................................................................
-3-5 -3-7 -3-8 x -5 x -7 x -8...............................................................................................
=
-8 .-10.-11...................................................................................................................
-x3 x 2 131x7.................................................................................................................
=
880 + 44 - 880 + 22......................................................................................................
L1 ( x ) = ∏ x - xk.............................................................................................................
x - x 0 x - x 2 x - x 3.............................................................................................................
= ...................................................................................................................................
x - (-3) x -7 x - 8................................................................................................................
=
5-(-3).5-7.5-8...................................................................................................................
x +3 x -7 x -8.....................................................................................................................
=
8 . -2 . -3.....................................................................................................................
x3 x 2 11x7...................................................................................................................
=
48 + 4 - 48 + 2...................................................................................................................
L2 ( x ) = ∏ x - xk.............................................................................................................
x - (-3) x - 5 x - 8...............................................................................................................
=
7-(-3).7-5.7-8...................................................................................................................
x +3 x -5 x -8.....................................................................................................................
=
10 . 2 . -1.....................................................................................................................
-x3 x 2 x 6......................................................................................................................
=
20 + 2 - 20 -.................................................................................................................
Example 3.2 (Continued)..................................................................................................
Segmental Interpolation: Cubic Tracers...................................................................................
x 0 < x 1 < . . . < xn..............................................................................................................
( k )....................................................................................................................................
p3(x)≡pk(x)=ak+bk(x−xk)+ck(x−xk)2+dk(
x − x k ) 3...........................................................................................................................
Example 3.3......................................................................................................................
Example 3.3 (Continued)..................................................................................................
2k1 + 0.6k2 = -8.35.........................................................................................................
0.6k1 + 1.6k2 + 0.2k3 = 1.9...........................................................................................
0.2k2 + 1.2k3 + 0.4k4 = 4.8...........................................................................................
0.4k3 + 1.66k4 = 1.84.......................................................................................................
k 1 = -4, 9588....................................................................................................................
k 2 = 2, 6128.....................................................................................................................
k 3 = 3, 4740.....................................................................................................................
k4 = 0.2713.......................................................................................................................
Example 3.3 (Continued)..................................................................................................

Department of Basic Sciences


Curve Fitting by Least Squares................................................................................................
3.4.1 Least Squares and Linear Regression Analysis...............................................................
Criteria:.............................................................................................................................
Example 3.4......................................................................................................................
A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = 16 ,..................................................
b =.....................................................................................................................................
5(260) - (16)(47)...............................................................................................................
5(106) - (16)2.....................................................................................................................
(106)(47) - (206)(16).........................................................................................................
5(106) - (16)2.....................................................................................................................
a =2...................................................................................................................................
b =3...................................................................................................................................
Example 3.5......................................................................................................................
A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D
=∑ and i = 43.5 60
b =.....................................................................................................................................
8(546.63) - 89.8(43.5).......................................................................................................
8(1292.92) - (89.8)2 =⇒
...................................................................................................
(1292.92)(43.5) - 596.63(89.8).........................................................................................
8(1292.92) - (89.8)2...........................................................................................................
a = 0.20477.......................................................................................................................
b = 3.1389.........................................................................................................................
3.4.2 Linearization of Nonlinear Relationships.......................................................................
3.4.2.1 Model Power......................................................................................................
y = α x β.............................................................................................................................
| {z} |{z} | {z}.............................................................................................................
Example 3.6 (Continued)..................................................................................................
A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C =∑ x i =....................................................
a =.....................................................................................................................................
b =.....................................................................................................................................
5(1.9209) - (-2.0756)(-1.7657)..........................................................................................
5(1.4556) - (-2.0756)2 =
..................................................................................................
(1.4556)(-1.7657) - (1.9209)(-2.0756)..............................................................................
5(1.4556) - (-2.0756)2.......................................................................................................
a = 1.999947.....................................................................................................................
b = 0.477078.....................................................................................................................
Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997....................................................
β = 1.999947.....................................................................................................................
y = 2.9963 x 1.999947.............................................................................................................
Numerical Differentiation and Integration......................................................................................
Numerical Differentiation........................................................................................................
4.1.1 Accuracy Formulas for the First Derivative....................................................................
Three Point Formulas...............................................................................................................
. Forward:..........................................................................................................................
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 ).....................................................
. Backward:.......................................................................................................................
f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 ) 2 hours..............................................
Five Point Formulas.................................................................................................................
. Forward:..........................................................................................................................
-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f
( x i +4 h )..........................................................................................................................
. Central:............................................................................................................................
f 0 ( x i ) =..........................................................................................................................
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2 h ).....................................................
+ O ( h4 )...........................................................................................................................
Example 4.1......................................................................................................................
. Three Point Formula: If h = 0.1 we have to..................................................................
-3 f (2)+4 f (2+0.1)- f (2+2(0.1)).......................................................................................
2 ∗ (0.1)............................................................................................................................
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404......................................................
0.2......................................................................................................................................
-0.417756085....................................................................................................................

Department of Basic Sciences


. Five Point Formula: If h = 0.1 we have to....................................................................
-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f
(2+4(0.1))..........................................................................................................................
12 ∗ (0.1)..........................................................................................................................
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 +
16 × 0.745705212 - 3 × 0.675463180...............................................................................
1.2......................................................................................................................................
-0.416135615....................................................................................................................
Clearly f 0 (2) = cos(2)...................................................................................................
-0.416146837 - (-0.416135615)........................................................................................
-0.416146837....................................................................................................................
-0.416146837 - (-0.417756085)........................................................................................
-0.416146837....................................................................................................................
× 100 ≈ 0.38% and...........................................................................................................
4.2.................................................................................................................................................
Numerical Integration..............................................................................................................
4.2.1 Newton-Cotes formulas.............................................................................................
simple trapezoid rule.........................................................................................................
P ( x ) = f ( a ) + f ( b ) - f ( a ) ( x - a ) b - a..........................................................................
Zbf (
x ) to...........................................................................................................................
f ( b ) -f ( a ) b - a.............................................................................................................
/(b)-/(a) (xa)21 b...............................................................................................................
b - a 2................................................................................................................................
( b - a ) f ( a )+ f ( b )............................................................................................................
Example 4.2......................................................................................................................
2.........................................................................................................................................
π
2 + 6π...............................................................................................................................
4.........................................................................................................................................
π........................................................................................................................................
π
= 0.5235988....................................................................................................................
6.........................................................................................................................................
5π-6 3................................................................................................................................
12.......................................................................................................................................
0.4429715.........................................................................................................................
0.4429715 - 0.5235988.....................................................................................................
0.4429715..........................................................................................................................
Composite Trapezoid Rule...............................................................................................
f ( x ) dx + f ( x ) dx +. . . + f ( x ) dx..............................................................................
x0 x 1 x n -1................................................................................................................
I = h f ( x 0 )+2 f ( x 1 ) + h f ( x 1 )+2 f ( x 2 ) +...+ h.............................................................
f ( xn − 1 ) + f (xn )...........................................................................................................
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n ).....................................................................................
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )............................................................................................
i =1
I=(b-a) .............................................................................................................
2 n

Et =...................................................................................................................................
( b - a ) 3 12 n 3..................................................................................................................
∑ f 00 (ξ i )..........................................................................................................................
∑ f 00 (ξ i )..........................................................................................................................
f 00 ' i =1...............................................................................................................................
Example 4.3......................................................................................................................
2.........................................................................................................................................
b - a 1................................................................................................................................
2
= 0.1................................................................................................................................
x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1]...........................................................................................
0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]............................................
0.4513161..........................................................................................................................
E r =..................................................................................................................................
0.4429715 - 0.4513161.....................................................................................................
0.4429715..........................................................................................................................
· 100 ≈ 1.884%..................................................................................................................
Differential equations......................................................................................................................
Ordinary...........................................................................................................................................

Department of Basic Sciences


5.1 EULER METHOD..............................................................................................................
dy = f ( x , y ), y (0) = x 0..................................................................................................
f ( xn , yn ) =......................................................................................................................
( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )..............................................................................
Example 5.1......................................................................................................................
1. Analytical Solution....................................................................................................
Z dy
= Z 2 xdx.........................................................................................................................
Ln | and | = x 2 + C.............................................................................................................
Example 5.1 (Continued)..................................................................................................
Ln |1| =0+ C =⇒ C =0.......................................................................................................
Ln | and | = x 2 =⇒ y = e x 2................................................................................................
2. Numerical Solution....................................................................................................
x 1 = x 0 + h =0+0.1 =0.1.................................................................................................
y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which
means ( x 1 , y 1 ) = (0.1, 1)............................................................................................
x 2 = x 1 + h = 0.1 +0.1 =0.2............................................................................................
y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2
)=(0.2,1.02).......................................................................................................................
0.3 1.0608.....................................................................................................................
0.4 1.124448.................................................................................................................
0.5 1.21440384.............................................................................................................
0.6 1.33584422.............................................................................................................
0.7 1.49614553.............................................................................................................
0.8 1.70560591.............................................................................................................
0.9 1.97850285.............................................................................................................
1 2.33463336................................................................................................................
3. Approximate y (0.5)...................................................................................................
Example 5.1 (Continued)..................................................................................................
1.28403 - 1.21440.............................................................................................................
1.28403..............................................................................................................................
4. Graph and comparison of results...............................................................................
Fourth Order Runge-Kutta Method..........................................................................................
d y
dx = f ( x , y ), y (0) = x 0................................................................................................
k2=f(xn+h2,yn+h2k1)k3=f(xn+h2,yn+h2k2)k
4 = f ( x n + h , y n + hk 3 )...............................................................................................
6.........................................................................................................................................
Example 5.2......................................................................................................................
1. Analytical Solution....................................................................................................
Example 5.2 (Continued)..................................................................................................
3. Approximate y(0.5)...................................................................................................
1.28403 - 1.28402.............................................................................................................
1.28403..............................................................................................................................
· 100 ≈ 0.000779%............................................................................................................
4. Graph and comparison of results..................................................................................
Introduction to Matlab.....................................................................................................................
Matlab Environment Overview................................................................................................
. Save, Recall and Exit a work session.................................................................................
. Current Work Directory.....................................................................................................
. Delete Work Session Variables..........................................................................................
. Clean screen.......................................................................................................................
. Aids....................................................................................................................................
. Variables in use..................................................................................................................
Basic Constants and Operators.................................................................................................
a) Constants..............................................................................................................................
b) Operations Between Scalars................................................................................................
f) Useful Functions...................................................................................................................
A.3................................................................................................................................................
Elemental Functions.................................................................................................................
A.4................................................................................................................................................
Matrices and Vectors................................................................................................................
a) Matrices................................................................................................................................
1. Create a Matrix..............................................................................................................

Department of Basic Sciences


Example A.1.........................................................................................................................
42 44 70.............................................................................................................................
50 51..................................................................................................................................
2. Modify Elements of an Array........................................................................................
Example A.2.........................................................................................................................
3. Add or Delete Rows or Columns from a Matrix...........................................................
Example A.3.........................................................................................................................
Example A.4.........................................................................................................................
Example A.5.........................................................................................................................
Example A.6.........................................................................................................................
b) Vectors..............................................................................................................................
1. Vector Creation.............................................................................................................
Example A.7.........................................................................................................................
Example A.8.........................................................................................................................
2. Product of a Vector and a Scalar...................................................................................
Example A.9.........................................................................................................................
3. Product point.................................................................................................................
Example A.10.......................................................................................................................
4. Cross Product................................................................................................................
Example A.11.......................................................................................................................
c) Basic Operations Between Matrices and Between Vectors..............................................
Graph of Functions in 2D.........................................................................................................
a) In Rectangular or Cartesian Coordinates.............................................................................
Graph of a Simple Function.....................................................................................................
Example A.12.......................................................................................................................
Example A.12 (Continued)...................................................................................................
2. Second Option: Using the function fplot('Function',Limits,
[line specifiers]) ...................................................................................................................
3. Third Option: Using the function ezplot('Function',Limits,
[line specifiers]) ...................................................................................................................
Graph of Several Functions on the Same Plane.......................................................................
. First Option: Use The plot() Function ............................................................................
Example A.13.......................................................................................................................
Example A.14.......................................................................................................................
Graph of a Piecewise Function.................................................................................................
Example A.15.......................................................................................................................
b) In Polar Coordinates............................................................................................................
Example A.16.......................................................................................................................
A.6................................................................................................................................................
Symbolic Calculation...............................................................................................................
a) Algebraic Expressions..........................................................................................................
Useful Functions About Algebraic Expression........................................................................
Example A.17.......................................................................................................................
Evaluate a Symbolic Expression..............................................................................................
Example A.18.......................................................................................................................
b) Solution of Polynomial Equations and Systems of Linear
Equations..................................................................................................................................
Example A.19.......................................................................................................................
Example A.20.......................................................................................................................
c) Limit Calculation..............................................................................................................
Example A.21.......................................................................................................................
3. Lim.............................................................................................................................
-4.......................................................................................................................................
4. lim e - x.......................................................................................................................
1.........................................................................................................................................
d) Derivative Calculus...........................................................................................................
Example A.22.......................................................................................................................
d) Calculus of Integrals............................................................................................................
Example A.23.......................................................................................................................
A.7................................................................................................................................................
Programming in Matlab...........................................................................................................
The Function and Script Editor................................................................................................

Department of Basic Sciences


How do we access the editor?...............................................................................................
How do you run a script?......................................................................................................
How is a function executed?.................................................................................................
How do you program in Matlab?..........................................................................................
a) IF Control Statement.........................................................................................................
b) SWITCH Control Statement.............................................................................................
c) FOR Repetition Sentence..................................................................................................
d) WHILE Repetition Sentence.............................................................................................
e) Creation of Functions and Scripts.....................................................................................
. Features..................................................................................................................................
Example A.24.......................................................................................................................
Example A.25.......................................................................................................................
Example A.26.......................................................................................................................
Example A.27.......................................................................................................................
. Scripts.....................................................................................................................................
Example A.28.......................................................................................................................
Example A.29.......................................................................................................................
Bibliography....................................................................................................................................
3.1.1

Numerical analysis

Content.............................................................................................................................................
Introduction..................................................................................................................................
Errors and Floating Point Representation........................................................................................
Mistakes...................................................................................................................................
1.1.1 Numerical approximation and error theory...............................................................
Definition 1.3........................................................................................................................
Definition 1.6........................................................................................................................
1.1.2 Significant numbers............................................................................................
Definition 1.7........................................................................................................................
Example 1.1..........................................................................................................................
Rules for operations with significant figures........................................................................
1.1.3 Precision and accuracy......................................................................................
Definition 1.9........................................................................................................................
high Low................................................................................................................................
1.1.4 Types of Rounding..............................................................................................
Example 1.2..........................................................................................................................
1.2.1 Normalization of a Number................................................................................
Definition 1.12......................................................................................................................
Example 1.5..........................................................................................................................
1.2.2 IEEE 754 Standard.............................................................................................
Example 1.6......................................................................................................................
1, 011100101011101000011111000011111000100112 ×25............................................
The exponent, in excess of 2 n -1 - 1 , will be: 5 10 + (28 - 1
- 1) 10 = 5 10 + (2 7 - 1) 10 = 5 10 + (128 - 1) 10 = 132 10 ≡
10000100 2.......................................................................................................................
1, 0111001010111000000111.......................................................................................
01110010101110100001111.............................................................................................
0100 0010 0011 1001 0101 1101 0000 1111....................................................................
101110, 010101110100001111100001111100010011 2
≈42395 D 0 F HEX...........................................................................................................
Example 1.7......................................................................................................................
1. Convert 3E40000016 to base 2:.................................................................................
2. Get the bits of the sign, the mantissa and the exponent:............................................
3. Convert the exponent to base 10:..............................................................................
011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 -
1)10 = 12410?12710 = -3................................................................................................
1,1 ×2-3..............................................................................................................................
1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0,
187510..............................................................................................................................
1,1×2-3=((20+2-1)×2-
3
)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510........................................................
3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10.......................................................
Example 1.8......................................................................................................................

Department of Basic Sciences


0.5625×2= 1.125 I —, d 1 = 1...........................................................................................
0.125×2=0.25 I —• d 2 = 0...............................................................................................
0.25×2=0.5 I —, d 3 = 0....................................................................................................
0.5×2= 1............................................................................................................................
19,562510≡ 10011,10012.................................................................................................
10011,10012= 1,00111001 ×24.........................................................................................
410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 =
410 + 102310 = 102710 ≡ 100000000112......................................................................
00111001000000000000000000000000000000000000000000
00.......................................................................................................................................
Example 1.8 (Continued)..................................................................................................
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A
00000000000 HEX............................................................................................................
Example 1.9......................................................................................................................
Example 1.9 (Continued)..................................................................................................
-0, 10111 × 2-126.................................................................................................................
-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)..........................................................
= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)............................................................
≈ [-0.71875 × 1.1754943508222875079687365372222](10)..........................................
≈ -8.44886564653519146352938612849 × 10-39..............................................................
[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )...............................................................
[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )....................................................
Solution of Equations No................................................................................................................
Linear...............................................................................................................................................
Theorem 2.1..........................................................................................................................
Example 2.1......................................................................................................................
f (0) = -1 < 0......................................................................................................................
f (1)=1>0...........................................................................................................................
Methods for Solving Nonlinear Equations in One Variable....................................................
2.2.1 Bisection Method.......................................................................................................
Example 2.2......................................................................................................................
x 1 = 1 x2 = 2.................................................................................................................
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578................................................................................
f ( x3 ) = f ( 1.5 ) = -0.1823...............................................................................................
Example 2.2 (Continued)..................................................................................................
× 100=...............................................................................................................................
1.5-1..................................................................................................................................
1.5......................................................................................................................................
× 100 = 33.33%.................................................................................................................
x1 + x3 1 + 1.5..............................................................................................................
= 1.25................................................................................................................................
f ( x4 ) = f ( 1.25 ) = 0.0634...............................................................................................
× 100=...............................................................................................................................
1.5 - 1.25...........................................................................................................................
1.5......................................................................................................................................
× 100= 16.66%..................................................................................................................
x 4 + x 3 1.25 + 1.5........................................................................................................
= 1.375..............................................................................................................................
f ( x5 ) = f ( 1.375 ) = -0.0656...........................................................................................
2.2.2 False Position Method...............................................................................................
f ( xb ) -f ( xa ).....................................................................................................................
x1 = 0................................................................................................................................
x2 = 1................................................................................................................................
f ( x1 ) = -1.........................................................................................................................
f ( x2 ) = 0.7854...............................................................................................................
x 3= x 1 -
f ( x 2 ) - f ( x 1 ) = 0...............................................................................................
f (0)(1-0) f (1)- f (0) = 0......................................................................................................
(-1)(1 - 0)...........................................................................................................................
0.7854 - (-1) 1.7854........................................................................................................
= 0.5601............................................................................................................................
f ( x3 ) = f ( 0.5601 ) = 0.0707...........................................................................................
× 100=...............................................................................................................................
0.5601 - 1..........................................................................................................................

Department of Basic Sciences


0.5601................................................................................................................................
× 100 = 78.54%.................................................................................................................
(-1)(0.5601 - 0) 0.5601...................................................................................................
0.0707 - (-1) = 1.07070=0.5231..........................................................................................
f ( x4 ) = f ( 0.5231 ) = 0.0051...........................................................................................
Example 2.3 (Continued)......................................................................................................
Definition 2.3........................................................................................................................
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )..............................................................................................
2.2.4 Newton-Raphson method.......................................................................................
f 0 ( x ) =.............................................................................................................................
f ( x i ) - 0 x i - x i + 1........................................................................................................
f 0 ( x i )..............................................................................................................................
g ( x ) = x -.........................................................................................................................
f ( x ) f0 ( x )........................................................................................................................
Example 2.5......................................................................................................................
e - x i + x i - 2......................................................................................................................
- e - x i + 1...........................................................................................................................
e - x 0 + x 0.........................................................................................................................
-2.......................................................................................................................................
- e - x 0 + 1..........................................................................................................................
1-.......................................................................................................................................
e -(1) +(1)-2.........................................................................................................................
e +( )
= 1.843482357250334........................................................................................
-(1)....................................................................................................................................
e - x 1 + x 1 - 2....................................................................................................................
- e - x 1 + 1.........................................................................................................................
1-.......................................................................................................................................
e -(1.843482357250334) + (1.843482357250334) - 2.....................................................................
= 1.841406066157926......................................................................................................
e - x 2 + x 2 - 2....................................................................................................................
- e - x 2 + 1.........................................................................................................................
1.841405660436976..........................................................................................................
e - x 3 + x 3 - 2....................................................................................................................
- e - x 3 + 1.........................................................................................................................
1.841405660436961..........................................................................................................
e - x 4 + x 4 - 2....................................................................................................................
- e - x 4 + 1..........................................................................................................................
1.841405660436961..........................................................................................................
2.2.5 Secant Method.......................................................................................................
f ( x2 ) -f ( x1 )...................................................................................................................
f 0 ( x i )..............................................................................................................................
f 0 ( x i )..............................................................................................................................
xi +1= xi -
f ( x i ) - f ( x i - 1 )................................................................................................
Example 2.6......................................................................................................................
Yo
f ( x i )( x i - x i - 1 ) = x - f ( x i )- f ( x i - 1 ) ............................................................
x i
+ x i -2)( x i - x i - 1 ).....................................................................................................
( e - x i + x i -2) - ( e - x i......................................................................................................
1 + x i - 1 - 2)....................................................................................................................
( e -(1) +(1)-2)(1-0).............................................................................................................
( e -(1) +(1)-2)-( e -(0) +(0)-2)...............................................................................................
= 2.718281828459045......................................................................................................
Interpolation and curve fitting.........................................................................................................
f ( x k ) = y k , k = 1,..., n...................................................................................................
3.1 NEWTON'S INTERPOLATING POLYNOMIAL..........................................................................
P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · +
b n ( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )...........................................................................
P n ( x ) = b 0 +∑ b i ∏( x - x j )....................................................................................
f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ] x i + j + 1 - x i
...........................................................................................................................................
P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]
( x - x 0 )( x - x 1 ) +·· · + f [ x 0 , x 1 ,..., x n ]( x - x 0 )( x - x 1
) · · · ( x - x n - 1 ).............................................................................................................
Example 3.1......................................................................................................................

Department of Basic Sciences


Level b 1 :.........................................................................................................................
2-9 -7 (-1)-2 -3 (0 - (-1) 1.............................................................................................
5-(-3)= 8 7-5 = 2 8-7 = 1..................................................................................................
3.2.................................................................................................................................................
Lagrange Interpolating Polynomial..........................................................................................
Example 3.2......................................................................................................................
P ( x ).................................................................................................................................
9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0 L 3 ( x )..........................................................
,
..........................................................................................................................................
Example 3.2 (Continued)..................................................................................................
L 0 ( x ) = ∏ x - xk............................................................................................................
-3-5 -3-7 -3-8 x -5 x -7 x -8...............................................................................................
=
-8 .-10.-11...................................................................................................................
-x3 x 2 131x7.................................................................................................................
=
880 + 44 - 880 + 22......................................................................................................
L1 ( x ) = ∏ x - xk.............................................................................................................
x - x 0 x - x 2 x - x 3.............................................................................................................
= ...................................................................................................................................
x - (-3) x -7 x - 8................................................................................................................
=
5-(-3).5-7.5-8...................................................................................................................
x +3 x -7 x -8.....................................................................................................................
=
8 . -2 . -3.....................................................................................................................
x3 x 2 11x7...................................................................................................................
=
48 + 4 - 48 + 2...................................................................................................................
L2 ( x ) = ∏ x - xk.............................................................................................................
x - (-3) x - 5 x - 8...............................................................................................................
=
7-(-3).7-5.7-8...................................................................................................................
x +3 x -5 x -8.....................................................................................................................
=
10 . 2 . -1.....................................................................................................................
-x3 x 2 x 6......................................................................................................................
=
20 + 2 - 20 -.................................................................................................................
Example 3.2 (Continued)..................................................................................................
Segmental Interpolation: Cubic Tracers...................................................................................
x 0 < x 1 < . . . < xn..............................................................................................................
( k )....................................................................................................................................
p3(x)≡pk(x)=ak+bk(x−xk)+ck(x−xk)2+dk(
x − x k ) 3...........................................................................................................................
Example 3.3......................................................................................................................
Example 3.3 (Continued)..................................................................................................
2k1 + 0.6k2 = -8.35.........................................................................................................
0.6k1 + 1.6k2 + 0.2k3 = 1.9...........................................................................................
0.2k2 + 1.2k3 + 0.4k4 = 4.8...........................................................................................
0.4k3 + 1.66k4 = 1.84.......................................................................................................
k 1 = -4, 9588....................................................................................................................
k 2 = 2, 6128.....................................................................................................................
k 3 = 3, 4740.....................................................................................................................
k4 = 0.2713.......................................................................................................................
Example 3.3 (Continued)..................................................................................................
Curve Fitting by Least Squares................................................................................................
3.4.1 Least Squares and Linear Regression Analysis...............................................................
Criteria:.............................................................................................................................
Example 3.4......................................................................................................................
A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = 16 ,..................................................
b =.....................................................................................................................................
5(260) - (16)(47)...............................................................................................................
5(106) - (16)2.....................................................................................................................
(106)(47) - (206)(16).........................................................................................................
5(106) - (16)2.....................................................................................................................
a =2...................................................................................................................................
b =3...................................................................................................................................
Example 3.5......................................................................................................................
A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D

Department of Basic Sciences


=∑ and i = 43.5 60
b =.....................................................................................................................................
8(546.63) - 89.8(43.5).......................................................................................................
8(1292.92) - (89.8)2 =⇒
...................................................................................................
(1292.92)(43.5) - 596.63(89.8).........................................................................................
8(1292.92) - (89.8)2...........................................................................................................
a = 0.20477.......................................................................................................................
b = 3.1389.........................................................................................................................
3.4.2 Linearization of Nonlinear Relationships.......................................................................
3.4.2.1 Model Power......................................................................................................
y = α x β.............................................................................................................................
| {z} |{z} | {z}.............................................................................................................
Example 3.6 (Continued)..................................................................................................
A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C =∑ x i =....................................................
a =.....................................................................................................................................
b =.....................................................................................................................................
5(1.9209) - (-2.0756)(-1.7657)..........................................................................................
5(1.4556) - (-2.0756)2 =
..................................................................................................
(1.4556)(-1.7657) - (1.9209)(-2.0756)..............................................................................
5(1.4556) - (-2.0756)2.......................................................................................................
a = 1.999947.....................................................................................................................
b = 0.477078.....................................................................................................................
Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997....................................................
β = 1.999947.....................................................................................................................
y = 2.9963 x 1.999947.............................................................................................................
Numerical Differentiation and Integration......................................................................................
Numerical Differentiation........................................................................................................
4.1.1 Accuracy Formulas for the First Derivative....................................................................
Three Point Formulas...............................................................................................................
. Forward:..........................................................................................................................
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 ).....................................................
. Backward:.......................................................................................................................
f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 ) 2 hours..............................................
Five Point Formulas.................................................................................................................
. Forward:..........................................................................................................................
-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f
( x i +4 h )..........................................................................................................................
. Central:............................................................................................................................
f 0 ( x i ) =..........................................................................................................................
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2 h ).....................................................
+ O ( h4 )...........................................................................................................................
Example 4.1......................................................................................................................
. Three Point Formula: If h = 0.1 we have to..................................................................
-3 f (2)+4 f (2+0.1)- f (2+2(0.1)).......................................................................................
2 ∗ (0.1)............................................................................................................................
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404......................................................
0.2......................................................................................................................................
-0.417756085....................................................................................................................
. Five Point Formula: If h = 0.1 we have to....................................................................
-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f
(2+4(0.1))..........................................................................................................................
12 ∗ (0.1)..........................................................................................................................
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 +
16 × 0.745705212 - 3 × 0.675463180...............................................................................
1.2......................................................................................................................................
-0.416135615....................................................................................................................
Clearly f 0 (2) = cos(2)...................................................................................................
-0.416146837 - (-0.416135615)........................................................................................
-0.416146837....................................................................................................................
-0.416146837 - (-0.417756085)........................................................................................
-0.416146837....................................................................................................................
× 100 ≈ 0.38% and...........................................................................................................
4.2.................................................................................................................................................

Department of Basic Sciences


Numerical Integration..............................................................................................................
4.2.1 Newton-Cotes formulas.............................................................................................
simple trapezoid rule.........................................................................................................
P ( x ) = f ( a ) + f ( b ) - f ( a ) ( x - a ) b - a..........................................................................
Zbf (
x ) to...........................................................................................................................
f ( b ) -f ( a ) b - a.............................................................................................................
/(b)-/(a) (xa)21 b...............................................................................................................
b - a 2................................................................................................................................
( b - a ) f ( a )+ f ( b )............................................................................................................
Example 4.2......................................................................................................................
2.........................................................................................................................................
π
2 + 6π...............................................................................................................................
4.........................................................................................................................................
π........................................................................................................................................
π
= 0.5235988....................................................................................................................
6.........................................................................................................................................
5π-6 3................................................................................................................................
12.......................................................................................................................................
0.4429715.........................................................................................................................
0.4429715 - 0.5235988.....................................................................................................
0.4429715..........................................................................................................................
Composite Trapezoid Rule...............................................................................................
f ( x ) dx + f ( x ) dx +. . . + f ( x ) dx..............................................................................
x0 x 1 x n -1................................................................................................................
I = h f ( x 0 )+2 f ( x 1 ) + h f ( x 1 )+2 f ( x 2 ) +...+ h.............................................................
f ( xn − 1 ) + f (xn )...........................................................................................................
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n ).....................................................................................
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )............................................................................................
i =1
I=(b-a) .............................................................................................................
2 n

Et =...................................................................................................................................
( b - a ) 3 12 n 3..................................................................................................................
∑ f 00 (ξ i )..........................................................................................................................
∑ f 00 (ξ i )..........................................................................................................................
f 00 ' i =1...............................................................................................................................
Example 4.3......................................................................................................................
2.........................................................................................................................................
b - a 1................................................................................................................................
2
= 0.1................................................................................................................................
x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1]...........................................................................................
0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]............................................
0.4513161..........................................................................................................................
E r =..................................................................................................................................
0.4429715 - 0.4513161.....................................................................................................
0.4429715..........................................................................................................................
· 100 ≈ 1.884%..................................................................................................................
Differential equations......................................................................................................................
Ordinary...........................................................................................................................................
5.1 EULER METHOD..............................................................................................................
dy = f ( x , y ), y (0) = x 0..................................................................................................
f ( xn , yn ) =......................................................................................................................
( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )..............................................................................
Example 5.1......................................................................................................................
1. Analytical Solution....................................................................................................
Z dy
= Z 2 xdx.........................................................................................................................
Ln | and | = x 2 + C.............................................................................................................
Example 5.1 (Continued)..................................................................................................
Ln |1| =0+ C =⇒ C =0.......................................................................................................
Ln | and | = x 2 =⇒ y = e x 2................................................................................................
2. Numerical Solution....................................................................................................
x 1 = x 0 + h =0+0.1 =0.1.................................................................................................
y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which
means ( x 1 , y 1 ) = (0.1, 1)............................................................................................

Department of Basic Sciences


x 2 = x 1 + h = 0.1 +0.1 =0.2............................................................................................
y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2
)=(0.2,1.02).......................................................................................................................
0.3 1.0608.....................................................................................................................
0.4 1.124448.................................................................................................................
0.5 1.21440384.............................................................................................................
0.6 1.33584422.............................................................................................................
0.7 1.49614553.............................................................................................................
0.8 1.70560591.............................................................................................................
0.9 1.97850285.............................................................................................................
1 2.33463336................................................................................................................
3. Approximate y (0.5)...................................................................................................
Example 5.1 (Continued)..................................................................................................
1.28403 - 1.21440.............................................................................................................
1.28403..............................................................................................................................
4. Graph and comparison of results...............................................................................
Fourth Order Runge-Kutta Method..........................................................................................
d y
dx = f ( x , y ), y (0) = x 0................................................................................................
k2=f(xn+h2,yn+h2k1)k3=f(xn+h2,yn+h2k2)k
4 = f ( x n + h , y n + hk 3 )...............................................................................................
6.........................................................................................................................................
Example 5.2......................................................................................................................
1. Analytical Solution....................................................................................................
Example 5.2 (Continued)..................................................................................................
3. Approximate y(0.5)...................................................................................................
1.28403 - 1.28402.............................................................................................................
1.28403..............................................................................................................................
· 100 ≈ 0.000779%............................................................................................................
4. Graph and comparison of results..................................................................................
Introduction to Matlab.....................................................................................................................
Matlab Environment Overview................................................................................................
. Save, Recall and Exit a work session.................................................................................
. Current Work Directory.....................................................................................................
. Delete Work Session Variables..........................................................................................
. Clean screen.......................................................................................................................
. Aids....................................................................................................................................
. Variables in use..................................................................................................................
Basic Constants and Operators.................................................................................................
a) Constants..............................................................................................................................
b) Operations Between Scalars................................................................................................
f) Useful Functions...................................................................................................................
A.3................................................................................................................................................
Elemental Functions.................................................................................................................
A.4................................................................................................................................................
Matrices and Vectors................................................................................................................
a) Matrices................................................................................................................................
1. Create a Matrix..............................................................................................................
Example A.1.........................................................................................................................
42 44 70.............................................................................................................................
50 51..................................................................................................................................
2. Modify Elements of an Array........................................................................................
Example A.2.........................................................................................................................
3. Add or Delete Rows or Columns from a Matrix...........................................................
Example A.3.........................................................................................................................
Example A.4.........................................................................................................................
Example A.5.........................................................................................................................
Example A.6.........................................................................................................................
b) Vectors..............................................................................................................................
1. Vector Creation.............................................................................................................
Example A.7.........................................................................................................................
Example A.8.........................................................................................................................
2. Product of a Vector and a Scalar...................................................................................

Department of Basic Sciences


Example A.9.........................................................................................................................
3. Product point.................................................................................................................
Example A.10.......................................................................................................................
4. Cross Product................................................................................................................
Example A.11.......................................................................................................................
c) Basic Operations Between Matrices and Between Vectors..............................................
Graph of Functions in 2D.........................................................................................................
a) In Rectangular or Cartesian Coordinates.............................................................................
Graph of a Simple Function.....................................................................................................
Example A.12.......................................................................................................................
Example A.12 (Continued)...................................................................................................
2. Second Option: Using the function fplot('Function',Limits,
[line specifiers]) ...................................................................................................................
3. Third Option: Using the function ezplot('Function',Limits,
[line specifiers]) ...................................................................................................................
Graph of Several Functions on the Same Plane.......................................................................
. First Option: Use The plot() Function ............................................................................
Example A.13.......................................................................................................................
Example A.14.......................................................................................................................
Graph of a Piecewise Function.................................................................................................
Example A.15.......................................................................................................................
b) In Polar Coordinates............................................................................................................
Example A.16.......................................................................................................................
A.6................................................................................................................................................
Symbolic Calculation...............................................................................................................
a) Algebraic Expressions..........................................................................................................
Useful Functions About Algebraic Expression........................................................................
Example A.17.......................................................................................................................
Evaluate a Symbolic Expression..............................................................................................
Example A.18.......................................................................................................................
b) Solution of Polynomial Equations and Systems of Linear
Equations..................................................................................................................................
Example A.19.......................................................................................................................
Example A.20.......................................................................................................................
c) Limit Calculation..............................................................................................................
Example A.21.......................................................................................................................
3. Lim.............................................................................................................................
-4.......................................................................................................................................
4. lim e - x.......................................................................................................................
1.........................................................................................................................................
d) Derivative Calculus...........................................................................................................
Example A.22.......................................................................................................................
d) Calculus of Integrals............................................................................................................
Example A.23.......................................................................................................................
A.7................................................................................................................................................
Programming in Matlab...........................................................................................................
The Function and Script Editor................................................................................................
How do we access the editor?...............................................................................................
How do you run a script?......................................................................................................
How is a function executed?.................................................................................................
How do you program in Matlab?..........................................................................................
a) IF Control Statement.........................................................................................................
b) SWITCH Control Statement.............................................................................................
c) FOR Repetition Sentence..................................................................................................
d) WHILE Repetition Sentence.............................................................................................
e) Creation of Functions and Scripts.....................................................................................
. Features..................................................................................................................................
Example A.24.......................................................................................................................
Example A.25.......................................................................................................................
Example A.26.......................................................................................................................
Example A.27.......................................................................................................................
. Scripts.....................................................................................................................................
Example A.28.......................................................................................................................

Department of Basic Sciences


Example A.29.......................................................................................................................
Bibliography....................................................................................................................................
Introduction
Numerical Analysis is a branch of mathematics that, through the use of iterative algorithms, obtains numerical
solutions to problems in which symbolic (or analytical) mathematics is inefficient and consequently cannot offer a
solution. In particular, these algorithms are called numerical methods .

Numerical methods are generally composed of a finite number of steps that are executed manually. was logical,
improving initial approximations to a certain quantity, such as the root of an equation, until a certain error limit is met.
This cyclical operation of value improvement is known as iteration.

Numerical analysis is a very efficient alternative for solving equations, both algebraic (polynomials) and
transcendental, having a very important advantage over other types of methods: The repetition of logical instructions
(iterations), a process that allows improving the values initially considered. as a solution. Since it is always the same
logical operation, it is very perti nent the use of computing resources to perform this task.

The development and rise of the use of numerical analysis run parallel to the technological development of
computing. Computers (and consequently also calculators) are capable of performing a practically infinite multitude of
algebraic operations in very small time intervals; This makes them the ideal tool for the application of numerical
methods. In fact, the analysis Numerical sis turns out to be the natural way to solve mathematical models (of an
algebraic or transcendent nature for both continuous and discrete mathematics) through the computer.

On the other hand, as a direct consequence of the application of numerical solutions and the growth of computational
resources, the incorporation of mathematical simulation as a form of study of various systems has also been
achieved.

However, there must be clarity in the sense that numerical analysis is not the panacea in solving mathematical
problems.

A consequence of the above is that, in general, numerical methods yield numerical solutions. If in a certain case you
wish to obtain analytical solutions you should resort to the usual algebraic procedures. On the other hand, the
numerical solutions turn out to be approx. mations, that is, they are rarely exact solutions.

As would be analyzed in due course, numerical solutions carry a limit of error. This error, which although it may be as
small as the calculation resources allow, is always present and its management must be considered in the
development of the required solutions.

It is very possible that you are aware of various computer systems that provide analytical solutions. These systems do
not replace numerical methods, in fact they are a complement to the integral process of modeling physical systems
that are the fundamental element of Engineering practice.
Errors and Floating Point
Representation

Mistakes

A frequent activity of the Engineering professional consists of working with mathematical models representing
representative of a physical phenomenon. These models are mathematical abstractions that are far from accurately
representing the phenomenon under study due mainly to the shortcomings and difficulties that humans still have in
fully understanding nature.

As a consequence of this, there are differences between the results obtained experimentally and those obtained.
specifically added to the mathematical model.

The quantitative differences between the two models are called Errors .

1.1.1 Numerical approximation and error theory


We must always settle, in the practice of engineering and science, with an approximate solution given to a problem for
the following reasons:

1. Mathematical models are approximate; this is; simplifications to the real problem. Not all the factors that affect a
phenomenon are taken into account. For example, in the case of parabolic shooting, air resistance is usually
neglected, however, it can be important.

2. Mathematical models require parameters, which most of the time come from experimental measurements and
these only have a limited precision, which depends on the measuring instrument. For example the ideal gas
constant. They can also come from calculations and these have a limited precision that depends on both the
method and the calculation instrument used. For example π .

3. The resulting mathematical models are impossible to solve by analytical methods and the solution must be
approximated numerically. For example a fifth degree equation.

Due to the above, we humbly have to accept that errors will always be present, these can be classified as:

• Inherent errors.

Numerical analysis

Department of Basic Sciences


3

• Truncation errors.

• Rounding errors.

Definition 1.1
Inherent errors are those that have the input data of a problem, and are mainly due to the fact that they are obtained experimentally,
due both to the measuring instrument and to the conditions under which the experiment is carried out.

For example, if the experiment is at a constant temperature and this is only achieved approximately. They may also be due to being
obtained from previous calculations. For example, the calculated value is that of an irrational number such as T or V2.

Definition 1.2
Truncation errors originate from approximating the analytical solution of a problem by means of a numerical method.

For example, when evaluating the exponential function through the Taylor series, the value of the following infinite series must be
calculated:

Given the impossibility of taking all the terms of the series, truncation is required after a certain number of terms. This certainly
introduces us to an error, which is the truncation error. This is independent of the way the calculations are carried out. It only depends
on the numerical method used.

Definition 1.3
Rounding errors originate when performing calculations that every numerical or analytical method requires. want and are due to
the impossibility of taking all the digits that result from arithmetic operations metics such as products and quotients, having to retain
in each operation the number of digits allowed by the calculation instrument being used.

For example, when calculating the value of 3, we have to keep only the greatest number of digits 3 that our calculation instrument
handles.
The above errors are also often referred to as the sources of error. The magnitude of the error ated by some or all of the sources of
error mentioned above, can be quantified with the help of the following parameters:

• Real Error

• Relative Error

• Percentage Error

Numerical analysis

Department of Basic Sciences


4 Errors and Floating Point Representation

Definition 1.4
The Real Error is defined as the difference between the real value V r and an approximation to this value Va:

E t =Vr- Va

Definition 1.5
The relative error is defined as the quotient of the Real Error between the real value Vr (yes Vr 7 0):

Definition 1.6
The percentage error is simply the Relative Error expressed in percent (%).

Vr - V, Erp = -y" X 100(%)

It is also common to use the absolute value in the previous parameters, in which case they are called re spectively Absolute
Real Error, Absolute Relative Error and Absolute Percentage Error.

1.1.2 Significant numbers

The concept of significant figures has been developed to formally designate the reliability of a numerical value.

Definition 1.7
The number of significant figures is the number of digits that can be used with full confidence.

For example, we can calculate an irrational number with several figures, but not all of them, especially the last ones, can be
taken with full confidence that they are correct. On the other hand, zeros are not always significant figures since they can
only be used to locate the decimal point. For example I followed them The numbers all have 4 significant figures:
0.00001985, 0.0001985, 0.001985, 1985, 19.85. to ase To ensure that a zero represents a significant figure, it is common to
use scientific notation. For example For example, the following numbers have 3, 4 and 5 significant figures: 4.53 x 10-5,
4.530 x 10-5 and 4.5300 x 10-5. Zeros are also usually explicitly entered. The following numbers have 5 significant
figures: 19850, 0.019850, 19.850.

Non-significant figures appear as a result of calculations and have no meaning. The significant figures of a number are
determined by its error. Significant figures are those that occupy a position equal to or higher than the order or position of
the error. For example, consider a length measurement that returns a value of 5432.4764 m with an error of 0.8 m. The
error is therefore of the order of tenths of a meter. It is evident that all the digits of the number that occupy a position less
than tenths do not provide any information. Indeed, what sense does it make to give the number with precision of ten
thousandths if we affirm that the error is almost 1 meter? The significant figures in the number will therefore be those that
occupy the position of tenths, units, tens, etc., but not hundredths, thousandths and ten thousandths.

Numerical analysis

Department of Basic Sciences


5

Considerations:
In a scientific work or article, care must always be taken to ensure that these figures are appropriate. To know the correct number of
significant figures, the following rules are followed:

• Any non-zero digit is significant, whether it is 643 (which has three significant figures) or 9.873 kg (which has four).

• Zeros placed between different numbers are significant, whether it is 901 cm (having three significant figures) or 10,609 kg
(having five significant figures).

• Zeros to the left of the first nonzero number are not significant, either 0.03 cm (which has only one significant figure) or
0.0000000000000395 (this one has only three), and so on.

• Since a number greater than one, to the right, after the decimal point written zeros also count as significant figures, either 2.0
dm (which has two significant figures) or 10.093 cm (which has five figures).

• In numbers that have zeros after a non-zero digit, without being a decimal, they may or may not be significant figures, either
like 600 kg, it may have one significant figure (the number 6), perhaps two (60), or you can have all three (600). To know in
this case what is the correct number of digits: nificantly we need more data about the procedure with which the measurement
was obtained (the device, etc.) or we can use scientific notation, indicating the number 600 as 6 x 10 2 (six multi multiplied
by ten to the power of two) having only one significant figure (the number 6) or 6.0 x 10 2 , we have two significant figures
(6.0) or 6.00 x 10 2 , specifying having three significant figures.

When expressing a number, the use of non-significant figures should always be avoided, since it can be a source of confusion.
Numbers should be rounded so that they contain only significant figures. The process of eliminating non-significant figures from a
number is called rounding.

The rules that we will use when rounding numbers are the following:

• If the figure that is omitted is less than 5, it is simply eliminated.

• If the number removed is greater than 5, the last number retained is increased by one unit.

• If the eliminated digit is 5, the closest even number is taken as the last digit; That is, if the retained figure is even, it is left, and
if it is odd, the higher figure is taken.

Example 1.1

If we round 3.678 to three significant figures, the result is 3.68, which is closer to the original than 3.67. On the other hand, if the
number to be rounded, also to three figures, was 3.673, it would be 3.67, which is closer to the original than 3.68. To round 3.675,
according to the third rule, we must leave 3.68.

The first two rules are common sense. The third is a reasonable agreement because, if it is always followed pre, half the time we
round down and half the time we round up. When numbers to be rounded

Numerical analysis

Department of Basic Sciences


6 Errors and Floating Point Representation

are large, the eliminated figures are replaced by zeros. For example, the number 3875 rounded to a significant figure
becomes 4000. In this case, exponential notation is usually preferred, since if you wrote mos '4000' it may not be clear
whether the zeros are significant figures or not. In fact, by writing 4 x 10 3 it is clear that only the figure '4' is significant,
since if the zeros were also significant we would write 4,000 x 10 3 .

Rules for operations with significant figures.

Rule 1: Experimental results are expressed with only a doubtful figure, and indicating the uncertainty in the measurement
with ±.

Rule 2: Significant figures are counted from left to right, starting from the first non-zero digit and ending with the
doubtful digit.

Rule 3: When adding or subtracting two decimal numbers, the number of decimal places in the result is equal to the
quantity with the smallest number of them.

Attention: A case of special interest is that of subtraction. Let's quote the following example: 30.3475 — 30.3472 =
0.0003.

Let's note that each of the quantities has six significant figures and the result has only one. Significant figures have been
lost when subtracting. This is important to keep in mind when working with calculators or computers where there are
figures that are added and subtracted. It is advisable to do the additions first and then the subtractions to lose as few
significant figures as possible.

Rule 4: When multiplying or dividing two numbers, the number of significant figures of the result is equal to that of the
factor with the fewest figures.

1.1.3 Precision and accuracy

Accuracy indicates the results of the proximity of the measurement with respect to the true value, while after the precision
with respect to the repeatability or reproducibility of the measurement.

In engineering, science, industry and statistics, accuracy and precision are not equivalent.

Definition 1.8
Precision refers to the dispersion of the set of values obtained from repeated measurements of a quantity. The smaller the
dispersion, the greater the precision.

A common measure of variability is the standard deviation of measurements, and precision can be estimated as a function
of it.

Definition 1.9
Accuracy refers to how close the measured value is to the true value. In statistical terms, accuracy is related to the bias of
an estimate. The smaller the bias, the more accurate an estimate is.

Numerical analysis

Department of Basic Sciences


7

When we express the accuracy of a result, it is expressed by the absolute error, which is the difference between the experimental
value and the true value.

Precision
high Low

1.1.4 Types of Rounding

When performing the calculations that every numerical or analytical method requires, we must round. I'll stop dondear is usually
used:

• Truncated rounding

• Symmetrical rounding.

Definition 1.10
Truncated rounding consists of truncating the result of an operation to the number of significant figures being used.

Example 1.2

If we round 9 to 4 significant figures we have 0.7777.

Numerical analysis

Department of Basic Sciences


8 Errors and Floating Point Representation

Definition 1.11
Symmetric rounding consists of increasing the last retained figure by one if the first discarded figure is between 5 and 9, or
leaving it the same if the first discarded figure is between 0 and 4.

Example 1.3

If we round 9 to 4 significant figures we have 0.7778.

Example 1.4

It is assumed that 3 + 3 = 1. In practice this may not be the case. Yes We perform the sum using only
4 significant figures and we use both types of rounding. Is obtained:

0.3333 + 0.6666 = 0.9999 (Truncated rounding)

0.3333 + 0.6667 = 1.000 (Symmetrical rounding)

It can be shown that symmetrical rounding generally leads to more accurate results.

1.2 Floating Point Representation

The fundamental unit by which information is represented in a computer is called pal open. This is an entity that consists of
a string of binary digits or bits. Usually numbers are stored in one or more words.

Fractional quantities are usually represented on the computer using floating point form. With this method, the number is
expressed as a fractional part, called a mantissa or signifier. cando and an integer part, called an exponent or characteristic,
that is,

mxbe

Where m is the mantissa, b the base and e the characteristic. When representing a number in a word, it must be taken into
account that the first bit is reserved for the sign of the number (positive 0, negative 1), the next series of bits for the signed
characteristic or exponent and the last bits for the mantissa

Numerical analysis

Department of Basic Sciences


9

1.2.1 Normalization of a Number

Definition 1.12
A number x is written in its normalized form, if x is written in the following form:

x = 5 x (0.dd2dzd4 ■■■dp) b xb e

Where 5 is the sign, +1 if it is positive and -1 if it is negative; 0.d d2dzd4 - dp is the mantissa in base b, for 0 < di < b — 1 where d 7
0; ye is the characteristic.

T Yo d? d'i ik dp ..... ...


The mantissa 0.dd2d3d4 ■■ - dp denotes the sum l 1 4
-------h,, the
p
integer p means the quantity
b b I) b b
of digits of precision of the number and e the integer such that L < e < U, with L being the smallest integer exponent it can take and
U the largest.

Example 1.5

Represent the number -0.00523 in a 16-bit word, where b = 10.

First of all we must normalize the number, that is,

-0.00523 = (-l)(O.523) lo x 10 2

Now its representation:

l|l|2|0|0|0|0|0|0|0|0|0|0|5|2|3

1.2.2 IEEE 754 Standard

The IEEE 754 standard has been defined by the Institute of Electrical and Electronics Engineers (IEEE) and establishes two basic
formats for representing real numbers in the digital computer: single precision and double precision.

1.2.2.1 Simple Precision In single precision, 32 bits (4 bytes) are used to write a real number: 1 bit for the sign (s) of the number,
23 bits for the mantissa (m) and 8 bits for the exponent or characteristic (e). , which are distributed as follows:

31 30 • • • 23 22 0
yes and m
Sign Exponent Mantissa

The exponent is usually represented in Excess 2" 1 — 1, while for the mantissa, Sign Magnitude is usually used. Additionally, the
mantissa is usually normalized by placing the decimal point to the right of the most significant bit.

Numerical analysis

Department of Basic Sciences


10 Errors and Floating Point Representation

Example 1.6

To write the number 101110, 010101110100001111100001111100010011 2 in the IEEE 754 standard with single
precision, exponent in Excess to 2 n -1 - 1 and mantissa in Sign Magnitude, you must first normalize it:

1, 011100101011101000011111000011111000100112 ×25

n -1
The exponent, in excess of 2 - 1 , will be: 5 10 + (28 - 1 - 1) 10 =5 10 + (2 7 - 1) 10 =5 10 + (128 - 1) 10 = 132 10 ≡
10000100 2

The most significant 23-bit bits are taken from the mantissa:

1, 0111001010111000000111

The rest of the bits cannot be represented, since they do not fit in the mantissa. However, when the mantissa is
normalized by placing the decimal point to the right of the most significant bit, that bit is always equal to 1.
Therefore, you can do without it, and instead take one more bit of the mantissa. In this way, the precision of the
number represented is greater. Thus, the bits of the mantissa will be:

01110010101110100001111

The omitted bit is called the implicit bit. On the other hand, the sign bit is equal to 0, since the number is positive.
Consequently, the number can be represented as:

0100 0010 0011 1001 0101 1101 0000 1111


42395D0F

So that,

101110, 010101110100001111100001111100010011 2 ≈42395 D 0 F HEX

In this case, the numbers are not exactly the same, since, with simple precision, not all the bits of the mantissa
could be represented.

Numerical analysis

Department of Basic Sciences


1
1

Example 1.7

Given the number 3 E 400000 HEX of the IEEE 754 standard with single precision, exponent in excess of 2 n -1 - 1 and mantissa
in Sign Magnitude with implicit bit, to find out what number it represents in base 10, the following steps can be performed :

1. Convert 3E40000016 to base 2:

3 AND 0 0 0 0 0 0
0011 1110 0100 0000 0000 0000 0000 0000

2. Get the bits of the sign, the mantissa and the exponent:

31 30 · · · 23 22 ··· 0
0 01111100 10000000000000000000000
Sign Exponent Mantissa

3. Convert the exponent to base 10:

011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 - 1)10 = 12410?12710 = -3

4. Write the number in scientific notation. To do this, the mantissa must be written with the im bit implicit (1), followed by the
decimal point (,) and the mantissa bits (10000000000000000000000) , taking into account that zeros on the right can be
ignored. On the other hand, the number is positive, since the sign bit is 0. Therefore, the number is:

1,1 ×2-3

5. Express the number in base 10. To do this, there are two ways to do it, the first is:

1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0, 187510

and the second:

1,1×2-3=((20+2-1)×2-3)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510

Therefore,

3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10

Numerical analysis

Department of Basic Sciences


12 Errors and Floating Point Representation

1.2.2.2 Double Precision On the other hand, in double precision, 64 bits (8 bytes) are used to write a real number: 1 bit
for the sign (s) of the number, 52 bits for the mantissa (m) and 11 bits for the exponent or characteristic (e).
63 62 · · · 52 51 ··· 0
yes and m
Sign Exponent Mantissa

Example 1.8

If you want to write the number 19.5625 10 in the IEEE 754 standard with double precision, exponent in excess of 2
n -1
- 1 and mantissa in Sign Magnitude with implicit bit, the steps to follow are:

1. Change 19.5625 10 to base 2.

First the whole part:


19 2
Content..........................................................................................................................................................2
Introduction..............................................................................................................................................32
Errors and Floating Point Representation....................................................................................................33
Mistakes...............................................................................................................................................33
1.1.1 Numerical approximation and error theory...........................................................................33
Definition 1.3......................................................................................................................................3
Definition 1.6......................................................................................................................................4
1.1.2 Significant numbers..........................................................................................................4
Definition 1.7......................................................................................................................................4
Example 1.1........................................................................................................................................5
Rules for operations with significant figures......................................................................................6
1.1.3 Precision and accuracy....................................................................................................6
Definition 1.9......................................................................................................................................6
high Low.............................................................................................................................................7
1.1.4 Types of Rounding............................................................................................................7
Example 1.2........................................................................................................................................7
1.2.1 Normalization of a Number..............................................................................................9
Definition 1.12....................................................................................................................................9
Example 1.5........................................................................................................................................9
1.2.2 IEEE 754 Standard...........................................................................................................9
Example 1.6..................................................................................................................................10
1, 011100101011101000011111000011111000100112 ×25........................................................10
The exponent, in excess of 2 n -1 - 1 , will be: 5 10 + (28 - 1 - 1) 10 = 5 10 + (2 7 - 1) 10 = 5
Department of Basic Sciences
1
3

10 + (128 - 1) 10 = 132 10 ≡ 10000100 2...................................................................................10


1, 0111001010111000000111...................................................................................................10
01110010101110100001111........................................................................................................10
0100 0010 0011 1001 0101 1101 0000 1111...............................................................................10
101110, 010101110100001111100001111100010011 2 ≈42395 D 0 F HEX.............................10
Example 1.7..................................................................................................................................11
1. Convert 3E40000016 to base 2:............................................................................................11
2. Get the bits of the sign, the mantissa and the exponent:.......................................................11
3. Convert the exponent to base 10:..........................................................................................11
011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 - 1)10 = 12410?12710 = -3......11
1,1 ×2-3..........................................................................................................................................11
1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0, 187510.......................................11
1,1×2-3=((20+2-1)×2-3)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510....................................11
3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10...................................................................11
Example 1.8..................................................................................................................................12
0.5625×2= 1.125 I —, d 1 = 1.......................................................................................................25
0.125×2=0.25 I —• d 2 = 0...........................................................................................................25
0.25×2=0.5 I —, d 3 = 0................................................................................................................25
0.5×2= 1........................................................................................................................................25
19,562510≡ 10011,10012.............................................................................................................25
10011,10012= 1,00111001 ×24.....................................................................................................25
410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 = 410 + 102310 = 102710 ≡
100000000112..............................................................................................................................26
0011100100000000000000000000000000000000000000000000..............................................26
Example 1.8 (Continued)..............................................................................................................27
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A 00000000000 HEX...........................27
Example 1.9..................................................................................................................................27
Example 1.9 (Continued)..............................................................................................................28
-0, 10111 × 2-126.............................................................................................................................28
-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)......................................................................28
= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)........................................................................28
≈ [-0.71875 × 1.1754943508222875079687365372222](10)......................................................28
≈ -8.44886564653519146352938612849 × 10-39..........................................................................28
[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )...........................................................................29
[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )................................................................29
Department of Basic Sciences
14 Errors and Floating Point Representation

Solution of Equations No............................................................................................................................19


Linear...........................................................................................................................................................19
Theorem 2.1......................................................................................................................................19
Example 2.1..................................................................................................................................17
f (0) = -1 < 0..................................................................................................................................17
f (1)=1>0.......................................................................................................................................17
Methods for Solving Nonlinear Equations in One Variable................................................................17
2.2.1 Bisection Method...................................................................................................................17
Example 2.2..................................................................................................................................18
x 1 = 1 x2 = 2.............................................................................................................................18
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578............................................................................................18
f ( x3 ) = f ( 1.5 ) = -0.1823...........................................................................................................18
Example 2.2 (Continued)..............................................................................................................19
× 100=...........................................................................................................................................19
1.5-1..............................................................................................................................................19
1.5.................................................................................................................................................19
× 100 = 33.33%.............................................................................................................................19
x1 + x3 1 + 1.5..........................................................................................................................19
= 1.25............................................................................................................................................19
f ( x4 ) = f ( 1.25 ) = 0.0634...........................................................................................................19
× 100=...........................................................................................................................................19
1.5 - 1.25.......................................................................................................................................19
1.5.................................................................................................................................................19
× 100= 16.66%..............................................................................................................................19
x 4 + x 3 1.25 + 1.5....................................................................................................................19
= 1.375..........................................................................................................................................19
f ( x5 ) = f ( 1.375 ) = -0.0656.......................................................................................................19
2.2.2 False Position Method...........................................................................................................20
f ( xb ) -f ( xa ).................................................................................................................................20
x1 = 0............................................................................................................................................21
x2 = 1............................................................................................................................................21
f ( x1 ) = -1.....................................................................................................................................21
f ( x2 ) = 0.7854...........................................................................................................................21
x 3= x 1 -
f ( x 2 ) - f ( x 1 ) = 0...........................................................................................................21
f (0)(1-0) f (1)- f (0) = 0.................................................................................................................21
Department of Basic Sciences
1
5

(-1)(1 - 0)......................................................................................................................................21
0.7854 - (-1) 1.7854...................................................................................................................21
= 0.5601........................................................................................................................................21
f ( x3 ) = f ( 0.5601 ) = 0.0707.......................................................................................................21
× 100=...........................................................................................................................................21
0.5601 - 1......................................................................................................................................21
0.5601...........................................................................................................................................21
× 100 = 78.54%.............................................................................................................................21
(-1)(0.5601 - 0) 0.5601...............................................................................................................21
0.0707 - (-1) = 1.07070=0.5231......................................................................................................21
f ( x4 ) = f ( 0.5231 ) = 0.0051.......................................................................................................21
Example 2.3 (Continued)..................................................................................................................22
Definition 2.3....................................................................................................................................22
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )..........................................................................................................24
2.2.4 Newton-Raphson method..................................................................................................26
f 0 ( x ) =........................................................................................................................................26
f ( x i ) - 0 x i - x i + 1....................................................................................................................26
f 0 ( x i )..........................................................................................................................................26
g ( x ) = x -.....................................................................................................................................26
f ( x ) f0 ( x )...................................................................................................................................26
Example 2.5..................................................................................................................................27
e - x i + x i - 2..................................................................................................................................27
- e - x i + 1.......................................................................................................................................27
e - x 0 + x 0.....................................................................................................................................27
-2...................................................................................................................................................27
- e - x 0 + 1......................................................................................................................................27
1-...................................................................................................................................................27
e -(1) +(1)-2.....................................................................................................................................27
e +( )
= 1.843482357250334....................................................................................................27
-(1)................................................................................................................................................27
e - x 1 + x 1 - 2................................................................................................................................27
- e - x 1 + 1.....................................................................................................................................27
1-...................................................................................................................................................27
e -(1.843482357250334) + (1.843482357250334) - 2.................................................................................27
= 1.841406066157926..................................................................................................................27
Department of Basic Sciences
16 Errors and Floating Point Representation

e - x 2 + x 2 - 2................................................................................................................................27
- e - x 2 + 1.....................................................................................................................................27
1.841405660436976.....................................................................................................................27
e - x 3 + x 3 - 2................................................................................................................................27
- e - x 3 + 1.....................................................................................................................................27
1.841405660436961.....................................................................................................................27
e - x 4 + x 4 - 2................................................................................................................................27
- e - x 4 + 1......................................................................................................................................27
1.841405660436961.....................................................................................................................27
2.2.5 Secant Method...................................................................................................................27
f ( x2 ) -f ( x1 )...............................................................................................................................27
f 0 ( x i )..........................................................................................................................................28
f 0 ( x i )..........................................................................................................................................28
xi +1= xi -
f ( x i ) - f ( x i - 1 )............................................................................................................28
Example 2.6..................................................................................................................................29
Yo
f ( x i )( x i - x i - 1 ) = x - f ( x i )- f ( x i - 1 ) ........................................................................29
x i
+ x i -2)( x i - x i - 1 ).................................................................................................................29
( e - x i + x i -2) - ( e - x i..................................................................................................................29
1 + x i - 1 - 2)................................................................................................................................29
( e -(1) +(1)-2)(1-0).........................................................................................................................29
( e -(1) +(1)-2)-( e -(0) +(0)-2)...........................................................................................................29
= 2.718281828459045..................................................................................................................29
Interpolation and curve fitting.....................................................................................................................33
f ( x k ) = y k , k = 1,..., n...............................................................................................................33
3.1 NEWTON'S INTERPOLATING POLYNOMIAL......................................................................................33
P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · + b n ( x - x 0 )( x - x 1 ) · · · ( x - x
n - 1 ).............................................................................................................................................33
P n ( x ) = b 0 +∑ b i ∏( x - x j )................................................................................................33
f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ] x i + j + 1 - x i.....................................................31
P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]( x - x 0 )( x - x 1 ) +·· · + f [ x 0 ,
x 1 ,..., x n ]( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )..........................................................................31
Example 3.1..................................................................................................................................31
Level b 1 :.....................................................................................................................................31
2-9 -7 (-1)-2 -3 (0 - (-1) 1.........................................................................................................31
5-(-3)= 8 7-5 = 2 8-7 = 1..............................................................................................................31
Department of Basic Sciences
1
7

3.2............................................................................................................................................................33
Lagrange Interpolating Polynomial......................................................................................................33
Example 3.2..................................................................................................................................33
P ( x ).............................................................................................................................................33
9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0 L 3 ( x )......................................................................33
,
......................................................................................................................................................33
Example 3.2 (Continued)..............................................................................................................34
L 0 ( x ) = ∏ x - xk........................................................................................................................34
-3-5 -3-7 -3-8 x -5 x -7 x -8...........................................................................................................34
=
-8 .-10.-11...............................................................................................................................34
-x3 x 2 131x7.............................................................................................................................34
=
880 + 44 - 880 + 22..................................................................................................................34
L1 ( x ) = ∏ x - xk.........................................................................................................................34
x - x 0 x - x 2 x - x 3.........................................................................................................................34
= ...............................................................................................................................................34
x - (-3) x -7 x - 8............................................................................................................................34
=
5-(-3).5-7.5-8...............................................................................................................................34
x +3 x -7 x -8.................................................................................................................................34
=
8 . -2 . -3.................................................................................................................................34
x3 x 2 11x7...............................................................................................................................34
=
48 + 4 - 48 + 2...............................................................................................................................34
L2 ( x ) = ∏ x - xk.........................................................................................................................34
x - (-3) x - 5 x - 8...........................................................................................................................34
=
7-(-3).7-5.7-8...............................................................................................................................34
x +3 x -5 x -8.................................................................................................................................34
=
10 . 2 . -1.................................................................................................................................34
-x3 x 2 x 6..................................................................................................................................34
=
20 + 2 - 20 -.............................................................................................................................34
Example 3.2 (Continued)..............................................................................................................35
Segmental Interpolation: Cubic Tracers...............................................................................................35
x 0 < x 1 < . . . < xn..........................................................................................................................35
( k )................................................................................................................................................35
p 3 ( x ) ≡ p k ( x ) = a k + b k ( x − x k ) + c k ( x − x k ) 2 + d k ( x − x k ) 3.................................35
Example 3.3..................................................................................................................................37
Example 3.3 (Continued)..............................................................................................................39
Department of Basic Sciences
18 Errors and Floating Point Representation

2k1 + 0.6k2 = -8.35.....................................................................................................................39


0.6k1 + 1.6k2 + 0.2k3 = 1.9.......................................................................................................39
0.2k2 + 1.2k3 + 0.4k4 = 4.8.......................................................................................................39
0.4k3 + 1.66k4 = 1.84...................................................................................................................39
k 1 = -4, 9588................................................................................................................................39
k 2 = 2, 6128.................................................................................................................................39
k 3 = 3, 4740.................................................................................................................................39
k4 = 0.2713...................................................................................................................................39
Example 3.3 (Continued)..............................................................................................................42
Curve Fitting by Least Squares............................................................................................................42
3.4.1 Least Squares and Linear Regression Analysis..........................................................................43
Criteria:.........................................................................................................................................59
Example 3.4..................................................................................................................................59
A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = 16 ,.............................................................59
b =.................................................................................................................................................59
5(260) - (16)(47)...........................................................................................................................59
5(106) - (16)2.................................................................................................................................59
(106)(47) - (206)(16)....................................................................................................................59
5(106) - (16)2.................................................................................................................................59
a =2...............................................................................................................................................59
b =3...............................................................................................................................................59
Example 3.5..................................................................................................................................60
A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D =∑ and i = 43.5........................60
b =.................................................................................................................................................60
8(546.63) - 89.8(43.5)...................................................................................................................60
8(1292.92) - (89.8)2 =⇒
...............................................................................................................60
(1292.92)(43.5) - 596.63(89.8).....................................................................................................60
8(1292.92) - (89.8)2.......................................................................................................................60
a = 0.20477...................................................................................................................................60
b = 3.1389.....................................................................................................................................60
3.4.2 Linearization of Nonlinear Relationships...................................................................................60
3.4.2.1 Model Power..................................................................................................................60
y = α x β.........................................................................................................................................60
| {z} |{z} | {z}.........................................................................................................................60
Example 3.6 (Continued)..............................................................................................................62
Department of Basic Sciences
1
9

A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C =∑ x i =................................................................62


a =.................................................................................................................................................62
b =.................................................................................................................................................62
5(1.9209) - (-2.0756)(-1.7657).....................................................................................................62
5(1.4556) - (-2.0756)2 =
.............................................................................................................62
(1.4556)(-1.7657) - (1.9209)(-2.0756)..........................................................................................62
5(1.4556) - (-2.0756)2...................................................................................................................62
a = 1.999947.................................................................................................................................62
b = 0.477078.................................................................................................................................62
Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997................................................................62
β = 1.999947.................................................................................................................................62
y = 2.9963 x 1.999947.........................................................................................................................62
Numerical Differentiation and Integration..................................................................................................52
Numerical Differentiation....................................................................................................................52
4.1.1 Accuracy Formulas for the First Derivative................................................................................52
Three Point Formulas...........................................................................................................................52
. Forward:......................................................................................................................................52
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 ).................................................................52
. Backward:...................................................................................................................................52
f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 ) 2 hours..........................................................52
Five Point Formulas.............................................................................................................................50
. Forward:......................................................................................................................................50
-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f ( x i +4 h ).................................50
. Central:........................................................................................................................................50
f 0 ( x i ) =......................................................................................................................................50
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2 h ).................................................................50
+ O ( h4 ).......................................................................................................................................50
Example 4.1..................................................................................................................................50
. Three Point Formula: If h = 0.1 we have to..............................................................................50
-3 f (2)+4 f (2+0.1)- f (2+2(0.1))...................................................................................................50
2 ∗ (0.1)........................................................................................................................................50
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404..................................................................50
0.2.................................................................................................................................................50
-0.417756085................................................................................................................................50
. Five Point Formula: If h = 0.1 we have to................................................................................50
Department of Basic Sciences
20 Errors and Floating Point Representation

-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f (2+4(0.1))..........................................50


12 ∗ (0.1)......................................................................................................................................50
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 + 16 × 0.745705212 - 3 ×
0.675463180.................................................................................................................................50
1.2.................................................................................................................................................50
-0.416135615................................................................................................................................50
Clearly f 0 (2) = cos(2)...............................................................................................................50
-0.416146837 - (-0.416135615)....................................................................................................50
-0.416146837................................................................................................................................50
-0.416146837 - (-0.417756085)....................................................................................................50
-0.416146837................................................................................................................................50
× 100 ≈ 0.38% and.......................................................................................................................50
4.2............................................................................................................................................................51
Numerical Integration..........................................................................................................................51
4.2.1 Newton-Cotes formulas.........................................................................................................51
simple trapezoid rule.....................................................................................................................51
P ( x ) = f ( a ) + f ( b ) - f ( a ) ( x - a ) b - a......................................................................................52
Zbf (
x ) to.......................................................................................................................................52
f ( b ) -f ( a ) b - a.........................................................................................................................52
/(b)-/(a) (xa)21 b...........................................................................................................................52
b - a 2............................................................................................................................................52
( b - a ) f ( a )+ f ( b )........................................................................................................................52
Example 4.2..................................................................................................................................52
2....................................................................................................................................................52
π
2 + 6π...........................................................................................................................................52
4....................................................................................................................................................52
π....................................................................................................................................................52
π
= 0.5235988................................................................................................................................52
6....................................................................................................................................................52
5π-6 3............................................................................................................................................52
12..................................................................................................................................................52
0.4429715.....................................................................................................................................52
0.4429715 - 0.5235988.................................................................................................................52
0.4429715.....................................................................................................................................52
Composite Trapezoid Rule...........................................................................................................53
Department of Basic Sciences
2
1

f ( x ) dx + f ( x ) dx +. . . + f ( x ) dx..........................................................................................53
x0 x 1 x n -1............................................................................................................................53
I = h f ( x 0 )+2 f ( x 1 ) + h f ( x 1 )+2 f ( x 2 ) +...+ h.........................................................................53
f ( xn − 1 ) + f (xn ).......................................................................................................................53
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n )................................................................................................53
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )........................................................................................................53
i =1
I=(b-a) .........................................................................................................................53
2 n

Et =...............................................................................................................................................53
( b - a ) 3 12 n 3..............................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................54
f 00 ' i =1...........................................................................................................................................54
Example 4.3..................................................................................................................................54
2....................................................................................................................................................54
b - a 1............................................................................................................................................54
2
= 0.1............................................................................................................................................54
x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1].......................................................................................................54
0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]........................................................54
0.4513161.....................................................................................................................................54
E r =..............................................................................................................................................54
0.4429715 - 0.4513161.................................................................................................................54
0.4429715.....................................................................................................................................54
· 100 ≈ 1.884%..............................................................................................................................54
Differential equations..................................................................................................................................58
Ordinary.......................................................................................................................................................58
5.1 EULER METHOD..........................................................................................................................58
dy = f ( x , y ), y (0) = x 0..............................................................................................................58
f ( xn , yn ) =..................................................................................................................................56
( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )..........................................................................................56
Example 5.1..................................................................................................................................56
1. Analytical Solution................................................................................................................56
Z dy
= Z 2 xdx.....................................................................................................................................56
Ln | and | = x 2 + C........................................................................................................................56
Example 5.1 (Continued)..............................................................................................................57
Ln |1| =0+ C =⇒ C =0...................................................................................................................57
Department of Basic Sciences
22 Errors and Floating Point Representation

Ln | and | = x 2 =⇒ y = e x 2............................................................................................................57
2. Numerical Solution................................................................................................................57
x 1 = x 0 + h =0+0.1 =0.1.............................................................................................................57
y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which means ( x 1 , y 1 ) = (0.1, 1)......57
x 2 = x 1 + h = 0.1 +0.1 =0.2........................................................................................................57
y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2 )=(0.2,1.02)..............................57
0.3 1.0608................................................................................................................................71
0.4 1.124448............................................................................................................................71
0.5 1.21440384........................................................................................................................71
0.6 1.33584422........................................................................................................................71
0.7 1.49614553........................................................................................................................71
0.8 1.70560591........................................................................................................................71
0.9 1.97850285........................................................................................................................71
1 2.33463336............................................................................................................................71
3. Approximate y (0.5)...............................................................................................................71
Example 5.1 (Continued)..............................................................................................................72
1.28403 - 1.21440.........................................................................................................................72
1.28403.........................................................................................................................................72
4. Graph and comparison of results...........................................................................................72
Fourth Order Runge-Kutta Method......................................................................................................72
d y
dx = f ( x , y ), y (0) = x 0...........................................................................................................72
k 2 = f ( x n + h 2 , y n + h 2 k 1 ) k 3 = f ( x n + h 2 , y n + h 2 k 2 ) k 4 = f ( x n + h , y n + hk 3 )......73
6....................................................................................................................................................73
Example 5.2..................................................................................................................................73
1. Analytical Solution................................................................................................................73
Example 5.2 (Continued)..............................................................................................................75
3. Approximate y(0.5)...............................................................................................................75
1.28403 - 1.28402.........................................................................................................................75
1.28403.........................................................................................................................................75
· 100 ≈ 0.000779%........................................................................................................................75
4. Graph and comparison of results..............................................................................................75
Introduction to Matlab.................................................................................................................................65
Matlab Environment Overview............................................................................................................65
. Save, Recall and Exit a work session.............................................................................................65
. Current Work Directory.................................................................................................................65
Department of Basic Sciences
2
3

. Delete Work Session Variables......................................................................................................63


. Clean screen...................................................................................................................................63
. Aids................................................................................................................................................63
. Variables in use..............................................................................................................................63
Basic Constants and Operators.............................................................................................................63
a) Constants..........................................................................................................................................63
b) Operations Between Scalars............................................................................................................64
f) Useful Functions...............................................................................................................................64
A.3............................................................................................................................................................65
Elemental Functions.............................................................................................................................65
A.4............................................................................................................................................................67
Matrices and Vectors............................................................................................................................67
a) Matrices............................................................................................................................................67
1. Create a Matrix..........................................................................................................................67
Example A.1.....................................................................................................................................67
42 44 70........................................................................................................................................67
50 51.............................................................................................................................................67
2. Modify Elements of an Array...................................................................................................67
Example A.2.....................................................................................................................................68
3. Add or Delete Rows or Columns from a Matrix.......................................................................68
Example A.3.....................................................................................................................................68
Example A.4.....................................................................................................................................68
Example A.5.....................................................................................................................................69
Example A.6.....................................................................................................................................69
b) Vectors..........................................................................................................................................69
1. Vector Creation.........................................................................................................................69
Example A.7.....................................................................................................................................70
Example A.8.....................................................................................................................................70
2. Product of a Vector and a Scalar...............................................................................................70
Example A.9.....................................................................................................................................70
3. Product point.............................................................................................................................71
Example A.10...................................................................................................................................71
4. Cross Product............................................................................................................................71
Example A.11...................................................................................................................................71
c) Basic Operations Between Matrices and Between Vectors..........................................................71
Department of Basic Sciences
24 Errors and Floating Point Representation

Graph of Functions in 2D.....................................................................................................................72


a) In Rectangular or Cartesian Coordinates.........................................................................................72
Graph of a Simple Function.................................................................................................................72
Example A.12...................................................................................................................................72
Example A.12 (Continued)...............................................................................................................73
2. Second Option: Using the function fplot('Function',Limits,[line specifiers]) .......................73
3. Third Option: Using the function ezplot('Function',Limits,[line specifiers]) ........................73
Graph of Several Functions on the Same Plane...................................................................................73
. First Option: Use The plot() Function ........................................................................................73
Example A.13...................................................................................................................................74
Example A.14...................................................................................................................................74
Graph of a Piecewise Function............................................................................................................74
Example A.15...................................................................................................................................75
b) In Polar Coordinates........................................................................................................................75
Example A.16...................................................................................................................................76
A.6............................................................................................................................................................76
Symbolic Calculation...........................................................................................................................76
a) Algebraic Expressions......................................................................................................................76
Useful Functions About Algebraic Expression....................................................................................76
Example A.17...................................................................................................................................77
Evaluate a Symbolic Expression..........................................................................................................77
Example A.18...................................................................................................................................77
b) Solution of Polynomial Equations and Systems of Linear Equations.............................................78
Example A.19...................................................................................................................................78
Example A.20...................................................................................................................................78
c) Limit Calculation..........................................................................................................................79
Example A.21...................................................................................................................................79
3. Lim........................................................................................................................................79
-4...................................................................................................................................................79
4. lim e - x...................................................................................................................................79
1....................................................................................................................................................79
d) Derivative Calculus.......................................................................................................................79
Example A.22...................................................................................................................................79
d) Calculus of Integrals........................................................................................................................80
Example A.23...................................................................................................................................80
Department of Basic Sciences
2
5

A.7............................................................................................................................................................80
Programming in Matlab.......................................................................................................................80
The Function and Script Editor............................................................................................................80
How do we access the editor?..........................................................................................................81
How do you run a script?..................................................................................................................81
How is a function executed?.............................................................................................................81
How do you program in Matlab?......................................................................................................81
a) IF Control Statement.....................................................................................................................82
b) SWITCH Control Statement.........................................................................................................83
c) FOR Repetition Sentence..............................................................................................................83
d) WHILE Repetition Sentence........................................................................................................83
e) Creation of Functions and Scripts.................................................................................................84
. Features..............................................................................................................................................84
Example A.24...................................................................................................................................84
Example A.25...................................................................................................................................85
Example A.26...................................................................................................................................85
Example A.27...................................................................................................................................86
. Scripts.................................................................................................................................................86
Example A.28...................................................................................................................................87
Example A.29...................................................................................................................................87
Bibliography................................................................................................................................................90

and then the fractional part:

0.5625×2= 1.125 I —, d 1 = 1
0.125×2=0.25 I —• d 2 = 0
0.25×2=0.5 I —, d 3 = 0
0.5×2= 1
/ —, d 2 = 1
So that,

19,562510≡ 10011,10012

2. Normalize the binary number obtained, placing the decimal point to the right of the most significant bit. ficative:

10011,10012= 1,00111001 ×24


3. Write the exponent in excess of 2 n -1 - 1 :

410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 = 410 + 102310 = 102710 ≡ 100000000112
4. Set the mantissa using implicit bit. To do this, the eight bits to the right of the comma (00111001) are taken

Department of Basic Sciences


26 Errors and Floating Point Representation

and the rest of the mantissa is filled with zeros:

0011100100000000000000000000000000000000000000000000

Numerical analysis

Department of Basic Sciences


2
7

Example 1.8 (Continued).

5. Express the number in the IEEE 754 standard with double precision. In this case, it must be taken into account that the
sign bit is equal to 0, since the number is positive:

63 62 · · · 52 51 ··· 0
0 10000000011 0011100100000000000000000000000000000000000000000000
Sign Exponent Mantissa

6. Represent the number in hexadecimal:


0100 0000 0011 0011 1001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
4 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0

So that,
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A 00000000000 HEX

In both double precision and single precision, there are some special cases that depend on the values of the sign, the exponent
and the mantissa:

Sign(s) Exponent(e) Mantissa(m) Meaning


Positive (0) All ones (11...1) All zeros (00...0) Plus Infinity (+∞)
Negative (1) All ones (11...1) All zeros (00...0) Minus Infinity (-∞)
0ó1 All ones (11...1) Different from all zeros It is not a number (Not a Number, NaN)
0ó1 All zeros (00...0) All zeros (00...0) Represents Zero (0)
0ó1 All zeros (00...0) Different from all zeros Very small number close to zero (0)

The last two cases deserve special attention, since, when all the bits of the exponent are zeros (00...0) , this means that no
implicit bit is being used. If, in addition, the mantissa is all zeros (00...0) , the number represented is zero (0), but if the mantissa
is different from all zeros, the number being represented is very small, so that, the exponent will be worth -126 or -1022 ,
depending on whether the number is written in single or double precision, respectively.

Example 1.9

Given the number 805 C 0000 HEX of the IEEE 754 standard with single precision, exponent in excess of 2 n -1 - 1 and
mantissa in Sign Magnitude with implicit bit, to find out what number it represents in base 10, the following steps can be
performed :

1. Convert 805 C 0000 HEX to base 2:

8 0 5 c 0 0 0 0
1000 0000 0101 1100 0000 0000 0000 0000

Numerical analysis

Department of Basic Sciences


28 Errors and Floating Point Representation

Example 1.9 (Continued).

2. Get the bits of the sign, the mantissa and the exponent:

31 30 · · · 23 22 ··· 0
1 00000000 10111000000000000000000
Sign Exponent Mantissa

3. By observing that all the bits of the exponent are zeros (00000000) and the mantissa is not all zeros, it follows
that it is a special case. It is represented to a very small number without im bit implicit and therefore the exponent
is -126. 4. In exponential notation, since no implicit bit is used in this case, the mantissa is written with a zero (0),
followed by the decimal point (,) and the bits of the mantissa (10111000000000000000000). As for the sign of the
number, it is negative, since the sign bit is 1. With all this, the number is:

-0, 10111 × 2-126

5. Express the number in base 10:

-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)


= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)
≈ [-0.71875 × 1.1754943508222875079687365372222](10)
≈ -8.44886564653519146352938612849 × 10-39

The following two tables summarize the calculations that must be performed to deduce the base 10 value of an
integer written in the IEEE 754 standard with single or double precision.

Sign(s) Exponent(e) Mantissa(m) Meaning


0ó1 0< e <255 Indifferent (-1) s · 1. m · 2 e -127
0 e =255 m =0 (+∞)
1 e =255 m =0 (-∞)
0ó1 e =255 m 6= 0 NaN
0ó1 e =0 m =0 0
0ó1 e =0 m 6= 0 (-1) s · 0. m · 2 -126
Calculation of the base 10 value of a real number written in the IEEE 754 standard with single precision.

Numerical analysis

Department of Basic Sciences


2
9

Sign(s) Exponent(e) Mantissa(m) Meaning


0ó1 0< e <2047 Indifferent (-1) s · 1. m · 2 e -1023
0 e =2047 m =0 (+∞)
1 e =2047 m =0 (-∞)
0ó1 e =2047 m 6= 0 NaN
0ó1 e =0 m =0 0
0ó1 e =0 m 6= 0 (-1) · 0. m · 2 -1022
s

Calculation of the base 10 value of a real number written in the IEEE 754 standard with double precision.

The representation ranges in the IEEE 754 standard with single and double precision, exponent in Excess to 2 n -1 - 1 and
mantissa in Sign Magnitude with implicit bit, are the following:

1. Representation range in the IEEE 754 standard with single precision:

[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )

2. Representation range in the IEEE 754 standard with double precision:

[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )

Both representation ranges are discontinuous, that is, not all real numbers that exist between any two of them can be
represented. This is because between two real numbers There are always infinite numbers, however, only a certain number of
bits are available to represent real numbers. For this reason, not all real numbers can be represented on digital computers. For
-126
example, with single precision, around the number zero (0) there are infinitely many real numbers, greater than -2 and less
-126
than 2 , which are not representable. Gra physically:

2-126 0 2-126

Non-Representable Numbers

Numerical analysis

Department of Basic Sciences


Solution of Equations No
Linear

Introduction to Solving Nonlinear Equations in One Variable

Definition 2.1
Let f(x), be a given function. A real number a is said to be a root of the equation f(x) = 0, or a zero of the function f(x) if f(a)
= 0.

Definition 2.2
Given an equation f(x) = 0 . A number a is said to be a root of multiplicity m (m a positive integer) of the equation f(x) = 0, if
f(a) = 0, and for

x # a, f(x) = (x — ay n h(x), with lim h(x) # 0

If m = 1, the root is said to be simple.

Theorem 2.1
Suppose that the function f(x) has its first two continuous derivatives on an interval [a,b] containing a number a. Then a is a
simple root of the equation f(x) = 0 if and only if f(a) = 0 and f'(0)/0.

Bolzano's theorem has an interesting application in locating the roots or zeros of a continuous function.

Theorem 2.2 (Bolzano)


If a function f(x) is continuous on a closed interval [a,b] and f(a) and f(b) are of different signs, then there exists at least one
point between a and b for which /(c ) = 0.

Numerical analysis

Department of Basic Sciences


1
7

Geometrically, the theorem states that if two points ( a , f ( a )) and ( b , f ( b )) on the graph of a continuous function are located
on different sides of the x- axis, then the graph intersects the axis at some point between a and b . Of course there can be
several intersections.

Example 2.1

Check that the equation x 3 + x - 1 = 0 has at least one real solution in the interval [0, 1] .

We consider the function f ( x ) = x 3 + x - 1 , which is continuous in [0, 1] because it is polynomial. We study the sign at the
ends of the interval:

f (0) = -1 < 0
f (1)=1>0

Since the signs are different, the Bolzano theorem is fulfilled, therefore there exists a c belongs (0, 1) such that f ( c ) = 0 .
Which shows that it has a solution in that interval.

2.2 Methods for Solving Nonlinear Equations in One Variable

2.2.1 Bisection Method


We consider an interval [ a , b ] where the function f ( x ) changes sign, that is, f ( a ) · f ( b ) < 0 . The method consists of dividing
the interval [ a , b ] in half as follows:

a + b
The midpoint is taken 2 . If f ( a + 2 b ) = 0 we have already found the root x = a + 2 b . Otherwise, if f ( a + 2 b ) · f ( b ) < 0 then we
set a = a + 2 b and subdivide the new interval [ a , b ] again. Yes, for the with Otherwise, f ( a ) · f ( a + 2 b ) < 0 , then we set b = a + 2
b
and start again. The successive subdivisions of the interval [ a , b ] approximate the root.

Numerical analysis

Department of Basic Sciences


18 Solution of Nonlinear Equations

Graphic Representation of the Bisection Method

Example 2.2

Approximate the root of f ( x ) = e x - ln ( x ) by 3 iterations of the bisection method. Calculate the absolute
percentage relative error in each iteration.

First we assume that it has been shown that f ( x ) is continuous at (0, +∞) . Now look for x 1 and x 2 such that f ( x 1
) · f ( x 2 ) < 0 , to have the guarantee that a root exists in the interval [ x 1 , x 2 ] .

Sean
x1=1 x2 = 2
Evaluate yourself in function
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578

It is clearly verified that f ( x 1 ) · f ( x 2 ) < 0 (or that there is a change of sign), which indicates that there is a root in
the interval [1,2].

Iteration 1: Calculate the error


x2-x1 2 -1
E rpa — x 100 — 2 x 100 — 50%
x2

Calculate the midpoint


x1 + x2 1+2
x3 2 2 1.5
Evaluate said point in the function to verify if it is a root

f ( x3 ) = f ( 1.5 ) = -0.1823

Numerical analysis

Department of Basic Sciences


1
9
Example 2.2 (Continued).

It is evident that the midpoint is not a root. Therefore we must verify in which subinterval the root is located. To determine it, you
can choose any of the two subintervals generated by the midpoint and perform said verification on it. If there is no root in it, it will
automatically be found in the other subinterval (this is guaranteed by the Bolzano theorem). That is to say,

f ( x 1 ) · f ( x 3 ) = f (1) · f (1.5) = (0.3679) · (-0.1823) < 0 , indicating that there is a root in the subinterval [1,1.5].

x3-x1
E=
r pa = x3

1.5-1
× 100=
1.5

Iteration 2: Calculate the error


× 100 = 33.33%

Calculate the midpoint


x1 + x3 1 + 1.5
x4= = 1.25
Evaluate said point in the function to verify if it is a root

f ( x4 ) = f ( 1.25 ) = 0.0634

It is evident that the midpoint is not a root. Therefore we must verify in which subinterval the root is located. To determine it, you
can choose any of the two subintervals generated by the midpoint and perform said verification on it. In case there is no root in
it, it will automatically It will normally be found in the other subinterval. That is to say,

f ( x 4 ) · f ( x 3 ) = f (1.25) · f (1.5) = (0.0634) · (-0.1823) < 0 , indicating that there is a root in the subinterval [1.25,1.5].

Iteration 3: Calculate the error


x4-x3 1.5 - 1.25
E rpa = × 100= × 100= 16.66%
x4 1.5

Calculate the midpoint


x4+x3 1.25 + 1.5
x5= = 1.375
Evaluate said point in the function to verify if it is the root

f ( x5 ) = f ( 1.375 ) = -0.0656

It is evident that the midpoint is not a root. Therefore we must verify in which subinterval the root is located. To determine it, you
can choose any of the two subintervals generated by the midpoint and perform said verification on it. In case there is no root in
it, it will automatically It will normally be found in the other subinterval. That is to say,

f ( x 4 ) · f ( x 5 ) = f (1.25) · f (1.375) = (0.0634) · (-0.0656) < 0 , indicating that there is a root in the subinterval [1.25,1.375].

Department of Basic Sciences


20 Solution of Nonlinear Equations
After applying the 3 iterations the approximate root is x 5 = 1.375 .

Numerical analysis

Department of Basic Sciences


2
1
2.2.2 False Position Method
The false position method aims to combine the security of the bisection method to converge, with the speed of the
secant method (see section 2.4); It is also called: False Rule , False Position or Linear Interpolation . Like bisection,
the false Position method relies on the Bolzano Theorem so that the root of a function exists in a given interval. The
method can be described as follows:

It begins with an interval [ x a , x b ] , which encloses the root, that is, f ( x a ) and f ( x b ) are of opposite signs, as
indicated by the Bolzano Theorem . It is similar to the bisection method since it consists of generating subin intervals
that enclose the root; but the approximation of the root x r is not obtained with the midpoint, but with the intersection of
the secant line to the curve that joins the points ( x a , f ( x a )) and ( x b , f ( x b ) with the x axis; providing a better
estimate of the root. Replacing the curve with a straight line gives a false position of the root, hence the name of the
method.

A practical way to implement the method can be:

Being f ( x ) continuous,

1. Find initial values x a and x b such that f ( x a ) and f ( x b ) have opposite signs, that is, f ( x a ) · f ( x b ) < 0 .
f ( x a )( x b - x a )
2. The first approximation to the root is taken equal to: x r = x a -
f ( xb ) -f ( xa )

3. Evaluate f ( xr ) . We must necessarily fall into one of the following cases:

(a) f ( x a ) · f ( x r ) < 0 . In this case, we have that f ( x a ) and f ( x r ) have opposite signs, and therefore the
root lies in the interval [ x a , x r ] .
(b) f ( x a ) · f ( x r ) > 0 . In this case, we have that f ( x a ) and f ( x r ) have the same sign, and hence f ( x a )
and f ( x r ) have opposite signs. Therefore, the root lies in the interval [ x r , x b ] .
(c) f ( x a ) · f ( x r ) = 0 . In this case we have that f ( x r ) = 0 and therefore we have already located the root.

In case of f ( x r ) 6= 0 , the process is repeated with the new interval until a desired error is satisfied.

Numerical analysis

Department of Basic Sciences


2
2

Approximate the root of f ( x ) = t an -1 ( x ) + x - 1 on the interval [0, 1] using 3 iterations of the False Position method. Calculate
Example 2.3
the absolute percentage relative error in each iteration.

First we assume that it has been shown that f ( x ) is continuous on [0, 1] . Now verify that f (0) · f (1) < 0 , to have the guarantee
that a root exists in the interval [0, 1] .

Sean
x1 = 0 x2 = 1
Evaluate yourself in function
f ( x1 ) = -1 f ( x2 ) = 0.7854

It is clearly verified that f ( x 1 ) · f ( x 2 ) < 0 (or that there is a change of sign), which indicates that there is a root in the interval
[0,1].

Iteration 1: Calculate root approximation


f ( x 1 )( x 2 - x 1 ) f (0)(1-0) f (1)- f (-1)(1 - 0)
x 3= x 1 -
f(x2)-f(x1)=0 = 0.5601
(0) = 0 0.7854 - (-1) 1.7854

Evaluate this approximation in the function to verify if it is a root

f ( x3 ) = f ( 0.5601 ) = 0.0707

It is evident that the approximation is not root. Therefore we must verify in which subinterval the root is located. To determine it,
you can choose any of the two subintervals generated by the approximation and perform said verification on it. In case there is
no root in it, it will automatically It will normally be found in the other subinterval (this is guaranteed by Bolzano's theorem). That
is to say,

f ( x 1 ) · f ( x 3 ) = f (0) · f (0.5601) = (-1) · (0.0707) < 0 , indicating that there is a root in the subinterval [0,0.5601].

To calculate the error in the first iteration we choose any of the ends of the interval as an approximation of the root, then
x3-x2 0.5601 - 1
E rpa = × 100= × 100 = 78.54%
x3 0.5601

Iteration 2: Calculate root approximation again

f ( x 1 )( x 3 - x 1 ) x 4= x 1 - f f (0)(0.5601 - 0) = 0 (-1)(0.5601 - 0)0.5601


(x3)-f(x1)=0 f (0.5601)- f (0) = 0 0.0707 - (-1) = 1.07070=0.5231

Evaluate this approximation in the function to verify if it is a


root

f ( x4 ) = f ( 0.5231 ) = 0.0051

Numerical analysis

Department of Basic Sciences


23 Solution of Nonlinear Equations

Example 2.3 (Continued).

It is evident that the approximation is not root. Therefore we must verify in which subinterval the root is located. To
determine it, you can choose any of the two subintervals generated by the approximation and perform said verification
on it. In case there is no root in it, it will automatically It will normally be found in the other subinterval. That is to
say,

f(X1)f(x4) =f(0) f(0.5231) = (— 1) • (0.0051) < 0, which indicates that there is a root in the subinterval [0,0.5231].
To calculate the error in the second iteration we do it with the two approximate roots that have been calculated, then

X4-X3 x 100 = 0.5231-0.5601


E r pa — x 100 = 7.07%
0.5231
x4
Iteration 3: Calculate root approximation again

= 7(XI)(X 4 -XI) 7(O)(O-5231-O) (-l)(0.5231-0) _ 0.5231


5 _1
f(x4)—f(x1) 7(0.5231)-7(0) 0.0051-(-1) 1.0051

Evaluate this approximation in the function to verify if it is a root

f (X5) = f(0.5204) = 0.00023409

It is evident that the approximation is not root. Therefore we must verify in which subinterval the root is located. To
determine it, you can choose any of the two subintervals generated by the approximation and perform said verification
on it. In case there is no root in it, it will automatically It will normally be found in the other subinterval. That is to
say,

= /(0) • 7(0.5204) = ( — 1) • (0.00023409) < 0, indicating that there is a root in the subinterval
[0,0.5204],

To calculate the error in the third iteration, the same idea that was used in calculating the error in the second iteration
is repeated, therefore
X5-X4 0.5204-0.5231
E r pa — x 100 = x 100 = 0.52%
x5 0.5204
After applying the 3 iterations the approximate root is X5 = 0.5204.

2.2.3 Fixed Point Method

Definition 2.3
Let P € R, and g(x) be a function. P is a fixed point of g(x), if and only if, P = g(P).

Numerical analysis

Department of Basic Sciences


2
4

Theorem 2.3 (of the Fixed Point)


If g(x) is a continuous function on [a,b] and g(x) € [a,b] for all x € [a,b], then g(x) has at least one fixed point on [ a,b]. If in
addition, g\x) exists for all x € {a,b') and Ig'(x)I < K < 1 for all x € (a,b) , K constant, then g(^x) has a single fixed point a € [a, b] and
the sequence {Xn}n defined by the iteration formula
Xn=g(X1), n= 1,2,3,...

converges to a (limxn = a) whatever x € [a,b].

The following graphs show some forms of convergence or divergence of the sequence {xn}n, where Xn =g(xn—1), n= 1,2,3,...

is monotonous).

Numerical analysis

Department of Basic Sciences


25

Example 2.4

Divergence. It does not satisfy the Convergence (depending on the starting point). It does not
hypotheses of the Point theorem. satisfy the hypotheses of the Fixed Point theorem.

2.2.3.1 Method Implementation The fixed point method can be implemented as follows:

1. Set f ( x ) = 0 and solve for an x of f ( x ) . Express the clearance in the form x = g ( x ) .

2. Show that g ( x ) is continuous on some closed interval [ a , b ] .

3. Determine an open interval ( c , d ) where it is satisfied

| g 0 ( x )| < 1, ∀ x ∈ ( c , d )

4. Take a x i ∈ ( c , d ) and evaluate successively in the recursive formula

xi+1=g(xi)

until x i +1 is exactly g ( x i ) .

Numerical analysis

Department of Basic Sciences


26 Solution of Nonlinear Equations
Use the fixed point method to determine a root of f ( x ) = e - x + x - 2 .

1. Let f ( x ) = 0 =⇒ e − x + x − 2 = 0 =⇒ x = 2 − e − x =⇒ g ( x ) = 2 − e − x .

2. Since g ( x ) has no indeterminacy for some value of x ∈ R , we conclude that g ( x ) is continuous on all R and therefore,
g ( x ) is continuous on any closed interval.

3. Let g ( x ) = 2 − e − x =⇒ g 0 ( x ) = e − x =⇒ | g 0 ( x )| = | e - x | , as e - x > 0, ∀ x ∈ R =⇒ | g 0 ( x )| = e - x .
Now, we want to find an open interval ( c , d ) such that | g 0 ( x )| < 1 .

Suppose | g 0 ( x )| = ex < 1 =⇒ x <1 =⇒ 1 < e x =⇒ ln (1) < ln ( ex ) =⇒ 0 < x , therefore


| g 0 ( x )| = e - x < 1 for all x ∈ (0, +∞) .

4. Take a x 0 = 1 ∈ (0, +∞) and compute x i + 1 = g ( x i ) successively.

x 1 =2- e x 0 =2- e 1 = 1.804485465847412


x 2 =2- e x 1 = 2 - e 1.804485465847412 = 1.835440893922046
x 3 =2- e x 2 = 2 - e 1.835440893922046 = 1.840456855343537
x 4 = 2 - e 1.840456855343537 = 1.841255113911434
x 5 = 2 - e 1.841255113911434 = 1.841381782812870
x 6 = 2 - e 1.841381782812870 = 1.841401873535727
x 7 = 2 - e 1.841401873535727 = 1.841405059854723
x 8 = 2 - e 1.841405059854723 = 1.841405565187989
x 9 = 2 - e 1.841405565187989 = 1.841405645331012
x 10 = 2 - e 1.841405645331012 = 1.841405658041243
x 11 = 2 - e 1.841405658041243 = 1.841405660057013
x 12 = 2 - e 1.841405660057013 = 1.841405660376703
x 13 = 2 - e 1.841405660376703 = 1.841405660427404
x 14 = 2 - e 1.841405660427404 = 1.841405660435445
x 15 = 2 - e 1.841405660435445 = 1.841405660436720
x 16 = 2 - e 1.841405660436720 = 1.841405660436922
x 17 = 2 - e 1.841405660436922 = 1.841405660436955
x 18 = 2 - e 1.841405660436955 = 1.841405660436960
x 19 = 2 - e 1.841405660436955 = 1.841405660436960

Since in iterations 18 and 19 the quantities are identical, we conclude that x = 1.841405660436960 is a fixed and unique
point at (0, +∞) for g ( x ) = 2 - e - x , which becomes a root for f ( x ) = e - x + x - 2 .

Numerical analysis

Department of Basic Sciences


27

2.2.4 Newton-Raphson method


Example 2.4 method (also known as the Newton-Raphson method) is an efficient algorithm for finding approximations of
Newton's
the zeros or roots of a real function. It can also be used to find the maximum or minimum of a function, by finding the
zeros of its first derivative.

2.2.4.1 Method Description The idea of this method is as follows:

You start with a value reasonably close to the root (called the initial value x i ), then you draw the tangent to the
function from the point ( x i , f ( x i )) until it intersects the x axis at x i +1 .

Illustration of the Newton-Raphson Method.

The slope of the tangent line is determined by the expression


f(xi)-0
f0(x)
= xi-xi+1

and that after solving for x i + 1 the recursive Newton-Raphson formula is obtained
f ( xi )
xi+=xi
1 f0(xi
)
This x i + 1 will generally be a better approximation to the root of the function.

Then, apply as many iterations as desired.

According to the above formula, it is clearly seen that the Newton-Raphson method is a special case of the Fixed
Point iteration method, when the function is taken as the iteration function

f(x)
g(x)=x- f0
(x)

Numerical analysis

Department of Basic Sciences


28 Solution of Nonlinear Equations

Example 2.5

x
Use the Newton-Raphson method to determine a root of f ( x ) = e initial to x 0 = 1 . + x - 2 . Take as value

x
First calculate the first derivative of the function: f ( x ) = - e recursive: + 1 . Then create the formula

e-xi+xi-2
xi+1= -e-xi+1
xi
Finally apply this formula successively:

e - x 0 + x 0 -2 e -(1) +(1)-2
x1 = x0 1- e +( )
= 1.843482357250334
-e-x0+1 -(1)

e-x1+x1-2 e -(1.843482357250334) + (1.843482357250334) - 2


x2 = x1 -e-x1+1 1- = 1.841406066157926
e - ( 1 . 843482357250334 ) + 1
e-x2+x2-2
x3 = x2 1.841405660436976
-e-x2+1
e-x3+x3-2
x4 = x3 1.841405660436961
-e-x3+1
e-x4+x4-2
x5 = x4 -e-x4+1 1.841405660436961

Therefore the expected root is 1.841405660436961.

When making a comparison between the results of the Fixed Point and Newto-Raphson methods, it can be said that the
Newton-Raphson method is more agile in finding the root of the function than the Fixed Point method.

2.2.5 Secant Method


The Secant Method is a variation of the Newton-Raphson method, where instead of calculating the derivative of the function at
the point of study, keeping in mind the definition of the derivative, it is approximated ima the slope of the line that joins the
function evaluated at the point of study and at the point of the previous iteration. This method is of special interest when the
computational cost of deriving the study function and evaluating it is too high, making Newton's method unattractive.

2.2.5.1 Description of the method Starting with two initial approximations x 0 and x 1 , in order to induce an initial slope f ( x 1) - f ( x
0)
. The approximation x 2 will be the intersection of the line that joins ( x 0 , f ( x 0 )) and x 1 - x 0
f ( x2 ) -f ( x1 )
( x 1 , f ( x 1 )) with the x axis. Now we have the slope line . The x 3 approximation will be the inter
section of the line that joins ( x 1 , f ( x 1 )) and ( x 2 , f ( x 2 )) with the x axis.

Then, apply as many iterations as desired.

Numerical analysis

Department of Basic Sciences


2
9

Ultimately what is done is to replace the derivative that appears in the Newton-Raphson recursive formula with an
approximation of it, that is, assuming that

f(xi)-f(xi-
f0(xi
) 1 )
xi-xi-1

and replace it in

f ( xi )
xi+=xi
1 f0(xi
)

to obtain the Recursive formula of the Secant

f ( x i )( x i - x i - 1 )
xi +1= xi -
f(xi)-f(xi-1)

Numerical analysis

Department of Basic Sciences


30 Solution of Nonlinear Equations

Example 2.6

x
Use the Secant method to determine a root of f ( x ) = e initials x 0 = 0 and x 1 = + x - 2 . Take as values
1.

First we build the Recursive Formula of the Secant Method:


x i
f ( x i )( x i - x i - 1 ) = - f x ( + x i -2)( x i - x i - 1 )
xi+1= Yo - x an
xi
( x i )- f ( x i - 1 ) ( e i + x i -2) - ( e - x i 1 + x i - 1 - 2)

And then successively apply said formula:


( e - x 1 + x 1 - 2)( x 1 - x 0 ) ( e -(1) +(1)-2)(1-0)
x2=x1- 1- -(1)
= 2.718281828459045
(e - x 1
+ x 1 - 2) - ( e - x 0 + x 0 - 2) (e +(1)-2)-( e -(0) +(0)-2)
( e - x 2 + x 2 - 2)( x - x 1 )
x3=x2- 2 1.766851605243141
( e - x 2 + x 2 -2) - ( e - x + x 1 - 2)
1 ( e - x 3 + x 3 - 2)( x
x4=x3- 3
-x2) 1.836845784497266
( e - x 3 + x 3 -2) - ( e - x + x 2 - 2)
2 ( e - x 4 + x 4 - 2)( x
x5=x4- 4
-x3) 1.841438813679357
( e - x 4 + x 4 - 2) - ( e - x 3 + x 3 - 2)
( e - x 5 + x 5 - 2)( x - x 4 )
x6=x5- 5 1.841405646162081
( e - x 5 + x 5 - 2) - ( e - x 4 + x 4 - 2)
( e - x 6 + x 6 - 2)( x - x 5 )
x7=x6- 6 1.841405660436916
( e - x 6 + x 6 - 2) - ( e - x 5 + x 5 - 2)
( e - x 7 + x 7 - 2)( x - x 6 )
x8=x7- 7 1.841405660436961
( e - x 7 + x 7 - 2) - ( e - x 6 + x 6 - 2)
( e - x 8 + x 8 - 2)( x -x 7 )
x9=x8- 8 1.841405660436961
( e - x 8 + x 8 - 2) - ( e - x 7 + x 7 - 2)

Therefore the expected root is 1.841405660436961.

When comparing the Newton-Raphson method with the Secant Method, it can be seen that the Newton-Raphson Method is still
more efficient, and this is because the derivative in Newton's recursive formula was replaced by a approximation of it.

Numerical analysis

Department of Basic Sciences


Interpolation and curve fitting

In the mathematical subfield of numerical analysis, the construction of new points based on knowledge of a discrete set
of points is called interpolation.

In engineering and some sciences it is common to have a certain number of points obtained by sampling or from an
experiment and try to build a function that adjusts them. Another problem closely linked to that of interpolation is the
approximation of a complicated function by a simpler one. If we have a function whose calculation is expensive, we can
start from a certain number of its values and in terpolate said data by constructing a simpler function. In general, of
course, we will not obtain the same values by evaluating the obtained function as if we were evaluating the original
function, although depending on the characteristics of the problem and the interpolation method used, the gain in
efficiency can compensate for the error made. In any case, it is about, from n pairs of points ( x k ,y k ) , obtaining a
function f that verifies

f ( x k ) = y k , k = 1,..., n

which is called the interpolating function of said points. The points x k are called nodes. Some forms of interpolation that
are frequently used are polynomial interpolation, linear interpolation (which is a particular case of the previous one) and
interpolation by means of spline or cubic tracer.

3.1 NEWTON'S INTERPOLATING POLYNOMIAL


This method is algorithmic and is extremely convenient in certain cases, especially when you want We will calculate an
interpolating polynomial of high degree.

For an nth order polynomial , n + 1 points are required: ( x 0 , f ( x 0 )), ( x 1 , f ( x 1 )), ..., ( x n , f ( x n ) ) and to determine
it we use the formula:

P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · + b n ( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )

or what is the same:

n i-1
P n ( x ) = b 0 +∑ b i ∏( x - x j )
i=1 j=1

Numerical analysis

Department of Basic Sciences


3
1

These coefficients are calculated by divided differences , whose general expression is given by:

f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ]
f [ x i , ..., x i + j + 1
]= xi+j+1-xi

As seen in the formula, the split differences are calculated recursively using previous coefficients. Once we have done all the
calculations, we will notice that there are (many) more divided differences than coefficients b i . The calculation of all the
intermediate terms must be done simply because they are necessary to form all the final terms. However, the terms used in the
construction of the interpolating polynomial are all those that involve x 0 , like this:

b 0 = f [ x 0 ] , b 1 = f [ x 0 , x 1 ] ,..., b i = f [ x 0 , x 1 ,..., x i ]

With this notation, we can reexpress the polynomial P n as:

P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]( x - x 0 )( x - x 1 ) +·· · + f [ x 0 , x 1 ,..., x n ]( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )

This equation is known as Newton's interpolating divided difference formula.

Example 3.1

Determine the Newton Interpolant polynomial containing the points (3,-1), (5,-3), (7,9) and (8,6).

The main idea is to complete the following table:

For ease of understanding the Newton interpolating polynomial algorithm using divided differences, let's do the construction by
levels or columns:

Level b 1 :

2-9 -7 (-1)-2 -3 (0 - (-1) 1


5-(-3)= 8 7-5 = 2 8-7 =
1

Numerical analysis

Department of Basic Sciences


32 Interpolation and curve fitting

Example 3.1 (Continued).

Level b 2 : Level b 3 :
-1
86 43
-1 16 96 = 48 = 43
7-(-3) 10 16 8-(-3) 11 11 528

1-

8-5

Therefore the data table is complete as follows:

xi and b1 b2 b3
i
8 -1 43
-3 9 -7 16 528
3
) -2 22 3
5 22
15 6
7 -1 2 6
31

8 0

Next, the polynomial is constructed taking the numbers in the first row as coefficients, except the first of all. What
you want is

P 3 ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) + b 3 ( x - x 0 )( x - x 1 )( x - x 2 )

Replacing the data we obtain


-7 ( x - (-3)) ( x -5)+ 43
P(x)=9+ ( x -(-3))+ ( x - (-3)) ( x -5)( x -7)
6 528
and by simplifying we arrive at the polynomial
P ( x ) = 43 x 3 35 2 439 399
528 528 x + 22
44 x
Check:

P (-3) = 43(-3) 3 35(-3)2 439 349 43 35 439 349


528 44( ) - 528(-3)+ 22 = P (7) = (7)3 - 44(7)2 - 528(7) + 22 -1
528
43 35 439 349 43 439 349
P (5) = (5)3 (5)2 - 528(5) + 22 = 2 P (8) = (8)3 35 - 528(8) + 22 0
528 44( ) 528 - 44(8)2

Numerical analysis

uts
Department of Basic Sciences
3
3

xk
xk 3.2 Lagrange Interpolating Polynomial

In numerical analysis, the Lagrange polynomial, named after Joseph-Louis de Lagrange, is the polynomial that
interpolates a given set of points in the Lagrange form. It was discovered by Edward Waring in 1779 and later
rediscovered by Leonhard Euler in 1783.

To avoid the calculation of finite differences that is done in the Newton polynomial, the method of La Grange proposes
a simpler formula, but of course, because it is an approximation, it may have a slightly larger margin of error. Still, it is
a very practical method.

Reformulation of Newton's polynomial that avoids calculus by divided differences:

n n-
f n ( x ) = ∑ L i ( x ) f ( x i ) , where L i ( x ) = ∏ -j , i Π designates 'the product of'.
=0 j =0 x i x j

j 6= i
Example 3.2

Determine the Lagrange interpolating polynomial that contains the following points from the table:

xi -3 5 7 8
and i 9 2 -1 0

What we want is to construct the Lagrange polynomial using its recursive formula:

P(x ∑Li(x)f(xi)
) i=0
3
∑yiLi(x)
i=0
y 0 L 0 ( x )+ y 1 L 1 ( x )+ y 2 L 2 ( x )+ y 3 L
3 ( x )

9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0
L3(x)

All that remains is to construct the L i ( x ) , these will be determined by the formula

3 xLi(x)=∏k
=1k6=ixi

as it's shown in the following:

Department of Basic Sciences


Numerical analysis
34 Interpolation and curve fitting

Example 3.2 (Continued).

3
L0(x)= ∏ x - xk
k=1k6=0x0-xkx-x1x-x2x-x3= ..
x 0 - x 1 x 0 - x 2 x 0 - x 3 x -5 x -7 x -8 =..
-3-5 -3-7 -3-8 x -5 x -7 x -8
=
-8 .-10.-11
- x 3 x 2 131x7
=
880 + 44 - 880 + 22

3
L1 ( x ) = ∏ x - xk
k=1k6=1x1xk
x-x0x-x2x-x3
= ..
x1-x0x1-x2x1-x3
x - (-3) x -7 x - 8
=
5-(-3).5-7.5-8
x +3 x -7 x -8
=
8 . -2 . -3
x3 x2 11x7
=
48 4 48 + 2
+ -

3
L2 ( x ) = ∏ x - xk
k=1k6=2x2-xk
x-x0x-x1x-x3= ..
x2-x0x2-x1x2-x3
x - (-3) x - 5 x - 8
=
7-(-3).7-5.7-8
x +3 x -5 x -8
=
10 . 2 . -1
-x3 x2 x6
=
20 + 2 - 20 -

It is not necessary to construct L 3 ( x ) , since this polynomial will be multiplied by 0 . A useful technique to add
all these products is to build a table that contains all the information and then do the sum by columns, as shown
below:

Numerical analysis

Department of Basic Sciences


3
5

Example 3.2 (Continued).

yiLi x3 x2 x k
-9 9 -1179 63
9L0 800 44 880 22
1 -1 2 11
2L1 24 24 7
1 -1 2 1
-L2 20 20
6
P(x) 43
528
-35
4
-439
528
349
22

The last row indicates that the Lagrange interpolating polynomial is P ( x ) = 5 4 2 3 8 x 3 - 3 4 5 4 x 2 - 4 5 3 2 9 8 x + 3 2 4 2 9 .

3.3 Segmental Interpolation: Cubic Tracers

Given n + 1 points ( x 0 , y 0 ), ( x 1 , y 1 ), ..., ( x n , y n ) with x 0 , x 1 , ..., x n different real numbers, and f some real value
function defined on an interval [ a , b ] , which contains x 0 , x 1 , ..., x n , the aim is to approximate the function f by segments or
traces. Beforehand we are going to assume that:

x 0 < x 1 < . . . < xn

The idea is to approximate the function f in each subinterval [ x k , x k +1 ], k = 0, 1, ..., n - 1 , using a polynomial of degree less
than or equal to three, which we will assume as follows: shape:

(k)
p3(x)≡pk(x)=ak+bk(x−xk)+ck(x−xk)2+dk(x−xk)3

Numerical analysis
Department of Basic Sciences
36 Interpolation and curve fitting

1. p k ( x k ) = f ( x k ), p n - 1 ( x n )=f(y n ) , k = 0, 1, ..., n - 1 (basic interpolation condition) This condition


assumes n + 1 conditions.

2. p k ( x k +1 ) = p k +1 ( x k +1 ) , k = 0, 1, ..., n - 1 (continuity condition) This condition assumes n - 1 equation tions.

3. p 0 k ( x k +1 ) = p 0
k +1 (x k +1 ) , k = 0, 1, ..., n - 1 (first derivative condition) This condition suggests n - 1
conditions.

4. p 0 k 0 ( x k +1 ) = p 0
k
0
+1 (x k +1 ) , k = 0, 1, ..., n - 1 (second derivative condition) This condition suggests n - 1
conditions .

5. to . p 0 0 ( x 0 ) = f 0 ( x 0 ) b . p 0 n -1 ( x n ) = f 0 ( x n ) (boundary conditions)

For the p k to interpolate the points, the following conditions must be verified:

When checking conditions 1., 2., 3. and 4., it is ensured that the p k have their first and second derivatives at the
points x , x 1 , ..., x n , in this case it is said that the p k are cubic tracers that approximate the function f . Now, if
0

condition 5.a is met, the cubic tracer is called natural , and if condition 5.b is met, the cubic tracer is called bounded
boundary (they are not mutually exclusive).

Numerical analysis

Department of Basic Sciences


3
7

Example 3.3

Determine by segmental interpolation the set of polynomials that fit the points in the table shown below:

xi f ( xi )
0.2 0.2
0.6 0.55
1.2 0.211
1.4 0.2
1.8 0.44
2.23 0.83

To accomplish this task, it is first necessary to find the k i as shown in the following table:

xi f ( xi ) f 00 ( x i )
0.2 0.2 0
0.6 0.55 k1
1.2 0.211 k2
1.4 0.2 k3
1.8 0.44 k4
2.23 0.83 0

To determine these k i it is necessary to construct the following equations, which are constructed from the data recorded in
the previous table, then solve the system generated by them:

Numerical analysis

Department of Basic Sciences


38 Interpolation and curve fitting

Example 3.3 (Continued).

To construct these equations we rely on the formula:

Given three consecutive pairs from the table, [ x i − 1 , f ( x i − 1 )], [ x i , f ( x i )] and [ x i + 1 , f ( x i + 1 )] apply

( x i - x i - 1 ) f 00 ( x i - 1 ) + 2( x i + 1 - x i - 1 ) f 00 ( x i ) + ( x i + 1 - x i ) f 00 ( x i + 1 ) =
x [ f ( x i + 1 )- f ( x i )]+x [ f ( xi − 1 ) −f ( xi
xi+ xi-1
)]
1

Let's continue with the construction of the equations:

For the equation E 1 , take the pairs (0.2, 0.2), (0.6, 0.55) and (1.2, 0.211) , and apply the previous formula:

[(0.6-0-2)(0)]+2[1.2-0.2] k 1 +[1.2-0.6] k 2 = (0.24 - 0.55) + (0.2 - 0.55)


(1.2 - 0.6) 0.2 - 0.55
2k1 + 0.6k2 = -3.1 - 5.25
2k1 + 0.6k2 = -8.35

For the equation E 2 , take the pairs (0.6, 0.55), (1.2, 0.211) and (1.4, 0.2) :

0.6k1 + 1.6k2 + 0.2k3 = 1.9

For the equation E 3 , take the pairs (1.2, 0.211), (1.4, 0.2) and (1.8, 0.44) :

0.2k2 + 1.2k3 + 0.4k4 = 4.8

For the equation E 4 , take the pairs (1.2, 0.211), (1.4, 0.2) and (2.23, 0.83) :

6
(1.8 - 1.4) k 3 + 2(2.23 - 1.4) k 4 + (2.23 - 1.8)(0) = 2.23 - 1.8 (0.83 - 0.44) + (0.2 - 0.44)
(1.8 - 1.4)
0.4k3 + 1.66k4 = 1.84

The system of equations obtained is:

Numerical analysis

Department of Basic Sciences


3
9

Example 3.3 (Continued).

2k1 + 0.6k2 = -8.35


0.6k1 + 1.6k2 + 0.2k3 = 1.9
0.2k2 + 1.2k3 + 0.4k4 = 4.8
0.4k3 + 1.66k4 = 1.84

To better visualize the system of equations, let's place the coefficients of each equation in a table, as shown below:

And k1 k2 k3 k4 R
i
E1 2 0.6 0 0 -8.35
E2 0.6 1.6 0.2 0 1.9
E3 0 0.2 1.2 0.4 4.8
E4 0 0 0.4 1.66 1.84

We can solve this system with Matlab using the linsolve ( A , B ) command, where A and B are constructed as follows:

2 0.6 0 0 -8.35
0.6 1.6 0.2 0 1.9
B=
0 0.2 1.2 0.4 4.8
0 0 0.4 1.60 1.84

The solution returned by Matlab is:

k 1 = -4, 9588
k 2 = 2, 6128
k 3 = 3, 4740
k4 = 0.2713

This completes the initial table:

xi f ( xi ) f 00 ( x i )
0.2 0.2 0
0.6 0.55 -4.9588
1.2 0.211 2.6128
1.4 0.2 3.4740
1.8 0.44 0.2713
2.23 0.83 0

Numerical analysis

Department of Basic Sciences


40 Interpolation and curve fitting

Example 3.3 (Continued).

Once the table is completed, the polynomials P i ( x ) must be constructed, which fit the original data. This will be
done taking into account the formula:

Given two consecutive rows of the data table, [ x i − 1 , f ( x i − 1 ), f 00 ( x i − 1 )] and [ x i , f ( x i ), f 00 ( x i ) ] , apply


f 00 ( x i - 1 ) 3 f 00 ( x i f(xi−1) f 00 ( x i - 1 )( x i x i - 1 )
P i ( x )=6 ( i - i -1) ( x i - x ) +
(x−xi)3 (xi-
6( x) i - x i - ( -x i − x i − 1
x x

+ x)
f ( xi ) f ( x 1i ))( x i - x i - 1
00 )
(x−xi−1
( x i) − x i − 1 )
)

To determine P 1 ( x ) , take the rows [0.2, 0.2, 0] and [0.6, 0.55, -4.9588] , and apply the previous formula:
-4.9588 0.2 0(0.6 - 0.2)
P1 ( x ) (0.6- x ) 3 + ( x -0.2) 3 + (0.6 - x )
= 6(0.6 - 0.2) 6(0.6 - 0.2) 0.6 - 0.2
0.55 4.9588(0.6 - 0.2)
( x - 0.2)
0.6 - 0.2
-12397 1
6000 ( x -0.2) 3 + 2 (0.6 - x ) + 1.705586667() x - 0.2
( x ) 3 + 1.2397( x 2 ) + 0.957647 x - 0.02458892
-12397
6000

To determine P 2 ( x ) , take the rows [0.6, 0.55, -4.9588] and [1.2, 0.211, 2.6128] :
= 2.10322 x 3 - 6.2652 x 2 + 5.46037 x - 0.925168

-4.9588 2.6128 0.55 4.9588(1.2 - 0.6)


P2 ( x ) (1.2 - x ) 3 + ( x -0.6) 3 + (1.2 - x )
= 6(1.2 - 0.6) 6(1.2 - 0.6) (1.2 - 0.6)
0.24 2.6128(1.2 - 0.6)
( x - 0.6)
1.2 - 0.6
-1.377444444(1.2 - x ) 3 + 1633 ( x - 0.6) 3 + 1.412546667(1.2 - x ) + 867
( x - 0.6)
2250 6250

To determine P 3 ( x ) , take the rows [1.2, 0.211, 2.6128] and [1.4, 0.2, 3.4740] :

Numerical analysis

Department of Basic Sciences


4
1

Example 3.3 (Continued).

2.6128 3.4740 0.24 2.6128(1.4 - 1.2)


P3 ( x ) (1.4- x ) 3 + ( x - 1.2) 3 + (1.4 - x )
6(1.4 - 1.2) 6(1.4 - 1.2) (1.4 - 1.2) 6
=
0.2 7.4740(1.4 - 1.2)
-----------r-------------------------- ( x - 1.2)
(1.4 - 1.2) 6
1633 (1.4- x ) 3 + 579 ( x - 1.2) 3 + 1.112906667(1.4 - x ) + 4421
( x - 1.2)
750 200 5000
3
2153 3 - 1.2772 x - 0.525027 x + 1.46907
5000x

To determine P 4 ( x ) , take the rows [1.4, 0.2, 3.4740] and [1.8, 0.44, 0.2713] :
0.44 0.2713(1.8 - 1.4)
( x - 1.4)

3.4740 0.2713 0.2 3.4740(1.8 - 1.4)


P4 ( x ) (1.8- x ) 3 + ( x - 1.4) 3 + (1.8 - x )
6(1.8 - 1.4) 6(1.8 - 1.4) (1.8 - 1.4) 6
=
2 --------------------------------------------
(1.8 - 1.4) 6
579 2713 671
=
400 (1.8- x ) 3 + 3
24000 ( x -1.4) +
2500 (1.8 - x ) + 1.081913333( x - 1.4)

= - 32027 x 3 + 7, 34173 x 2 - 12.5915 x + 7.10008


24000

To determine P 5 ( x ) , take the rows [1.8, 0.44, 0.2713] and [2.23, 0.83, 0] :

0.2713 0 0.44 0.2713(2.23 - 1.8)


P5 ( x ) (2.23 - x ) 3 + (2.23 - x )
6(2.23 - 1.8) 6(2.23 - 1.8) ( x - 1 . 8 ) + (2.23 - 1.8) 6
=
0.83 0(2.23 - 1.8)
- ( x - 1.8)
(2.23 - 1.8) 6
2713 83
=
25800 (2.23 - x ) 3 + 1.003812647(2.23 - x ) + 8 4 3 3 ( x - 1.8)
27 . 13 x 3 + 0.703487 x 2 - 0.642357 x - 0.0697926
25800

Therefore the set of all polynomials that fit the data of the initial table, with their respective domains are:

Numerical analysis

Department of Basic Sciences


42 Interpolation and curve fitting

Example 3.3 (Continued).

-12397
P1 ( x ) = 6000 ( x ) 3 + 1.2397( x 2 ) + 0.957647 x - 0.02458892, x ∈ [0.2, 0.6]
P2 ( x ) = 2.10322 x 3 - 6.2652 x 2 + 5.46037 x - 0.925168, x ∈ [0.6, 1.2]

P ( x ) = 2153 x 3
3
5000 - 1.2772 x 3 - 0.525027 x + 1.46907, x ∈ [1.2, 1.4]

32027 x 3 + 7, 34173 x 2 - 12.5915 x + 7.10008, x ∈ [1.4, 1.8]


P4 ( x ) = -24000
P ( x )=- 27.13
5
25800 x 3 + 0.703487 x 2 - 0.642357 x - 0.0697926, x ∈ [1.8,2.23]

3.4 Curve Fitting by Least Squares

On New Year's Day 1801, Italian astronomer Giuseppe Piazzi discovered the asteroid Ceres. It was ca peace of
following its orbit for 40 days. During the course of that year, many scientists attempted to estimate its trajectory
based on Piazzi's observations (solving Kepler's nonlinear equations of motion is very difficult).

Most evaluations were useless; The only calculation sufficiently precise to allow Zach, a German astronomer, to
find Ceres again at the end of the year was that of a 24-year-old Carl Friedrich Gauss (he had already laid the
foundations for his approach in 1795, when he was still 18 years old). But his method of least squares was not
published until 1809, appearing in the second volume of his work on celestial mechanics, Theoria Motus Corporum
Coelestium in sctionibus conicis solem ambientium.

Least squares is a mathematical optimization technique that, given a series of measurements, attempts to find a
function that approximates the data by minimizing the sum of squares of the ordered differences (called residuals)
between the points generated by the function and the corresponding ones in the data.

An implicit requirement for the least squares method to work is that the errors of each method dida are distributed
randomly. The Gauss-Markov theorem proves that the min estimators Quadratic data are unbiased and that data
sampling does not have to fit, for example, a normal distribution. It is also important that the data collected is well
chosen, so that it allows so visibility in the variables that have to be resolved (to give more weight to a particular
data, see weighted least squares).

Numerical analysis

Department of Basic Sciences


4
3

3.4.1 Least Squares and Linear Regression Analysis


The simplest example of a least squares approximation is fitting a straight line to a con together with observations defined by
points:

Let ( x 1 , y 1 ), ( x 2 , y 2 ), ..., ( x n , y n ) be n points. The mathematical expression for the straight line that fits them is

y = a + bx +ε

Where the coefficients a and b are that represent the slope and the y-intercept respectively. ε is the error or difference
between the model and the observations.

Thus the error or residual can be expressed as:

ε = y - a - bx
Then the sum of the squares of these deviations would be given by:

nn
S r = ∑ε 2 = ∑ y i - a - bx i
Content...........................................................................................................................................................
Introduction..............................................................................................................................................32
Errors and Floating Point Representation....................................................................................................33
Mistakes...............................................................................................................................................33
1.1.1 Numerical approximation and error theory...........................................................................33
Definition 1.3......................................................................................................................................
Definition 1.6......................................................................................................................................
1.1.2 Significant numbers..........................................................................................................
Definition 1.7......................................................................................................................................
Example 1.1........................................................................................................................................
Rules for operations with significant figures......................................................................................
1.1.3 Precision and accuracy....................................................................................................
Definition 1.9......................................................................................................................................
high Low..............................................................................................................................................
1.1.4 Types of Rounding............................................................................................................
Example 1.2........................................................................................................................................
1.2.1 Normalization of a Number..............................................................................................
Definition 1.12....................................................................................................................................
Example 1.5........................................................................................................................................
1.2.2 IEEE 754 Standard...........................................................................................................
Example 1.6..................................................................................................................................10
1, 011100101011101000011111000011111000100112 ×25........................................................10
Department of Basic Sciences
44 Interpolation and curve fitting

The exponent, in excess of 2 n -1 - 1 , will be: 5 10 + (28 - 1 - 1) 10 = 5 10 + (2 7 - 1) 10 =


5 10 + (128 - 1) 10 = 132 10 ≡ 10000100 2................................................................................10
1, 0111001010111000000111...................................................................................................10
01110010101110100001111.........................................................................................................10
0100 0010 0011 1001 0101 1101 0000 1111................................................................................10
101110, 010101110100001111100001111100010011 2 ≈42395 D 0 F HEX.............................10
Example 1.7..................................................................................................................................11
1. Convert 3E40000016 to base 2:.............................................................................................11
2. Get the bits of the sign, the mantissa and the exponent:........................................................11
3. Convert the exponent to base 10:..........................................................................................11
011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 - 1)10 = 12410?12710 = -3......11
1,1 ×2-3..........................................................................................................................................11
1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0, 187510.......................................11
1,1×2-3=((20+2-1)×2-3)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510....................................11
3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10...................................................................11
Example 1.8..................................................................................................................................12
0.5625×2= 1.125 I —, d 1 = 1.......................................................................................................25
0.125×2=0.25 I —• d 2 = 0...........................................................................................................25
0.25×2=0.5 I —, d 3 = 0................................................................................................................25
0.5×2= 1........................................................................................................................................25
19,562510≡ 10011,10012.............................................................................................................25
10011,10012= 1,00111001 ×24.....................................................................................................25
410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 = 410 + 102310 = 102710 ≡
100000000112..............................................................................................................................26
0011100100000000000000000000000000000000000000000000...............................................26
Example 1.8 (Continued)..............................................................................................................27
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A 00000000000 HEX...........................27
Example 1.9..................................................................................................................................27
Example 1.9 (Continued)..............................................................................................................28
-0, 10111 × 2-126.............................................................................................................................28
-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)......................................................................28
= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)........................................................................28
≈ [-0.71875 × 1.1754943508222875079687365372222](10)......................................................28
≈ -8.44886564653519146352938612849 × 10-39..........................................................................28
[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )...........................................................................29
Department of Basic Sciences
4
5

[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )................................................................29


Solution of Equations No............................................................................................................................19
Linear...........................................................................................................................................................19
Theorem 2.1......................................................................................................................................19
Example 2.1..................................................................................................................................17
f (0) = -1 < 0..................................................................................................................................17
f (1)=1>0.......................................................................................................................................17
Methods for Solving Nonlinear Equations in One Variable................................................................17
2.2.1 Bisection Method...................................................................................................................17
Example 2.2..................................................................................................................................18
x 1 = 1 x2 = 2..............................................................................................................................18
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578............................................................................................18
f ( x3 ) = f ( 1.5 ) = -0.1823...........................................................................................................18
Example 2.2 (Continued)..............................................................................................................19
× 100=...........................................................................................................................................19
1.5-1..............................................................................................................................................19
1.5..................................................................................................................................................19
× 100 = 33.33%.............................................................................................................................19
x1 + x3 1 + 1.5..........................................................................................................................19
= 1.25............................................................................................................................................19
f ( x4 ) = f ( 1.25 ) = 0.0634...........................................................................................................19
× 100=...........................................................................................................................................19
1.5 - 1.25.......................................................................................................................................19
1.5..................................................................................................................................................19
× 100= 16.66%..............................................................................................................................19
x 4 + x 3 1.25 + 1.5....................................................................................................................19
= 1.375..........................................................................................................................................19
f ( x5 ) = f ( 1.375 ) = -0.0656.......................................................................................................19
2.2.2 False Position Method...........................................................................................................20
f ( xb ) -f ( xa ).................................................................................................................................20
x1 = 0............................................................................................................................................21
x2 = 1............................................................................................................................................21
f ( x1 ) = -1.....................................................................................................................................21
f ( x2 ) = 0.7854............................................................................................................................21
x 3= x 1 -
f ( x 2 ) - f ( x 1 ) = 0...........................................................................................................21
Department of Basic Sciences
46 Interpolation and curve fitting

f (0)(1-0) f (1)- f (0) = 0..................................................................................................................21


(-1)(1 - 0).......................................................................................................................................21
0.7854 - (-1) 1.7854....................................................................................................................21
= 0.5601........................................................................................................................................21
f ( x3 ) = f ( 0.5601 ) = 0.0707.......................................................................................................21
× 100=...........................................................................................................................................21
0.5601 - 1......................................................................................................................................21
0.5601............................................................................................................................................21
× 100 = 78.54%.............................................................................................................................21
(-1)(0.5601 - 0) 0.5601...............................................................................................................21
0.0707 - (-1) = 1.07070=0.5231......................................................................................................21
f ( x4 ) = f ( 0.5231 ) = 0.0051.......................................................................................................21
Example 2.3 (Continued)..................................................................................................................22
Definition 2.3....................................................................................................................................22
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )...........................................................................................................24
2.2.4 Newton-Raphson method...................................................................................................26
0
f ( x ) =.........................................................................................................................................26
f ( x i ) - 0 x i - x i + 1....................................................................................................................26
f 0 ( x i )..........................................................................................................................................26
g ( x ) = x -.....................................................................................................................................26
f ( x ) f0 ( x )....................................................................................................................................26
Example 2.5..................................................................................................................................27
e - x i + x i - 2..................................................................................................................................27
- e - x i + 1.......................................................................................................................................27
e - x 0 + x 0......................................................................................................................................27
-2...................................................................................................................................................27
- e - x 0 + 1......................................................................................................................................27
1-...................................................................................................................................................27
e -(1) +(1)-2.....................................................................................................................................27
e +( )
= 1.843482357250334....................................................................................................27
-(1)................................................................................................................................................27
e - x 1 + x 1 - 2................................................................................................................................27
- e - x 1 + 1.....................................................................................................................................27
1-...................................................................................................................................................27
e -(1.843482357250334) + (1.843482357250334) - 2.................................................................................27
Department of Basic Sciences
4
7

= 1.841406066157926..................................................................................................................27
e - x 2 + x 2 - 2................................................................................................................................27
- e - x 2 + 1.....................................................................................................................................27
1.841405660436976......................................................................................................................27
e - x 3 + x 3 - 2................................................................................................................................27
- e - x 3 + 1.....................................................................................................................................27
1.841405660436961......................................................................................................................27
e - x 4 + x 4 - 2................................................................................................................................27
- e - x 4 + 1......................................................................................................................................27
1.841405660436961......................................................................................................................27
2.2.5 Secant Method...................................................................................................................27
f ( x2 ) -f ( x1 )...............................................................................................................................27
f 0 ( x i )..........................................................................................................................................28
f 0 ( x i )..........................................................................................................................................28
xi +1= xi -
f ( x i ) - f ( x i - 1 )............................................................................................................28
Example 2.6..................................................................................................................................29
Yo
f ( x i )( x i - x i - 1 ) = x - f ( x i )- f ( x i - 1 ) ........................................................................29
x i
+ x i -2)( x i - x i - 1 ).................................................................................................................29
( e - x i + x i -2) - ( e - x i..................................................................................................................29
1 + x i - 1 - 2)................................................................................................................................29
( e -(1) +(1)-2)(1-0).........................................................................................................................29
( e -(1) +(1)-2)-( e -(0) +(0)-2)...........................................................................................................29
= 2.718281828459045..................................................................................................................29
Interpolation and curve fitting.....................................................................................................................33
f ( x k ) = y k , k = 1,..., n...............................................................................................................33
3.1 NEWTON'S INTERPOLATING POLYNOMIAL......................................................................................33
P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · + b n ( x - x 0 )( x - x 1 ) · · · ( x -
x n - 1 )..........................................................................................................................................33
P n ( x ) = b 0 +∑ b i ∏( x - x j ).................................................................................................33
f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ] x i + j + 1 - x i.....................................................31
P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]( x - x 0 )( x - x 1 ) +·· · + f [ x 0
, x 1 ,..., x n ]( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )........................................................................31
Example 3.1..................................................................................................................................31
Level b 1 :.....................................................................................................................................31
2-9 -7 (-1)-2 -3 (0 - (-1) 1.........................................................................................................31
Department of Basic Sciences
48 Interpolation and curve fitting

5-(-3)= 8 7-5 = 2 8-7 = 1..............................................................................................................31


3.2.............................................................................................................................................................33
Lagrange Interpolating Polynomial......................................................................................................33
Example 3.2..................................................................................................................................33
P ( x ).............................................................................................................................................33
9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0 L 3 ( x )......................................................................33
,
......................................................................................................................................................33
Example 3.2 (Continued)..............................................................................................................34
L 0 ( x ) = ∏ x - xk........................................................................................................................34
-3-5 -3-7 -3-8 x -5 x -7 x -8...........................................................................................................34
=
-8 .-10.-11...............................................................................................................................34
-x3 x 2 131x7.............................................................................................................................34
=
880 + 44 - 880 + 22..................................................................................................................34
L1 ( x ) = ∏ x - xk.........................................................................................................................34
x - x 0 x - x 2 x - x 3.........................................................................................................................34
= ...............................................................................................................................................34
x - (-3) x -7 x - 8............................................................................................................................34
=
5-(-3).5-7.5-8...............................................................................................................................34
x +3 x -7 x -8.................................................................................................................................34
=
8 . -2 . -3.................................................................................................................................34
x3 x 2 11x7...............................................................................................................................34
=
48 + 4 - 48 + 2...............................................................................................................................34
L2 ( x ) = ∏ x - xk.........................................................................................................................34
x - (-3) x - 5 x - 8...........................................................................................................................34
=
7-(-3).7-5.7-8...............................................................................................................................34
x +3 x -5 x -8.................................................................................................................................34
=
10 . 2 . -1.................................................................................................................................34
-x3 x 2 x 6..................................................................................................................................34
=
20 + 2 - 20 -.............................................................................................................................34
Example 3.2 (Continued)..............................................................................................................35
Segmental Interpolation: Cubic Tracers...............................................................................................35
x 0 < x 1 < . . . < xn..........................................................................................................................35
( k )................................................................................................................................................35
p 3 ( x ) ≡ p k ( x ) = a k + b k ( x − x k ) + c k ( x − x k ) 2 + d k ( x − x k ) 3.................................35
Example 3.3..................................................................................................................................37
Department of Basic Sciences
4
9

Example 3.3 (Continued)..............................................................................................................39


2k1 + 0.6k2 = -8.35.....................................................................................................................39
0.6k1 + 1.6k2 + 0.2k3 = 1.9.......................................................................................................39
0.2k2 + 1.2k3 + 0.4k4 = 4.8.......................................................................................................39
0.4k3 + 1.66k4 = 1.84...................................................................................................................39
k 1 = -4, 9588................................................................................................................................39
k 2 = 2, 6128.................................................................................................................................39
k 3 = 3, 4740.................................................................................................................................39
k4 = 0.2713...................................................................................................................................39
Example 3.3 (Continued)..............................................................................................................42
Curve Fitting by Least Squares............................................................................................................42
3.4.1 Least Squares and Linear Regression Analysis...........................................................................43
Criteria:.........................................................................................................................................59
Example 3.4..................................................................................................................................59
A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = 16 ,..............................................................59
b =.................................................................................................................................................59
5(260) - (16)(47)...........................................................................................................................59
5(106) - (16)2.................................................................................................................................59
(106)(47) - (206)(16).....................................................................................................................59
5(106) - (16)2.................................................................................................................................59
a =2...............................................................................................................................................59
b =3...............................................................................................................................................59
Example 3.5..................................................................................................................................60
A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D =∑ and i = 43.5........................60
b =.................................................................................................................................................60
8(546.63) - 89.8(43.5)...................................................................................................................60
8(1292.92) - (89.8)2 =⇒
...............................................................................................................60
(1292.92)(43.5) - 596.63(89.8).....................................................................................................60
8(1292.92) - (89.8)2.......................................................................................................................60
a = 0.20477...................................................................................................................................60
b = 3.1389.....................................................................................................................................60
3.4.2 Linearization of Nonlinear Relationships....................................................................................60
3.4.2.1 Model Power..................................................................................................................60
y = α x β.........................................................................................................................................60
| {z} |{z} | {z}.........................................................................................................................60
Department of Basic Sciences
50 Interpolation and curve fitting

Example 3.6 (Continued)..............................................................................................................62


A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C =∑ x i =................................................................62
a =.................................................................................................................................................62
b =.................................................................................................................................................62
5(1.9209) - (-2.0756)(-1.7657)......................................................................................................62
5(1.4556) - (-2.0756)2 =
..............................................................................................................62
(1.4556)(-1.7657) - (1.9209)(-2.0756)..........................................................................................62
5(1.4556) - (-2.0756)2...................................................................................................................62
a = 1.999947.................................................................................................................................62
b = 0.477078.................................................................................................................................62
Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997................................................................62
β = 1.999947.................................................................................................................................62
y = 2.9963 x 1.999947.........................................................................................................................62
Numerical Differentiation and Integration..................................................................................................52
Numerical Differentiation....................................................................................................................52
4.1.1 Accuracy Formulas for the First Derivative................................................................................52
Three Point Formulas...........................................................................................................................52
. Forward:......................................................................................................................................52
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 )..................................................................52
. Backward:...................................................................................................................................52
f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 ) 2 hours..........................................................52
Five Point Formulas.............................................................................................................................50
. Forward:......................................................................................................................................50
-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f ( x i +4 h ).................................50
. Central:........................................................................................................................................50
f 0 ( x i ) =......................................................................................................................................50
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2 h ).................................................................50
+ O ( h4 ).......................................................................................................................................50
Example 4.1..................................................................................................................................50
. Three Point Formula: If h = 0.1 we have to..............................................................................50
-3 f (2)+4 f (2+0.1)- f (2+2(0.1))...................................................................................................50
2 ∗ (0.1)........................................................................................................................................50
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404..................................................................50
0.2..................................................................................................................................................50
-0.417756085................................................................................................................................50
Department of Basic Sciences
5
1

. Five Point Formula: If h = 0.1 we have to.................................................................................50


-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f (2+4(0.1))..........................................50
12 ∗ (0.1)......................................................................................................................................50
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 + 16 × 0.745705212 - 3 ×
0.675463180..................................................................................................................................50
1.2..................................................................................................................................................50
-0.416135615................................................................................................................................50
Clearly f 0 (2) = cos(2)...............................................................................................................50
-0.416146837 - (-0.416135615)....................................................................................................50
-0.416146837................................................................................................................................50
-0.416146837 - (-0.417756085)....................................................................................................50
-0.416146837................................................................................................................................50
× 100 ≈ 0.38% and.......................................................................................................................50
4.2.............................................................................................................................................................51
Numerical Integration..........................................................................................................................51
4.2.1 Newton-Cotes formulas.........................................................................................................51
simple trapezoid rule.....................................................................................................................51
P ( x ) = f ( a ) + f ( b ) - f ( a ) ( x - a ) b - a......................................................................................52
Zbf (
x ) to.......................................................................................................................................52
f ( b ) -f ( a ) b - a.........................................................................................................................52
/(b)-/(a) (xa)21 b...........................................................................................................................52
b - a 2............................................................................................................................................52
( b - a ) f ( a )+ f ( b )........................................................................................................................52
Example 4.2..................................................................................................................................52
2.....................................................................................................................................................52
π
2 + 6π...........................................................................................................................................52
4.....................................................................................................................................................52
π.....................................................................................................................................................52
π
= 0.5235988................................................................................................................................52
6.....................................................................................................................................................52
5π-6 3............................................................................................................................................52
12...................................................................................................................................................52
0.4429715.....................................................................................................................................52
0.4429715 - 0.5235988.................................................................................................................52
0.4429715......................................................................................................................................52
Department of Basic Sciences
52 Interpolation and curve fitting

Composite Trapezoid Rule...........................................................................................................53


f ( x ) dx + f ( x ) dx +. . . + f ( x ) dx..........................................................................................53
x0 x 1 x n -1............................................................................................................................53
I = h f ( x 0 )+2 f ( x 1 ) + h f ( x 1 )+2 f ( x 2 ) +...+ h.........................................................................53
f ( xn − 1 ) + f (xn )........................................................................................................................53
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n ).................................................................................................53
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )........................................................................................................53
i =1
I=(b-a) .........................................................................................................................53
2 n

Et =...............................................................................................................................................53
( b - a ) 3 12 n 3..............................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................54
f 00 ' i =1...........................................................................................................................................54
Example 4.3..................................................................................................................................54
2.....................................................................................................................................................54
b - a 1............................................................................................................................................54
2
= 0.1............................................................................................................................................54
x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1].......................................................................................................54
0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]........................................................54
0.4513161......................................................................................................................................54
E r =..............................................................................................................................................54
0.4429715 - 0.4513161.................................................................................................................54
0.4429715......................................................................................................................................54
· 100 ≈ 1.884%..............................................................................................................................54
Differential equations..................................................................................................................................58
Ordinary.......................................................................................................................................................58
5.1 EULER METHOD..........................................................................................................................58
dy = f ( x , y ), y (0) = x 0..............................................................................................................58
f ( xn , yn ) =..................................................................................................................................56
( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )..........................................................................................56
Example 5.1..................................................................................................................................56
1. Analytical Solution................................................................................................................56
Z dy
= Z 2 xdx.....................................................................................................................................56
Ln | and | = x 2 + C.........................................................................................................................56
Example 5.1 (Continued)..............................................................................................................57
Department of Basic Sciences
5
3

Ln |1| =0+ C =⇒ C =0...................................................................................................................57


Ln | and | = x 2 =⇒ y = e x 2............................................................................................................57
2. Numerical Solution................................................................................................................57
x 1 = x 0 + h =0+0.1 =0.1.............................................................................................................57
y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which means ( x 1 , y 1 ) = (0.1, 1).......57
x 2 = x 1 + h = 0.1 +0.1 =0.2........................................................................................................57
y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2 )=(0.2,1.02)..............................57
0.3 1.0608.................................................................................................................................71
0.4 1.124448.............................................................................................................................71
0.5 1.21440384.........................................................................................................................71
0.6 1.33584422.........................................................................................................................71
0.7 1.49614553.........................................................................................................................71
0.8 1.70560591.........................................................................................................................71
0.9 1.97850285.........................................................................................................................71
1 2.33463336............................................................................................................................71
3. Approximate y (0.5)...............................................................................................................71
Example 5.1 (Continued)..............................................................................................................72
1.28403 - 1.21440.........................................................................................................................72
1.28403..........................................................................................................................................72
4. Graph and comparison of results...........................................................................................72
Fourth Order Runge-Kutta Method......................................................................................................72
d y
dx = f ( x , y ), y (0) = x 0............................................................................................................72
k 2 = f ( x n + h 2 , y n + h 2 k 1 ) k 3 = f ( x n + h 2 , y n + h 2 k 2 ) k 4 = f ( x n + h , y n + hk 3 )......73
6.....................................................................................................................................................73
Example 5.2..................................................................................................................................73
1. Analytical Solution................................................................................................................73
Example 5.2 (Continued)..............................................................................................................75
3. Approximate y(0.5)...............................................................................................................75
1.28403 - 1.28402.........................................................................................................................75
1.28403..........................................................................................................................................75
· 100 ≈ 0.000779%........................................................................................................................75
4. Graph and comparison of results..............................................................................................75
Introduction to Matlab.................................................................................................................................65
Matlab Environment Overview............................................................................................................65
. Save, Recall and Exit a work session.............................................................................................65
Department of Basic Sciences
54 Interpolation and curve fitting

. Current Work Directory.................................................................................................................65


. Delete Work Session Variables......................................................................................................63
. Clean screen...................................................................................................................................63
. Aids................................................................................................................................................63
. Variables in use..............................................................................................................................63
Basic Constants and Operators.............................................................................................................63
a) Constants..........................................................................................................................................63
b) Operations Between Scalars............................................................................................................64
f) Useful Functions...............................................................................................................................64
A.3............................................................................................................................................................65
Elemental Functions.............................................................................................................................65
A.4............................................................................................................................................................67
Matrices and Vectors............................................................................................................................67
a) Matrices............................................................................................................................................67
1. Create a Matrix..........................................................................................................................67
Example A.1.....................................................................................................................................67
42 44 70.........................................................................................................................................67
50 51..............................................................................................................................................67
2. Modify Elements of an Array....................................................................................................67
Example A.2.....................................................................................................................................68
3. Add or Delete Rows or Columns from a Matrix.......................................................................68
Example A.3.....................................................................................................................................68
Example A.4.....................................................................................................................................68
Example A.5.....................................................................................................................................69
Example A.6.....................................................................................................................................69
b) Vectors..........................................................................................................................................69
1. Vector Creation.........................................................................................................................69
Example A.7.....................................................................................................................................70
Example A.8.....................................................................................................................................70
2. Product of a Vector and a Scalar...............................................................................................70
Example A.9.....................................................................................................................................70
3. Product point.............................................................................................................................71
Example A.10...................................................................................................................................71
4. Cross Product............................................................................................................................71
Example A.11...................................................................................................................................71
Department of Basic Sciences
5
5

c) Basic Operations Between Matrices and Between Vectors..........................................................71


Graph of Functions in 2D.....................................................................................................................72
a) In Rectangular or Cartesian Coordinates.........................................................................................72
Graph of a Simple Function.................................................................................................................72
Example A.12...................................................................................................................................72
Example A.12 (Continued)...............................................................................................................73
2. Second Option: Using the function fplot('Function',Limits,[line specifiers]) .......................73
3. Third Option: Using the function ezplot('Function',Limits,[line specifiers]) ........................73
Graph of Several Functions on the Same Plane...................................................................................73
. First Option: Use The plot() Function ........................................................................................73
Example A.13...................................................................................................................................74
Example A.14...................................................................................................................................74
Graph of a Piecewise Function.............................................................................................................74
Example A.15...................................................................................................................................75
b) In Polar Coordinates........................................................................................................................75
Example A.16...................................................................................................................................76
A.6............................................................................................................................................................76
Symbolic Calculation...........................................................................................................................76
a) Algebraic Expressions......................................................................................................................76
Useful Functions About Algebraic Expression....................................................................................76
Example A.17...................................................................................................................................77
Evaluate a Symbolic Expression..........................................................................................................77
Example A.18...................................................................................................................................77
b) Solution of Polynomial Equations and Systems of Linear Equations.............................................78
Example A.19...................................................................................................................................78
Example A.20...................................................................................................................................78
c) Limit Calculation..........................................................................................................................79
Example A.21...................................................................................................................................79
3. Lim.........................................................................................................................................79
-4...................................................................................................................................................79
4. lim e - x...................................................................................................................................79
1.....................................................................................................................................................79
d) Derivative Calculus.......................................................................................................................79
Example A.22...................................................................................................................................79
d) Calculus of Integrals........................................................................................................................80
Department of Basic Sciences
56 Interpolation and curve fitting

Example A.23...................................................................................................................................80
A.7............................................................................................................................................................80
Programming in Matlab.......................................................................................................................80
The Function and Script Editor............................................................................................................80
How do we access the editor?...........................................................................................................81
How do you run a script?..................................................................................................................81
How is a function executed?.............................................................................................................81
How do you program in Matlab?......................................................................................................81
a) IF Control Statement.....................................................................................................................82
b) SWITCH Control Statement.........................................................................................................83
c) FOR Repetition Sentence..............................................................................................................83
d) WHILE Repetition Sentence.........................................................................................................83
e) Creation of Functions and Scripts.................................................................................................84
. Features..............................................................................................................................................84
Example A.24...................................................................................................................................84
Example A.25...................................................................................................................................85
Example A.26...................................................................................................................................85
Example A.27...................................................................................................................................86
. Scripts.................................................................................................................................................86
Example A.28...................................................................................................................................87
Example A.29...................................................................................................................................87
Bibliography................................................................................................................................................90

In this way, two equations called normal equations of the model are obtained that can be re released by any method.

Writing the system of equations in matrix form we have:


n n n ∑ x
n 2Xira ∑ and i n
ii = 1 n n
2

i=1 i=1
∑x i
2
n; det ( A ) = n =n∑xi -
n n· n∑x ∑xi 2
yxiyi i=1 i=
2xi 2x2 b ii = 1 i=1
i=1 i=1 i=1

If we use Cramer's rule:

Numerical analysis

Department of Basic Sciences


5
7

nn n
∑ and i ∑ x i n ∑ and
i
i=1 i=1 nn nn n nn
2 i=1
nn ∑ x i y i ∑ x i ∑ y i ∑ x i 2 -∑ x i ∑ x i y i nn n∑xiyi-∑xi∑yi
i=1 i=1 i=1 i=1 i=1 i=1 ∑xi∑xiyi i =1 i =1 i=1
b= i=1 i=1
det ( A ) nn
2
n
n∑xi2- ∑xi det ( A ) n∑xi2-
i=1 i=1 i =1

It is up to the reader to demonstrate that indeed these values for a and b are those that make the sum of errors ( S r
) minimum. Therefore, the linear model that best fits a data set is given by:

y = a + bx
where

nn nn n nn
∑yi∑xi2-∑xi∑xiyi n∑xiyi-∑xi∑yi
i=1i=1 i=1 i=1 i =1 i =1 i =1
a= 2
and b
nn n n∑
n∑xi2- ∑xi n∑xi2- i=
i=1 i=1 i =1 1

The difference between the value obtained with the regression equation and the observed y value must be kept in
mind. While it is an estimate and its goodness in the estimate depends on how close the relationship is between the
two variables being studied. This difference is known as error in the estimate, this error can be measured from the
Standard Deviation of the estimate:

Sxy = n
v r
, where ( and - a - bx i )
2

i
n -2 i ∑= 1

As this measure tries to summarize the disparity between what is observed and what is estimated, that is, it tries to
measure the average difference between what is observed and what is estimated or expected according to the model,
it can be considered be used as an indicator of the degree of precision with which the regression equation describes
the relationship between the two variables. It is not possible to compare with the relationships of variables given in
different units of measurement. dida. It is then necessary to calculate a measure that better interprets or measures the
degree of relationship between the variables:

Determination and correlation coefficients:

The coefficient of determination is the relationship between the explained variation and the total variation. This Coef
Correlation coefficient measures the strength of the relationship between variables. Its value will always be 0 < r < 1.
St - S r
r2 = , where S is the residual error associated with the dependent variable before regression. A
st
Alternative presentation is the following:

n nn
n∑xiyi-∑xi∑yi
r= i =1 i =1 i=1
2
nn∑yi -
xi
i=1

Numerical analysis

Department of Basic Sciences


58 Interpolation and curve fitting

Criteria:

• 0 to 0.2 Very weak correlation, negligible

• 0.2 to 0.4 Weak correlation. low

• 0.4 to 0.7 Moderate correlation

• 0.7 to 0.9 Strong, high, important correlation

• 0.9 to 1.0 Very strong correlation, very high

The correlation between the values of two variables is a fact. Whether we consider it satisfactory or not depends on
interpretation. Another problem that correlation represents is when it is asked if one variable somehow causes or determines
the other. Correlation does not imply causation. If the variables x and y are correlated, this may be because x causes y , or
because y causes x , or because some other variable affects both x and y , or for a combination of all of these reasons; or it
may be that the relationship is a coincidence.

Example 3.4

Determine the best Linear model that fits the following data:

xi -1 0 4 5 8
and i 1 3 11 13 19

First of all, let's perform the following calculations:

n = 5 , since there are 5 data ( x , y ).

A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = D =∑ and i =47


16 ,

Then we substitute into the formulas associated with a and b :

5(260) - (16)(47)
a =2
5(106) - (16)2
(106)(47) - (206)(16)
b= 5(106) - (16)2 b =3

Therefore linear y = ax + b associated with the data in the table is y = 2 x + 3.

Numerical analysis

Department of Basic Sciences


5
9

Example 3.5

A random sample of 8 cities is taken from a geographic region of 13 departments, which are determined by
census data, the percentage of graduates in higher education and the median income of each city, the results
are as follows:

% 7.2 6.7 17.0 12.5 6.3 23.9 6.0 10.2


M 4.2 4.9 7.0 6.2 3.8 7.6 4.4 5.4

Determine the best linear model that fits the information.

n = 8 , since there are 8 data ( x , y ).

A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D =∑ and i = 43.5

8(546.63) - 89.8(43.5)
8(1292.92) - (89.8)2 =⇒ a = 0.20477

(1292.92)(43.5) - 596.63(89.8)
b= 8(1292.92) - (89.8)2 b = 3.1389

Therefore, the linear model that best fits is y = 0.20477 x + 3.1389 .

3.4.2 Linearization of Nonlinear Relationships

Linearization is the process by which it is possible to convert a curve into a line so that through linear regression
certain parameters can be found that define the equation of the curve.

3.4.2.1 Model Power


y=αxβ
Log 10 y = Log 10 α x β
Log 10 y = Log 10 α+ Log 10 x β

The new independent and dependent variables are seen in

Cut Point
Log 10 y = z Log 10 α{ + β Log 10x
| {z} |{z} | {z}
New Var Dep Pte Nueva Var Ind

Numerical analysis

Department of Basic Sciences


60 Interpolation and curve fitting

3.4.2.2 Exponential Model


y=αeβx
Ln y = Ln α e β x
Ln y = Ln α + β xLn e

The new independent and dependent variables are seen in

Ln y = Ln α + β x |{z} |{z} |{z} |{z}


Var Dep P . CP Var Ind

3.4.2.3 Growth Rate


αx
x +β
x +β
αx
x+β
αx αx

The new independent and dependent variables are seen in

1+β 1
α αx
|{z} |{z}
P. c |{z} |{z}
P Var Ind
Var From
p

Example 3.6

Determine the Power Model that approximates the data in the following table:

Numerical analysis

xi and i
0.1 0.03
0.3 0.27
0.4 0.48
0.7 1.47
1 3

Department of Basic Sciences


6
1
Example 3.6 (Continued).

First of all, let's complete the table as follows:

xi and i log 10 ( x i ) log 10 ( y i )


0.1 0.03 -1 -1.5228
0.3 0.27 -0.5228 -0.5686
0.4 0.48 -0.3979 -0.3187
0.7 1.47 -0.1549 0.1673
1 3 0 0.4771

Then we take columns 3 and 4 and perform the following calculations:

A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C 2.0756 D =∑ and i = -1.7657 =∑ x i =

n = 5 -→ because we have 5 data.

Next we substitute in the linear model,

nA - CD 5(1.9209) - (-2.0756)(-1.7657)
a= 5(1.4556) - (-2.0756)2 = a = 1.999947

BD - AC (1.4556)(-1.7657) - (1.9209)(-2.0756)
b= 5(1.4556) - (-2.0756)2 b = 0.477078
nB - C 2

With which the linear model is obtained y = 1.999947 x + 0.477078 .

Now we proceed to determine the nonlinear model in power form:

Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997

β = 1.999947

Therefore, the power model is

y = 2.9963 x 1.999947

Numerical analysis

Department of Basic Sciences


Numerical Differentiation and
Integration

Numerical Differentiation

Formulas will be developed for forward, backward, and centered difference approximations for the first derivative using the
truncated Taylor series.

In the best of cases, these estimates present errors of order O ( h 2 ) ; that is, their errors were proportional to the square of their
step size. This level of accuracy is due to the number of terms in the Taylor series. Highly accurate formulas can be generated
by including additional terms in the Taylor series expansion.

4.1.1 Accuracy Formulas for the First Derivative

Three Point Formulas

Defining a step size h = x i + 1 - x i .

. Forward:

f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 )

. Backward:

f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 )

2 hours

f 0 ( x i )' f ( x i + h )- f ( x i - h ) + O ( h 2 )

. Central:

Numerical analysis

Department of Basic Sciences


50 Numerical Differentiation and Integration

Five Point Formulas

Defining a step size h = x i + 1 - x i .

. Forward:
-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f ( x i +4 h )
f 0 ( x i )= + O ( h4
12 noon )

. Central:
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2
f0(xi) + O ( h4
h) 12 )
=
noo
Using the truncated Taylor series, iterative approximate formulas can be developed to solve the derivatives
numerically using n points.

Using even the Taylor expression of the second derivative, greater precision can be found for the derivative of the
function.

Example 4.1

Use the forward three- and five-point formulas to approximate f 0 (2) , if f ( x ) = sin ( x ) and h = 0.1 .

. Three Point Formula: If h = 0.1 we have to

-3 f (2)+4 f (2+0.1)- f (2+2(0.1))


f 0 (2)
2 ∗ (0.1)
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404
0.2
-0.417756085

. Five Point Formula: If h = 0.1 we have to

-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f (2+4(0.1))


f 0 (2)
12 ∗ (0.1)
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 + 16 × 0.745705212 - 3 × 0.675463180
1.2
-0.416135615

Clearly f 0 (2) = cos(2) -0.416146837. Which allows us to calculate the error accord
-0.416146837 - (-0.417756085) ing to
each formula, i.e. E f 3 p -0.416146837 × 100 ≈ 0.38% and Ef5p=

-0.416146837 - (-0.416135615)
× 100 ≈ 0.002%. With these calculations it can be concluded that
-0.416146837
Five-point formula best approximates the first derivative at the given point.

Numerical analysis
uts

Department of Basic Sciences


5
1

4.2 Numerical Integration

In many cases, the integration of a function f ( x ) is difficult or even impossible to find, for this we must use a function that is
close to the original function and that is easier to solve, the most common is the polynomial of n terms.

The other issue to take into account is when the integration is developed. Numerically we must use summations to develop it;
The more points that are on the limits of the integral, the more approximate the solution will be, but this implies that the
algorithm is sometimes more complicated.

4.2.1 Newton-Cotes formulas


The Newton-Cotes integration formulas are the most common schemes within numerical integration. They are based on the
strategy of replacing a complicated function or a tabu data set lares with some approximate function that is easier to integrate.
The integral can be approximated using a series of polynomials applied piecewise to the function or data over intervals of length
with stante.

The open and closed forms of the Newton-Cotes formulas are available. Closed forms are those where the points at the
beginning and end of the limits of integration are known. Open formulas have the limits of integration extended beyond the
range of the data. The open Newton-Cotes formulas are generally not used in definite integration. However, they are used
extensively to evaluate improper integrals and in the solution of ordinary differential equations.

4.2.1.1 Simple and compound Trapezium Rule The trapezoid rule or trapezoidal rule is the first of the closed Newton-Cotes
formulas.

simple trapezoid rule

Consider the function f ( x ) , whose graph is between the extremes x = a and x = b as shown in the figure.

If we use a polynomial P ( x ) of the first degree as an approximation of f ( x ) we have:

Numerical analysis

Department of Basic Sciences


52 Numerical Differentiation and Integration

x - bx - a
P(x)=f(a) +f(b) , which is equivalent to:

P ( x ) = f ( a ) + f ( b )- f ( a ) ( x - a )
b-a

The area under this straight line is an approximation of the area under the curve between limits a and b .

Zbf (
x) f ( b ) -f ( a )
to dx dx
b-a

Z
f ( x ) dx ≈ Z
to dx
to to b-a
/(b)-/(a) (xa)21 b
f(a)x+ b-a2

( b - a ) f ( a )+ f ( b )

Example 4.2

Use the simple trapezoid rule to approximate the integral arcsin ( x ) dx .


2

f (1)+ f ( 1 2 )
2
π
2 + 6π
4
π
π
= 0.5235988
6

5π-6 3
The exact solution of this integral is 0.4429715.
12

The relative percentage error that was committed when applying the simple trapezoid rule is given by:

Er=

Department of Basic Sciences


5
3

0.4429715 - 0.5235988
0.4429715

Numerical analysis

· 100 ≈ 18.2%

Department of Basic Sciences


54 Numerical Differentiation and Integration

Composite Trapezoid Rule

One way to improve the accuracy of the trapezoid rule is to divide the interval of integration from a to b into an n
number of segments and apply the method to each of them. The resulting equations are called multiple application or
compound integration formulas.

There are n + 1 equally spaced base points ( x 0 , x 1 , x 2 , ..., x n ) . Consequently there are n segments of equal
b-a
width: h =

If a and b are designated as x 0 and x n respectively, the total integral will be represented as:

Zx1 x2 xn
f ( x ) dx + f ( x ) dx +. . . + f(x)
dx
x 0 x 1 x n -1

By substituting the trapezoidal rule for each integral:

I = h f ( x 0 )+2 f ( x 1 ) + f ( x 1 )+2 f ( x 2 ) +...+


h h f ( xn − 1 ) + f (xn
)

And by grouping terms:

h n-1
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n )

Using h = ( b ? a )/ n and expressing it in the general form:

n-1
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )
i =1
I=(b-a) 2 n

An error for the multiple application trapezoidal rule can be obtained by adding the individual errors of each segment,
to give:

( b - a ) 3 n 00
Et = ∑ f (ξ i )
12 n 3
i=1

Where f 00 (ξ i ) is the second derivative at a point ξ i located on segment i . This result can be simplified by estimating
the mean or average value of the second derivative for the entire interval:

Numerical analysis

Department of Basic Sciences


5
5

n
∑ f 00 (ξ i )
00 i =1
f '
n
Therefore ∑ f 00 (ξ i ) ≈ nf 00 . Then the trapezoidal error equation can be written as:

E a = (b1−2 a2)3 n f 00

Thus, if the number of segments is doubled, the truncation error will decrease to a quarter.

Example 4.3

Use the compound trapezoid rule with n=5 subintervals to approximate the integral arcsin ( x ) dx .

2
1
b-a1 2
= 0.1
n

x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1]

arcsin ( x ) dx 0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]


2
0.4513161

The relative percentage error that was committed when applying the compound trapezoid rule is given by:

0.4429715 - 0.4513161
Er= · 100 ≈ 1.884%
0.4429715

We see that a better approximation has indeed been obtained with this method, reducing the true relative error from
18.2% to 1.884%.

Numerical analysis

Department of Basic Sciences


Differential equations
Ordinary

5.1 EULER METHOD

One of the simplest techniques to approximate solutions to a differential equation is the Euler method, or tangent lines. The idea
of Euler's method is very simple and is based on the geometric meaning of the derivative of a function at a given point.

Suppose we had the solution curve of the differential equation and we drew the tangent line to the curve at the point given by
the initial condition.

dy = f ( x , y ), y (0) = x 0
dx
Observe in the figure that the slope of the line intersecting the curve is given by
yn + 1 - yn
xn + 1 - xn
, but x n + 1 = x n + h , so
yn + 1 - yn = yn + 1 - yn
xn + h - xn h

Numerical analysis

Department of Basic Sciences


56 Ordinary differential equations

is approximately equal to the slope of the tangent line as long as h is small.

From here we obtain that

yn +1 - yn
f ( xn , yn ) = yn = yn + f (xn , yn ) · h
h

We thus obtain the well-known Euler Formula :

yn + 1 = yn + f ( xn , yn ) · h

With which we can use the point ( x 0 , y 0 ) to construct the next point ( x 1 , y 1 ) and so on. In this way we generate
the sequence of points:

( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )

which are expected to be close to the points ( x 0 , y ( x 0 )), ( x 1 , y ( x 1 )), · · · , ( x n , y ( x n ))

Example 5.1

Given the following differential equation y = 2 xy with the initial condition: y (0) = 1 ,

1. Solve it analytically.

2. Use Euler's Method on the interval from x = 0 to x = 1 with step size h = 0.1 .

3. Approximate y(0.5).

4. Graph and compare the results.

1. Analytical Solution

First we observe that this equation can be solved analytically (by traditional methods of differential equations). We
can apply the method of separation of variables:

Z dy
dy = Z 2 xdx
= 2 x dx Ln | and | = x 2 +
y C

Numerical analysis

Department of Basic Sciences


5
7

Example 5.1 (Continued).

Substituting the initial condition x = 0 -→ y = 1

Ln |1| =0+ C =⇒ C =0

Therefore, we have that the solution is given by:

Ln | and | = x 2 =⇒ y = e x 2

2. Numerical Solution

We apply Euler's method for values of x between 0 and 1 with step size h = 0.1 and Initial condition: x = 0 -→ y = 1 , which
means that ( x 0 , y 0 ) = (0, 1)

Now,

x 1 = x 0 + h =0+0.1 =0.1

y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which means ( x 1 , y 1 ) = (0.1, 1)

Applying Euler's formula again, we have, in a second step:

x 2 = x 1 + h = 0.1 +0.1 =0.2

y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2 )=(0.2,1.02)

We summarize all the results in the following table:

Content...........................................................................................................................................................2
Introduction..............................................................................................................................................32
Errors and Floating Point Representation....................................................................................................33
Mistakes...............................................................................................................................................33
1.1.1 Numerical approximation and error theory...........................................................................33
Definition 1.3......................................................................................................................................3
Definition 1.6......................................................................................................................................4
1.1.2 Significant numbers..........................................................................................................4
Definition 1.7......................................................................................................................................4
Example 1.1........................................................................................................................................5
Rules for operations with significant figures......................................................................................6
1.1.3 Precision and accuracy....................................................................................................6
Definition 1.9......................................................................................................................................6
high Low..............................................................................................................................................7
1.1.4 Types of Rounding............................................................................................................7

Department of Basic Sciences


58 Ordinary differential equations

Example 1.2........................................................................................................................................7
1.2.1 Normalization of a Number..............................................................................................9
Definition 1.12....................................................................................................................................9
Example 1.5........................................................................................................................................9
1.2.2 IEEE 754 Standard...........................................................................................................9
Example 1.6..................................................................................................................................10
1, 011100101011101000011111000011111000100112 ×25........................................................10
The exponent, in excess of 2 n -1 - 1 , will be: 5 10 + (28 - 1 - 1) 10 = 5 10 + (2 7 - 1) 10 = 5
10 + (128 - 1) 10 = 132 10 ≡ 10000100 2...................................................................................10
1, 0111001010111000000111...................................................................................................10
01110010101110100001111.........................................................................................................10
0100 0010 0011 1001 0101 1101 0000 1111................................................................................10
101110, 010101110100001111100001111100010011 2 ≈42395 D 0 F HEX.............................10
Example 1.7..................................................................................................................................11
1. Convert 3E40000016 to base 2:.............................................................................................11
2. Get the bits of the sign, the mantissa and the exponent:........................................................11
3. Convert the exponent to base 10:..........................................................................................11
011111002 - (28-1 - 1)10 = 12410 - (27 - 1)10 = 12410 - (128 - 1)10 = 12410?12710 = -3......11
1,1 ×2-3..........................................................................................................................................11
1,1 ×2-3 =0,00112= (2-3 + 2-4)10 = 0, 12510 + 0, 062510 = 0, 187510.......................................11
1,1×2-3=((20+2-1)×2-3)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510....................................11
3 E 400000 HEX = 1.1 ×2 -3 = 0.0011 2 = 0.1875 10...................................................................11
Example 1.8..................................................................................................................................12
0.5625×2= 1.125 I —, d 1 = 1.......................................................................................................25
0.125×2=0.25 I —• d 2 = 0...........................................................................................................25
0.25×2=0.5 I —, d 3 = 0................................................................................................................25
0.5×2= 1........................................................................................................................................25
19,562510≡ 10011,10012.............................................................................................................25
10011,10012= 1,00111001 ×24.....................................................................................................25
410 + (211-1 - 1)10 = 410 + (210 - 1)10 = 410 + (1024 - 1)10 = 410 + 102310 = 102710 ≡
100000000112..............................................................................................................................26
0011100100000000000000000000000000000000000000000000...............................................26
Example 1.8 (Continued)..............................................................................................................27
19, 5625 10 = 10011.1001 2 = 1.00111001 ×2 4 ≡4033 A 00000000000 HEX...........................27
Example 1.9..................................................................................................................................27
Department of Basic Sciences
5
9

Example 1.9 (Continued)..............................................................................................................28


-0, 10111 × 2-126.............................................................................................................................28
-0.10111 × 2-126 = [-(2-1 + 2-3 + 2-4 + 2-5) ×2-126](10)......................................................................28
= [-(0.5 + 0.125 + 0.0625 + 0.03125) × 2-126](10)........................................................................28
≈ [-0.71875 × 1.1754943508222875079687365372222](10)......................................................28
≈ -8.44886564653519146352938612849 × 10-39..........................................................................28
[(2 -23 -2)·2 127 ]( 10 )6 x 6[(2-2 -23 )·2 127 ]( 10 )...........................................................................29
[(2 -52 - 2) · 2 1023 ]( 10 ) 6 x 6 [(2 - 2 -52 ) · 2 1023 ]( 10 )................................................................29
Solution of Equations No............................................................................................................................19
Linear...........................................................................................................................................................19
Theorem 2.1......................................................................................................................................19
Example 2.1..................................................................................................................................17
f (0) = -1 < 0..................................................................................................................................17
f (1)=1>0.......................................................................................................................................17
Methods for Solving Nonlinear Equations in One Variable................................................................17
2.2.1 Bisection Method...................................................................................................................17
Example 2.2..................................................................................................................................18
x 1 = 1 x2 = 2.............................................................................................................................18
f ( x1 ) = 0.3679 f ( x2 ) = -0.5578............................................................................................18
f ( x3 ) = f ( 1.5 ) = -0.1823...........................................................................................................18
Example 2.2 (Continued)..............................................................................................................19
× 100=...........................................................................................................................................19
1.5-1..............................................................................................................................................19
1.5..................................................................................................................................................19
× 100 = 33.33%.............................................................................................................................19
x1 + x3 1 + 1.5..........................................................................................................................19
= 1.25............................................................................................................................................19
f ( x4 ) = f ( 1.25 ) = 0.0634...........................................................................................................19
× 100=...........................................................................................................................................19
1.5 - 1.25.......................................................................................................................................19
1.5..................................................................................................................................................19
× 100= 16.66%..............................................................................................................................19
x 4 + x 3 1.25 + 1.5....................................................................................................................19
= 1.375..........................................................................................................................................19

Department of Basic Sciences


60 Ordinary differential equations

f ( x5 ) = f ( 1.375 ) = -0.0656.......................................................................................................19
2.2.2 False Position Method...........................................................................................................20
f ( xb ) -f ( xa ).................................................................................................................................20
x1 = 0............................................................................................................................................21
x2 = 1............................................................................................................................................21
f ( x1 ) = -1.....................................................................................................................................21
f ( x2 ) = 0.7854...........................................................................................................................21
x 3= x 1 -
f ( x 2 ) - f ( x 1 ) = 0...........................................................................................................21
f (0)(1-0) f (1)- f (0) = 0..................................................................................................................21
(-1)(1 - 0).......................................................................................................................................21
0.7854 - (-1) 1.7854....................................................................................................................21
= 0.5601........................................................................................................................................21
f ( x3 ) = f ( 0.5601 ) = 0.0707.......................................................................................................21
× 100=...........................................................................................................................................21
0.5601 - 1......................................................................................................................................21
0.5601............................................................................................................................................21
× 100 = 78.54%.............................................................................................................................21
(-1)(0.5601 - 0) 0.5601...............................................................................................................21
0.0707 - (-1) = 1.07070=0.5231......................................................................................................21
f ( x4 ) = f ( 0.5231 ) = 0.0051.......................................................................................................21
Example 2.3 (Continued)..................................................................................................................22
Definition 2.3....................................................................................................................................22
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )..........................................................................................................24
2.2.4 Newton-Raphson method...................................................................................................26
f 0 ( x ) =.........................................................................................................................................26
f ( x i ) - 0 x i - x i + 1....................................................................................................................26
f 0 ( x i )..........................................................................................................................................26
g ( x ) = x -.....................................................................................................................................26
f ( x ) f0 ( x )....................................................................................................................................26
Example 2.5..................................................................................................................................27
e - x i + x i - 2..................................................................................................................................27
- e - x i + 1.......................................................................................................................................27
e - x 0 + x 0......................................................................................................................................27
-2...................................................................................................................................................27

Department of Basic Sciences


6
1

- e - x 0 + 1......................................................................................................................................27
1-...................................................................................................................................................27
e -(1) +(1)-2.....................................................................................................................................27
e +( )
= 1.843482357250334....................................................................................................27
-(1)................................................................................................................................................27
e - x 1 + x 1 - 2................................................................................................................................27
- e - x 1 + 1.....................................................................................................................................27
1-...................................................................................................................................................27
e -(1.843482357250334) + (1.843482357250334) - 2.................................................................................27
= 1.841406066157926..................................................................................................................27
e - x 2 + x 2 - 2................................................................................................................................27
- e - x 2 + 1.....................................................................................................................................27
1.841405660436976......................................................................................................................27
e - x 3 + x 3 - 2................................................................................................................................27
- e - x 3 + 1.....................................................................................................................................27
1.841405660436961......................................................................................................................27
e - x 4 + x 4 - 2................................................................................................................................27
- e - x 4 + 1......................................................................................................................................27
1.841405660436961......................................................................................................................27
2.2.5 Secant Method...................................................................................................................27
f ( x2 ) -f ( x1 )...............................................................................................................................27
f 0 ( x i )..........................................................................................................................................28
f 0 ( x i )..........................................................................................................................................28
xi +1= xi -
f ( x i ) - f ( x i - 1 )............................................................................................................28
Example 2.6..................................................................................................................................29
Yo
f ( x i )( x i - x i - 1 ) = x - f ( x i )- f ( x i - 1 ) ........................................................................29
x i
+ x i -2)( x i - x i - 1 ).................................................................................................................29
( e - x i + x i -2) - ( e - x i..................................................................................................................29
1 + x i - 1 - 2)................................................................................................................................29
( e -(1) +(1)-2)(1-0).........................................................................................................................29
( e -(1) +(1)-2)-( e -(0) +(0)-2)...........................................................................................................29
= 2.718281828459045..................................................................................................................29
Interpolation and curve fitting.....................................................................................................................33
f ( x k ) = y k , k = 1,..., n...............................................................................................................33

Department of Basic Sciences


62 Ordinary differential equations

3.1 NEWTON'S INTERPOLATING POLYNOMIAL......................................................................................33


P n ( x ) = b 0 + b 1 ( x - x 0 ) + b 2 ( x - x 0 )( x - x 1 ) +·· · + b n ( x - x 0 )( x - x 1 ) · · · ( x - x
n - 1 ).............................................................................................................................................33
P n ( x ) = b 0 +∑ b i ∏( x - x j ).................................................................................................33
f [ x i + 1 , ..., x i + j + 1 ] - f [ x i , ..., x i + j ] x i + j + 1 - x i.....................................................31
P n ( x ) = f [ x 0 ] + f [ x 0 , x 1 ]( x - x 0 ) + f [ x 0 , x 1 , x 2 ]( x - x 0 )( x - x 1 ) +·· · + f [ x 0 ,
x 1 ,..., x n ]( x - x 0 )( x - x 1 ) · · · ( x - x n - 1 )..........................................................................31
Example 3.1..................................................................................................................................31
Level b 1 :.....................................................................................................................................31
2-9 -7 (-1)-2 -3 (0 - (-1) 1.........................................................................................................31
=
5-(-3) 8 7-5 = 2 8-7 = 1..............................................................................................................31
3.2.............................................................................................................................................................33
Lagrange Interpolating Polynomial......................................................................................................33
Example 3.2..................................................................................................................................33
P ( x ).............................................................................................................................................33
9 L 0 ( x ) + 2 L 1 ( x ) + (-1) L 2 ( x ) + 0 L 3 ( x )......................................................................33
,
......................................................................................................................................................33
Example 3.2 (Continued)..............................................................................................................34
L 0 ( x ) = ∏ x - xk........................................................................................................................34
-3-5 -3-7 -3-8 x -5 x -7 x -8...........................................................................................................34
=
-8 .-10.-11...............................................................................................................................34
-x3 x 2 131x7.............................................................................................................................34
=
880 + 44 - 880 + 22..................................................................................................................34
L1 ( x ) = ∏ x - xk.........................................................................................................................34
x - x 0 x - x 2 x - x 3.........................................................................................................................34
= ...............................................................................................................................................34
x - (-3) x -7 x - 8............................................................................................................................34
=
5-(-3).5-7.5-8...............................................................................................................................34
x +3 x -7 x -8.................................................................................................................................34
=
8 . -2 . -3.................................................................................................................................34
x3 x 2 11x7...............................................................................................................................34
=
48 + 4 - 48 + 2...............................................................................................................................34
L2 ( x ) = ∏ x - xk.........................................................................................................................34
x - (-3) x - 5 x - 8...........................................................................................................................34
=
7-(-3).7-5.7-8...............................................................................................................................34
Department of Basic Sciences
6
3

x +3 x -5 x -8.................................................................................................................................34
=
10 . 2 . -1.................................................................................................................................34
-x3 x 2 x 6..................................................................................................................................34
=
20 + 2 - 20 -.............................................................................................................................34
Example 3.2 (Continued)..............................................................................................................35
Segmental Interpolation: Cubic Tracers...............................................................................................35
x 0 < x 1 < . . . < xn..........................................................................................................................35
( k )................................................................................................................................................35
p 3 ( x ) ≡ p k ( x ) = a k + b k ( x − x k ) + c k ( x − x k ) 2 + d k ( x − x k ) 3.................................35
Example 3.3..................................................................................................................................37
Example 3.3 (Continued)..............................................................................................................39
2k1 + 0.6k2 = -8.35.....................................................................................................................39
0.6k1 + 1.6k2 + 0.2k3 = 1.9.......................................................................................................39
0.2k2 + 1.2k3 + 0.4k4 = 4.8.......................................................................................................39
0.4k3 + 1.66k4 = 1.84...................................................................................................................39
k 1 = -4, 9588................................................................................................................................39
k 2 = 2, 6128.................................................................................................................................39
k 3 = 3, 4740.................................................................................................................................39
k4 = 0.2713...................................................................................................................................39
Example 3.3 (Continued)..............................................................................................................42
Curve Fitting by Least Squares............................................................................................................42
3.4.1 Least Squares and Linear Regression Analysis...........................................................................43
Criteria:.........................................................................................................................................59
Example 3.4..................................................................................................................................59
A =∑ x i y i = 260 , B =∑ x i 2 = 106 , C =∑ x i = 16 ,..............................................................59
b =.................................................................................................................................................59
5(260) - (16)(47)...........................................................................................................................59
5(106) - (16)2.................................................................................................................................59
(106)(47) - (206)(16).....................................................................................................................59
5(106) - (16)2.................................................................................................................................59
a =2...............................................................................................................................................59
b =3...............................................................................................................................................59
Example 3.5..................................................................................................................................60
A =∑ x i y i = 596.63 B =∑ x i 2 = 1292.92 C =∑ x i = 89.8 D =∑ and i = 43.5........................60

Department of Basic Sciences


64 Ordinary differential equations

b =.................................................................................................................................................60
8(546.63) - 89.8(43.5)...................................................................................................................60
8(1292.92) - (89.8)2 =⇒
...............................................................................................................60
(1292.92)(43.5) - 596.63(89.8).....................................................................................................60
8(1292.92) - (89.8)2.......................................................................................................................60
a = 0.20477...................................................................................................................................60
b = 3.1389.....................................................................................................................................60
3.4.2 Linearization of Nonlinear Relationships...................................................................................60
3.4.2.1 Model Power..................................................................................................................60
y = α x β.........................................................................................................................................60
| {z} |{z} | {z}.........................................................................................................................60
Example 3.6 (Continued)..............................................................................................................62
A =∑ x i y i = 1.9209 B =∑ x i 2 = 1.4856 C =∑ x i =................................................................62
a =.................................................................................................................................................62
b =.................................................................................................................................................62
5(1.9209) - (-2.0756)(-1.7657)......................................................................................................62
5(1.4556) - (-2.0756)2 =
..............................................................................................................62
(1.4556)(-1.7657) - (1.9209)(-2.0756)..........................................................................................62
5(1.4556) - (-2.0756)2...................................................................................................................62
a = 1.999947.................................................................................................................................62
b = 0.477078.................................................................................................................................62
Log 10 α = 0.477078 -→ α= 100.477078 -→ α = 2.9997................................................................62
β = 1.999947.................................................................................................................................62
y = 2.9963 x 1.999947.........................................................................................................................62
Numerical Differentiation and Integration..................................................................................................52
Numerical Differentiation....................................................................................................................52
4.1.1 Accuracy Formulas for the First Derivative................................................................................52
Three Point Formulas...........................................................................................................................52
. Forward:......................................................................................................................................52
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 ).................................................................52
. Backward:...................................................................................................................................52
f 0 ( x )' f ( x i -2 h )-4 f ( x i - h )+3 f ( x i ) + O ( h 2 ) 2 hours..........................................................52
Five Point Formulas.............................................................................................................................50
. Forward:......................................................................................................................................50

Department of Basic Sciences


6
5

-25 f ( x i )+48 f ( x i + h ) -36 f ( x i +2 h )+16 f ( x i +3 h ) -3 f ( x i +4 h ).................................50


. Central:........................................................................................................................................50
f 0 ( x i ) =......................................................................................................................................50
f ( x i -2 h ) - 8 f ( x i - h ) +8 f ( x i + h ) - f ( x i +2 h ).................................................................50
+ O ( h4 ).......................................................................................................................................50
Example 4.1..................................................................................................................................50
. Three Point Formula: If h = 0.1 we have to..............................................................................50
-3 f (2)+4 f (2+0.1)- f (2+2(0.1))...................................................................................................50
2 ∗ (0.1)........................................................................................................................................50
-3 × 0.909297427 + 4 × 0.863209367 - 0.808496404..................................................................50
0.2..................................................................................................................................................50
-0.417756085................................................................................................................................50
. Five Point Formula: If h = 0.1 we have to................................................................................50
-25 f (2)+48 f (2+0.1)-36 f (2+2(0.1))+16 f (2+3(0.1))-3 f (2+4(0.1))..........................................50
12 ∗ (0.1)......................................................................................................................................50
-25 × 0.909297426 + 48 × 0.863209366 - 36 × 0.808496403 + 16 × 0.745705212 - 3 ×
0.675463180..................................................................................................................................50
1.2..................................................................................................................................................50
-0.416135615................................................................................................................................50
Clearly f 0 (2) = cos(2)...............................................................................................................50
-0.416146837 - (-0.416135615)....................................................................................................50
-0.416146837................................................................................................................................50
-0.416146837 - (-0.417756085)....................................................................................................50
-0.416146837................................................................................................................................50
× 100 ≈ 0.38% and.......................................................................................................................50
4.2.............................................................................................................................................................51
Numerical Integration..........................................................................................................................51
4.2.1 Newton-Cotes formulas.........................................................................................................51
simple trapezoid rule.....................................................................................................................51
P ( x ) = f ( a ) + f ( b ) - f ( a ) ( x - a ) b - a......................................................................................52
Zbf (
x ) to.......................................................................................................................................52
f ( b ) -f ( a ) b - a.........................................................................................................................52
/(b)-/(a) (xa)21 b...........................................................................................................................52
b - a 2............................................................................................................................................52
( b - a ) f ( a )+ f ( b )........................................................................................................................52
Department of Basic Sciences
66 Ordinary differential equations

Example 4.2..................................................................................................................................52
2.....................................................................................................................................................52
π
2 + 6π...........................................................................................................................................52
4.....................................................................................................................................................52
π.....................................................................................................................................................52
π
= 0.5235988................................................................................................................................52
6.....................................................................................................................................................52
5π-6 3............................................................................................................................................52
12...................................................................................................................................................52
0.4429715.....................................................................................................................................52
0.4429715 - 0.5235988.................................................................................................................52
0.4429715......................................................................................................................................52
Composite Trapezoid Rule...........................................................................................................53
f ( x ) dx + f ( x ) dx +. . . + f ( x ) dx..........................................................................................53
x0 x 1 x n -1............................................................................................................................53
I = h f ( x 0 )+2 f ( x 1 ) + h f ( x 1 )+2 f ( x 2 ) +...+ h.........................................................................53
f ( xn − 1 ) + f (xn ).......................................................................................................................53
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n ).................................................................................................53
f ( x 0 )+2 ∑ f ( x i )+ f ( x n )........................................................................................................53
i =1
I=(b-a) .........................................................................................................................53
2 n

Et =...............................................................................................................................................53
( b - a ) 3 12 n 3..............................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................53
∑ f 00 (ξ i )......................................................................................................................................54
f 00 ' i =1...........................................................................................................................................54
Example 4.3..................................................................................................................................54
2.....................................................................................................................................................54
b - a 1............................................................................................................................................54
2
= 0.1............................................................................................................................................54
x i = [0.5, 0.6, 0.7, 0.8, 0.9, 1].......................................................................................................54
0 2 . 1 [ f (0.5)+2 f (0.6)+2 f (0.7)+2 f (0.8)+2 f (0.9)+ f (1)]........................................................54
0.4513161......................................................................................................................................54
E r =..............................................................................................................................................54
0.4429715 - 0.4513161.................................................................................................................54

Department of Basic Sciences


6
7

0.4429715......................................................................................................................................54
· 100 ≈ 1.884%..............................................................................................................................54
Differential equations..................................................................................................................................58
Ordinary.......................................................................................................................................................58
5.1 EULER METHOD..........................................................................................................................58
dy = f ( x , y ), y (0) = x 0..............................................................................................................58
f ( xn , yn ) =..................................................................................................................................56
( x 0 , y 0 ),( x 1 , y 1 ),··· ,( x n , y n )..........................................................................................56
Example 5.1..................................................................................................................................56
1. Analytical Solution................................................................................................................56
Z dy
= Z 2 xdx.....................................................................................................................................56
Ln | and | = x 2 + C.........................................................................................................................56
Example 5.1 (Continued)..............................................................................................................57
Ln |1| =0+ C =⇒ C =0...................................................................................................................57
Ln | and | = x 2 =⇒ y = e x 2............................................................................................................57
2. Numerical Solution................................................................................................................57
x 1 = x 0 + h =0+0.1 =0.1.............................................................................................................57
y 1 = y 0 + f ( x 0 , y 0 ) · h = 1 + 2(0)(1) · (0.1) = 1 , which means ( x 1 , y 1 ) = (0.1, 1).......57
x 2 = x 1 + h = 0.1 +0.1 =0.2........................................................................................................57
y 2 = y 1 + f ( x 1 , y 1 )· h =1+2(0.1)(1)·(0.1)= 1.02,( x 2 , y 2 )=(0.2,1.02)..............................57
0.3 1.0608.................................................................................................................................71
0.4 1.124448.............................................................................................................................71
0.5 1.21440384.........................................................................................................................71
0.6 1.33584422.........................................................................................................................71
0.7 1.49614553.........................................................................................................................71
0.8 1.70560591.........................................................................................................................71
0.9 1.97850285.........................................................................................................................71
1 2.33463336............................................................................................................................71
3. Approximate y (0.5)...............................................................................................................71
Example 5.1 (Continued)..............................................................................................................72
1.28403 - 1.21440.........................................................................................................................72
1.28403..........................................................................................................................................72
4. Graph and comparison of results...........................................................................................72
Fourth Order Runge-Kutta Method......................................................................................................72

Department of Basic Sciences


68 Ordinary differential equations

d y
dx = f ( x , y ), y (0) = x 0............................................................................................................72
k 2 = f ( x n + h 2 , y n + h 2 k 1 ) k 3 = f ( x n + h 2 , y n + h 2 k 2 ) k 4 = f ( x n + h , y n + hk 3 )......73
6.....................................................................................................................................................73
Example 5.2..................................................................................................................................73
1. Analytical Solution................................................................................................................73
Example 5.2 (Continued)..............................................................................................................75
3. Approximate y(0.5)...............................................................................................................75
1.28403 - 1.28402.........................................................................................................................75
1.28403..........................................................................................................................................75
· 100 ≈ 0.000779%........................................................................................................................75
4. Graph and comparison of results..............................................................................................75
Introduction to Matlab.................................................................................................................................65
Matlab Environment Overview............................................................................................................65
. Save, Recall and Exit a work session.............................................................................................65
. Current Work Directory.................................................................................................................65
. Delete Work Session Variables......................................................................................................63
. Clean screen...................................................................................................................................63
. Aids................................................................................................................................................63
. Variables in use..............................................................................................................................63
Basic Constants and Operators.............................................................................................................63
a) Constants..........................................................................................................................................63
b) Operations Between Scalars............................................................................................................64
f) Useful Functions...............................................................................................................................64
A.3............................................................................................................................................................65
Elemental Functions.............................................................................................................................65
A.4............................................................................................................................................................67
Matrices and Vectors............................................................................................................................67
a) Matrices............................................................................................................................................67
1. Create a Matrix..........................................................................................................................67
Example A.1.....................................................................................................................................67
42 44 70.........................................................................................................................................67
50 51..............................................................................................................................................67
2. Modify Elements of an Array....................................................................................................67
Example A.2.....................................................................................................................................68

Department of Basic Sciences


6
9

3. Add or Delete Rows or Columns from a Matrix.......................................................................68


Example A.3.....................................................................................................................................68
Example A.4.....................................................................................................................................68
Example A.5.....................................................................................................................................69
Example A.6.....................................................................................................................................69
b) Vectors..........................................................................................................................................69
1. Vector Creation.........................................................................................................................69
Example A.7.....................................................................................................................................70
Example A.8.....................................................................................................................................70
2. Product of a Vector and a Scalar...............................................................................................70
Example A.9.....................................................................................................................................70
3. Product point.............................................................................................................................71
Example A.10...................................................................................................................................71
4. Cross Product............................................................................................................................71
Example A.11...................................................................................................................................71
c) Basic Operations Between Matrices and Between Vectors..........................................................71
Graph of Functions in 2D.....................................................................................................................72
a) In Rectangular or Cartesian Coordinates.........................................................................................72
Graph of a Simple Function.................................................................................................................72
Example A.12...................................................................................................................................72
Example A.12 (Continued)...............................................................................................................73
2. Second Option: Using the function fplot('Function',Limits,[line specifiers]) .......................73
3. Third Option: Using the function ezplot('Function',Limits,[line specifiers]) ........................73
Graph of Several Functions on the Same Plane...................................................................................73
. First Option: Use The plot() Function ........................................................................................73
Example A.13...................................................................................................................................74
Example A.14...................................................................................................................................74
Graph of a Piecewise Function.............................................................................................................74
Example A.15...................................................................................................................................75
b) In Polar Coordinates........................................................................................................................75
Example A.16...................................................................................................................................76
A.6............................................................................................................................................................76
Symbolic Calculation...........................................................................................................................76
a) Algebraic Expressions......................................................................................................................76

Department of Basic Sciences


70 Ordinary differential equations

Useful Functions About Algebraic Expression....................................................................................76


Example A.17...................................................................................................................................77
Evaluate a Symbolic Expression..........................................................................................................77
Example A.18...................................................................................................................................77
b) Solution of Polynomial Equations and Systems of Linear Equations.............................................78
Example A.19...................................................................................................................................78
Example A.20...................................................................................................................................78
c) Limit Calculation..........................................................................................................................79
Example A.21...................................................................................................................................79
3. Lim.........................................................................................................................................79
-4...................................................................................................................................................79
4. lim e - x...................................................................................................................................79
1.....................................................................................................................................................79
d) Derivative Calculus.......................................................................................................................79
Example A.22...................................................................................................................................79
d) Calculus of Integrals........................................................................................................................80
Example A.23...................................................................................................................................80
A.7............................................................................................................................................................80
Programming in Matlab.......................................................................................................................80
The Function and Script Editor............................................................................................................80
How do we access the editor?...........................................................................................................81
How do you run a script?..................................................................................................................81
How is a function executed?.............................................................................................................81
How do you program in Matlab?......................................................................................................81
a) IF Control Statement.....................................................................................................................82
b) SWITCH Control Statement.........................................................................................................83
c) FOR Repetition Sentence..............................................................................................................83
d) WHILE Repetition Sentence.........................................................................................................83
e) Creation of Functions and Scripts.................................................................................................84
. Features..............................................................................................................................................84
Example A.24...................................................................................................................................84
Example A.25...................................................................................................................................85
Example A.26...................................................................................................................................85
Example A.27...................................................................................................................................86

Department of Basic Sciences


7
1

. Scripts.................................................................................................................................................86
Example A.28...................................................................................................................................87
Example A.29...................................................................................................................................87
Bibliography................................................................................................................................................90

0.3 1.0608
0.4 1.124448
0.5 1.21440384
0.6 1.33584422
0.7 1.49614553
0.8 1.70560591
0.9 1.97850285
1 2.33463336

3. Approximate y (0.5)

From the analytical solution y (0.5) = e (0.5)2 = 1.28403

Numerical analysis

Department of Basic Sciences


72 Ordinary differential equations

Example 5.1 (Continued).

From the numerical solution y (0.5) = 1.2144

The relative percentage error that was committed when applying Euler's formula is given by:

1.28403 - 1.21440
Er= · 100 ≈ 5.423%
1.28403

4. Graph and comparison of results

Fourth Order Runge-Kutta Method


5.2
The Runge-Kutta is not only a method but an important family of iterative methods, both implicit and explicit, to
approximate the solutions of ordinary differential equations (ODE). These techniques were developed around 1900 by
the German mathematicians Carl David Tolmé Runge and Martin Wilhelm. Kutta.

The classic fourth-order Runge-Kutta method:

A member of the family of Runge-Kutta methods is so commonly used that it is often referred to as RK4 or the
Runge-Kutta method .

Let's define an initial value problem as:

d y
dx = f ( x , y ), y (0) = x 0

Numerical analysis

Department of Basic Sciences


7
3

Then the RK4 method for this problem is given by the following equation: y n + 1 = y n + h 6 [ k 1 + 2 k 2 + 2 k 3 + k 4 ] , from which:
k1=f(xn,yn)
k 2 = f ( x n + h 2 , y n + h 2 k 1 ) k 3 = f ( x n + h 2 , y n + h 2 k 2 ) k 4 = f ( x n + h , y n + hk 3 )

Thus, the next value y n +1 is determined by the present value y n plus the product of the size of the interval h times an estimated
slope. The slope is a weighted average of slopes:

k 1 is the slope at the beginning of the interval.

k 2 is the slope at the midpoint of the interval, using k 1 to determine the value of y at the point x n + h 2 using Euler's method.

k 3 is again the slope of the midpoint, but now using k 2 to determine the value of and k 4 is the slope at the end of the interval,
with the value of y determined by k 3 .

By averaging the four slopes, the greatest weight is assigned to the slopes at the midpoint:

k1+2k2+2k3+k
4 Slope =

6
The RK4 method is a fourth order method which means that the error per step is of the order of h5, while the total accumulated
error is of the order h4.

Example 5.2

Given the following differential equation y = 2 xy with the initial condition: y (0) = 1 ,

1. Solve it analytically.

2. Use the fourth-order Runge-Kutta Method on the interval from x = 0 to x = 1 with step size h = 0.1 .

3. Approximate y(0.5).

4. Graph and compare the results.

1. Analytical Solution

dy 2 y = ex
= 2xy dx

Numerical analysis

Department of Basic Sciences


74 Ordinary differential equations

Example 5.2 (Continued).

2. Numerical Solution

We apply the fourth-order Runge-Kutta method between 0 and 1 with step size h = 0.1 .

Initial condition:

x = 0 -→ y = 1, ( x0 , y0 ) = (0, 1)

x 1 = x 0 + h =0+0.1 =0.1

We apply the equations of the method:

For f ( x , y ) = 2xy

k 1 = f ( x 0 , y 0 )= f (0,1)=2(0)(1)=0

k 2 = fx 0 + h 2, y 0 + h 2 k 1 = f 0 + 02.1 , 1 + 02.1
= f (0.05, 1) = 2(0.05)(1) = 0.1
k 3 = f (X 0 + 2,, 0 + hk 2) = f (o + 9,, 1 + 02 1

k 4 = f ( x 0 + h , y 0 + hk 3 ) = f (0 + 0.1, 1 + (0.1) = f (0.05, 1.005) = 2(0.05)(1.005) = 0.1005


(0.1005)) = f (0.1, 1.01005) = 2(0.05)(1.01005) = 0.20201

Now we substitute into the recursive formula y n +1 = y n + h 6 [ k 1 + 2 k 2 +2 k 3 + k 4 ] :


y 1 = y 0 + h ( k 1 +2 k 2 +2 k 3 + k 4 ) = 1+ 0 . 1 (0 + 2(0.1) + 2(0.1005) + (0.20201)) = 1.0100502 66
We summarize all the results in the following table:
xi k1 k2 k3 k4 and i and analytics
0 1 1
0.1 0 0.1 0.1005 0.20201 1.0100502 1.0100502
0.2 0.202010033 0.3060452 0.3076057 0.416324296 1.0408108 1.0408108
0.3 0.416324308 0.5308135 0.5336757 0.656507005 1.0941743 1.0941743
0.4 0.656504559 0.7888996 0.7935335 0.93882209 1.1735108 1.1735109
0.5 0.938808651 1.0984061 1.105588 1.284069614 1.2840253 1.2840254
0.6 1.284025256 1.4830492 1.4939955 1.720109765 1.433329 1.4333294
0.7 1.719994793 1.9751274 1.991711 2.285500128 1.6323152 1.6323162
0.8 2.285241262 2.6198659 2.6449627 3.034898335 1.8964785 1.8964809
0.9 3.034365548 3.4819345 3.5199778 4.047257249 2.2479026 2.247908
1 4.046224662 4.6554063 4.7132785 5.438460884 2.7182702 2.7182818

Numerical analysis

Department of Basic Sciences uts


7
5

Example 5.2 (Continued).

3. Approximate y(0.5)

From the analytical solution y (0.5) = e (0.5)2 = 1.28403

From the numerical solution y (0.5) = 1.28402

The relative percentage error that was committed when applying the RK4 formula is given by:

1.28403 - 1.28402
Er= · 100 ≈ 0.000779%
1.28403

4. Graph and comparison of results

Numerical analysis
Department of Basic Sciences
Introduction to Matlab

The name MatLab is an abbreviation of the words MATrix LABoratory. MatLab is an interactive system tive for
scientific and engineering calculations based on the matrices. With it you can solve complete fixed numerical
problems without having to write a specific program for it, although it is also possible to program. In addition, the
Matlab program has, depending on the version, different modules (Toolboxes) that allow you to solve specific
problems.

In Matlab, commands are entered by writing them one by one after the prompt ( >> ) that appears in the Command
Window.

Matlab is case sensitive, that is, the variable A is different from the variable a .

A.1 Matlab Environment Overview

. Save, Recall and Exit a work session

? save: Stores the variables in a .mat file. For example:

> > save test

? load: Load variables and their content. For example:

> > load test

? delete: Delete file. For example:

> > delete test.mat

? quit: To end the session with Matlab.

. Current Work Directory

? pwd: Shows what the current directory is.


? cd: Change the path of the current directory.

> > cd c:\matlab\work

Numerical analysis

Department of Basic Sciences uts


6
3

? dir: Lists the contents of the current directory.

. Delete Work Session Variables

The functions that allow you to eliminate variables are:

? clear [var 1 , var 2 , ..., var n ]: Delete the indicated variables.


? clear all: Deletes all variables from the current session.

. Clean screen

To clear the screen you must type the clc function on the command line ( >> ).

. Aids

The functions that help us to request help from Matlab are:

? help Function_Name: Displays the help associated with Function_Name in the Command Window.

? helpwin Function_Name: Displays the help associated with Function_Name in a floating window.

. Variables in use

The following functions allow us special treatment with session variables:

? who: Tells us which variables are in use.

? whos: It tells us the same thing as who , but it also tells us the size and type of the variable.

? ans: It is the variable that is used in the results. In the next operation you can recover this result by retyping ans .
This variable will be modified as soon as there is a new result.

Basic Constants and Operators


A.2
a) Constants
Constant Meaning
pi 3.14159265...
Yo The imaginary unit, - 1.
j The same as i.
eps Relative precision of floating point numbers, 2 52 = 2 . 204 e - 16.
-

realmin Smallest floating point number, 2 1022 .


-

realmax Largest floating point number, ( 2 - eps ) 2 1 023.


info Infinite. It is produced by dividing a non-zero number by zero.
nan "Not-A-Number." It occurs when evaluating expressions such as 0/0 or inf-inf.

Numerical analysis

Department of Basic Sciences


64 Introduction to Matlab

b) Operations Between Scalars


Operator Operation Example Result
+ Addition 2+3 5
- Subtraction 2-3 -1
* Multiplication 2 ∗ 3 6
/ Division 2/3 0.6667
^ 2ˆ3 Boost 8

c) Complex Numbers
Function Description Example Result
abs(z) Returns the magnitude of z , where z is a complex number z=3+4i; abs(z) 5
angle(z) Returns the phase angle in radians of a complex number z z=1+i; angle(z) 0.7854
conj(z) Returns the conjugate of z z=3+4i; conj(z) 3.0000 - 4.0000i
image(z) Returns the imaginary part of z z=3+4i; image(z) 4
real(z) Returns the real part of z z=3+4i; real(z) 3
d) Relational Operators
Relational Operator Meaning
< smaller than
> greater than
<= less than or equal
>= greater than or
equal
equal
==
∼= distinct

e) Logical Operators

Operator Meaning &Y | Or ∼ NO

f) Useful Functions
. vpa(expr,n): Arithmetic Precision Variable, allows calculating the expr with n number of digits.

. sum(V): Add the elements of a vector V.

. roots(): Calculates the roots of a polynomial.

. length(V): Calculates the number of elements of a vector V.

. rats(x): Expresses any decimal number x in rational form.

. deconv(): Calculates division of polynomials.

Numerical analysis

Department of Basic Sciences


6
5

Function Name Example Result


abs(x) Absolute value of x or Module of x , if x is a complex number a)abs(-1) b)abs(3+4i) a)1.7 b)5
acos(x) Arc cosine of x acos(1) -6.1257e-17
acot(x) Cotangent arc of x acot(10) 0.0997
acoth(x) Hyperbolic cotangent arc of x acoth(5) 0.2027
acsc(x) Cosecant arc of x acsc(4) 0.2527
acsch(x) Hyperbolic cosecant arc of x acsch(0.2) 2.3124
asec(x) Arc secant of x asec(2) 1.0472
asech(x) Hyperbolic arc secant of x asech(0.5) 1.3170
asin(x) Arc cosine of x asin(1) 1.5708
atan(x) Arc tangent of x atan(1) 0.7854
atan2(y,x) Arc tangent of y x atan2(0,-1) 3.1416
ceil(x) Rounding to plus infinity of x a)ceil(-7.3) b)ceil(7.3) a)-7 b)8
cos(x) Cosine of x cos(0) 1
cosh(x) Hyperbolic cosine of x cosh(3) 10.0677
cot(x) Cotangent of x cot(3) -7.0153
coth(x) Hyperbolic cotangent of x coth(5) 1.0001
csc(x) cosecant of x csc(10) -1.8382
csch(x) Hyperbolic cosecant of x csch(2) 0.2757
exp(x) Exponential of x exp(1) 2.7183
fix(x) Rounding towards zero of x a)fix(2.7) b)fix(-2.7) a)2 b)-2
floor(x) Rounding down to minus infinity of x floor(-5.8) b)floor(5.8) a)-6 b)5
log(x) natural logarithm log(2.7183) 1.0000
log10(x) Logarithm to base 10 log10(350) 2.5441
mod(x,y) Module (integer quotient of division x and ) a)mod(7,3) b)mod(7,-3) a)1 b)-2
pow2(x) Power in base 2 of x pow2(4) 16
rem(x,y) Integer remainder from the division of x and a)rem(7,3) b)a)rem(7,-3) a)1 b)1
round(x) Rounding to the nearest integer of x a)round(7.3) b)round(7.7) a)7 b)8
sec(x) secant of x sec(5) 3.5253
sech(x) Hyperbolic secant of x sech(1) 0.6481
sign(x) Sign of x function a)sign(-7) b)sign(7) a)-1 b)1
sin(x) Sine of x sin(pi/6) 0.500
sinh(x) Hyperbolic sine of x sinh(3) 10.0179
sqrt(x) square root of x sqrt(2) 1.4142
so(x) Tangent of x tan(pi/4) 1.000
tanh(x) Hyperbolic tangent of x tanh(3) 0.9951

Department of Basic Sciences


66 Introduction to Matlab

A.3 Elemental Functions

Numerical analysis

Department of Basic Sciences


6
7

A.4 Matrices and


Vectors

a) Matrices
1. Create a Matrix

Matlab conceives a matrix as an array of n rows with m columns.

Example A.1

Write the matrix in matlab


42 44 70
M=
50 51

It would be written like this:


>> M=[42,44,70; 50,51,5]

It can also be written in the following way:


>> M=[42 44 70; 50 51 5]

The ";" symbol indicates that the column ends and the entry for a new one is given. As can be seen in the
examples in the last column, it is not necessary to type it.

It can also be observed that instead of writing commas to separate the elements of a row, it is enough to leave
a space between them.

2. Modify Elements of an Array

To Modify an element of an array, simply write the absolute position of the element you want to access.

Numerical analysis

Department of Basic Sciences


68 Introduction to Matlab

Example A.2

Modify the element at position (2,3) of the previously created matrix M by the quantity 10.

This would be done using the command:


>> M(2,3)=10

3. Add or Delete Rows or Columns from a Matrix

. To add a row to a matrix you must write it in the following format:

CurrentArray = [CurrentArray; NewRow] or CurrentArray = [NewRow; CurrentMatrix]

Example A.3

Add the row [1 5 9] to the end of the matrix M, created at the beginning of the matrices section.

The order would be:


>> M=[M;[1 5 9]]

Please note that the ";" symbol is mandatory. .

To add a column to a matrix you must write it in the following format:

CurrentArray = [CurrentArray NewColumn]


either
CurrentArray = [NewCurrentArrayColumn]

Example A.4

Add column [1; 5] to the matrix M , created at the beginning of the matrices section.

The following order must be written:


>> M=[[1; 5M]

Note that it is mandatory to leave a space between the new column and the current matrix .

Numerical analysis

Department of Basic Sciences


6
9

To delete a row from a matrix you must write it in the following format:

CurrentArray(RowNumber,:)=[]

Example A.5

Delete the second row of the matrix M , created at the beginning of the matrices section.

The following order must be written:


>> M(2,:)=[]

To remove a column from a matrix you must write it in the following format:

CurrentArray(:,ColumnNumber)=[]

Example A.6

Delete the third column of the matrix M , created at the beginning of the matrices section.

The following order must be written:


>> M(:,3)=[]

b) Vectors
Matlab defines vectors as matrices, in the case of a row vector it defines it as a matrix of 1 row by n columns, in the
case of a column vector, it defines it as a matrix of n rows by one column.

1. Vector Creation

To create a vector it is only necessary to write each component inside brackets separated by commas or
spaces in the case of a row vector, and ";" in the case of being a column vector.

Numerical analysis

Department of Basic Sciences


70 Introduction to Matlab

Example A.7

Write the vector V = 3 i - 2 j + 4 k and the vector W =

It would be written like this:


>> V=[3 -2 4]
>> W=[4; 5; 6]

To create vectors that preserve the distance from one element to another, such as V=[0 0.1 0.2 0.3 0.4 0.5], it is done using
the syntax:

NewVector=Start:Increment:end

Another way to do the same is with the linspace function, whose syntax is:

linspace(Start, End, Integer points)

Example A.8

Use a command in Matlab that allows you to construct the vector V=[0 0.1 0.2 0.3 0.4 0.5] automatically.

It would be written like this:


>> V=0:0.1:0.5

You can also write:


>> V=linspace(0,0.5,6)

2. Product of a Vector and a Scalar

Example A.9

Calculate w = 4 v - 9 u , if u = 3 i + 5 j and v = 6 i - 2 j .

It would be written like this:


>> u=[3 5]; v=[6 -2]; w=4 * v-9 * u

Numerical analysis

Department of Basic Sciences


7
1

3. Product point

To calculate the dot product between two vectors, simply use the function dot( V 1 , V 2 ) , where V 1 and V 2 are
vectors with the same number of components.

Example A.10

Calculate the dot product between the vectors V = 3 i + 4 j + 5 k and W = - 2 i + 9 j - 3 k .

It would be written:

>> V=[3 4 5]; W=[-2 9 -3]; cross(V,W) -

4. Cross Product

To calculate the cross product between two vectors, simply use the function cross( V 1 , V 2 ) , where V 1 and V 2
are vectors with the same number of components.

Example A.11

Calculate the cross product between the vectors V = 3 i + 4 j + 5 k and W = - 2 i + 9 j - 3 k .

It would be written like this:


>> V=[3 4 5]; W=[-2 9 -3]; dot(V,W)

c) Basic Operations Between Matrices and Between Vectors

Operator Operation Example Result


+ Addition [2 3]+[2 4] [4 7]
Subtraction [2 3]-[24] [ 0 -1]
-
42 44
* Multiplication [1 3 4; 4 2 5] ∗ [4 3; 2 7; 8 5] 50 51
.* Term-by-term multiplication [2 3].∗[24] [4 12]
./ Term-by-term division [2 3]./[2 4] [1 0.7500]
.^ Term-by-term enhancement [2 3].ˆ2 [4 9]

Numerical analysis

Department of Basic Sciences


72 Introduction to Matlab

Graph of Functions in 2D
TO
5
a) In Rectangular or Cartesian Coordinates

Graph of a Simple Function

To graph functions of a variable with MatLab, we can use the plot() , fplot() and ezplot() functions.

Example A.12

Draw the graph of the function y = sin ( x ) for x ∈ [ 0 , 2 π ] .

To solve the problem we have:

1. First Option: Using the plot( V x , V y ) function .

. First we create a table of values for x :

>> x=0:pi/100:2*pi;

With this command we have formed a table (the vector x ) with 200 values between 0 and 2 π . Another way to achieve
the same result would be to use the command:

>> x=linspace(0,2*pi,200);

. Now we calculate the values of y :

>> y=sin(x);

. Finally we draw it using the plot( V x , V y ) function:

>> plot(x,y), grid on

With what you get:

1 0.8 0.6 0.4 0.2

-0.2

-0.4

-0.6

-0.8

-1

01234567

Numerical analysis

Department of Basic Sciences


7
3

Example A.12 (Continued).

2. Second Option: Using the function fplot('Function',Limits,[line specifiers]) .

Just write:
>> fplot('sin(x)',[0,2*pi]), grid on

With which the same previous graph is obtained.

3. Third Option: Using the function ezplot('Function',Limits,[line specifiers]) .

Just write:
>> ezplot('sin(x)',[0,2*pi]), grid on

With which you obtain graph:

0123456
x

Graph of Several Functions on the Same Plane

Two options can be considered:

. First Option: Use The plot() Function .

The function can be used using the syntax:

plot( x 1 , y 1 , x 2 , y 2 , · · · , x n , y n ) ,

where each pair x i and y i are the vectors that represent a function.

Numerical analysis

Department of Basic Sciences


74 Introduction to Matlab

Example A.13

Graph the functions y = sin ( x ) and y = sin ( x + 3 π ) in the same plane.


>> x=linspace(0.2 * pi,300);
> > y=sin(x);
> > z=sin(x+pi/3);
> > plot(x,y,'r-',x,z,'b-'),grid on

. Second Option: Use the hold Function .

The hold function allows you to lock ( on ) or unlock ( off ) the current figure so that more than one function can be
graphed.

Example A.14

Graph the functions y = sin ( x ) and y = sin ( x + 3 π ) in the same plane.


>> fplot('sin(x)',[0,2*pi],'r'), grid on, hold on
>> fplot('sin(x+pi/3)',[0,2*pi],'b')

With both options the resulting graph is:

Graph of a Piecewise Function

To draw graphs of piecewise defined functions, we need to use what we are going to call indices or logical variables. To create
these logical variables, relational operators seen in the first section can be used.

Numerical analysis

Department of Basic Sciences


7
5

Example A.15

Graph the function

( x2 Yea x < 0
f(x) = < 1hYea 0 ≤ x < 1
t— X + 2
h
Yea x ≥ 1
You should write: h

>> x=linspace(-2,3,3000);
>> y=(x.^2). * (x<0)+1. * ((0<=x)&(x<1))+(-x+2). * (1<=x);
>> plot(x,y),grid on,title('Piecewise function')
With what you get:

b) In Polar Coordinates
A curve in polar coordinates is the image of the function

r=h(θ),θ∈[θ1,θ2]

A point on the curve in polar ( r 0 , θ 0 ) has distance to the origin r 0 and the angle formed by the position vector tion of
the point with the horizontal axis, measured in the positive direction, is θ .

Therefore, the relationship between polar coordinates and parametric coordinates is

x = r cos ( θ )
y = r sin ( θ )

To draw a polar curve with MatLab, use the polar() function.

Numerical analysis

Department of Basic Sciences


76 Introduction to Matlab

Example A.16

Graph of r = 2 - 4 cos ( θ ) , -π ≤ θ ≤ π.

You should write:


>> tetha=linspace(-pi,pi,100);
>> r=2-4 * cos(tetha);
>> polar(tetha,r)

A.6 Symbolic
Calculation
Whenever you need to work with symbolic objects or variables, you need to use the syms function to declare them beforehand.
It should be noted that it is enough to declare them only once during the entire work session.

It is highly recommended! than when working with algebraic expressions, please! don't forget the op basic erators, for example,
if you want to calculate ( 4 x - 5 y ) · ( 256 x - 4 y ) , it would NOT be good to write the following in Matlab:

>> (4x-5y) * (256x-4y)

The correct writing would be:

>> (4*x-5*y)*(256*x-4*y)

That is, the coefficients are followed by the multiplication operator (*).

a) Algebraic Expressions

Useful Functions About Algebraic Expression

Matlab has a set of functions that make it easier for us to process algebraic expressions, among them we find:

. collect() : Collects equal terms.

. expand() : Expands the products into sums.

. factor() : Factors the expression (sometimes) if the argument is a symbolic function. If it is a number, it provides
factorization into prime numbers.

. simplify() - Simplifies an expression by applying various algebraic identities.

. simple() : Use different simplification tools and select the form that has the least number of characters.

Numerical analysis

Department of Basic Sciences


7
7

pretty() : Displays the expression in a manner similar to that used in regular writing.

Example A.17

Let A = 2 x + 3 x 2 y and B = 3 x 2 y 4 - 5 x + 4 y + 4 . Calculate:

1. C = A + B 2. C = A - B 3. C = A 2 - B 3 4. C = A ∗ B

You could write:


> > syms xy
> > A=2*x+3*x^2*y
> > B=3*x^2*y^4-5*x+4*y+4
> > C=expand(A+B)
> > C=expand(AB)
> > C=expand(A^2-B^3)
> > C=expand(A*B)

Evaluate a Symbolic Expression

To evaluate you only need to use the subs() function.

Example A.18

Construct the symbolic expression y = sin ( x ) + 3 x + x +


8
1 and substitute it when x = 3 .

You should write:

> > syms x


> > y=sin(x)+3^x+8/(x+1)
> > subs(y,x,3)

Another alternative option is using the inline() function, i.e.


> > y=inline('sin(x)+3^x+8/(x+1)')
> > and(3)

Numerical analysis

Department of Basic Sciences


78 Introduction to Matlab

b) Solution of Polynomial Equations and Systems of Linear Equations


The function that allows us to solve both Polynomial Equations and Systems of Linear Equations is the solve() function.

Let's see its use through the following examples.

Example A.19

Determine the roots of the equation 4 x 5 - 7 x 2 - 2 x - 5 = 0 .

It could be written like this:

>> solve(4*x^5-7*x^2-2*x-5)

or also like this:


>> solve('4*x^5-7*x^2-2*x-5=0')

Example A.20

Solve the system of linear equations:

x+4y-z=6
2 x + 2 y - 14 z = - 36
x + 8 y + 4 z = 29

It could be written like this:

>> [x,y,z]=solve('x+4*yz=6','2*x+2*y-14*z=-36','x+8*y+4*z= 29')

Another alternative way to solve the system would be using the linsolve() function. To use it, the system must be written in its
matrix form, that is, A ∗ X = B , where A represents the coefficient matrix of the system, B represents the results vector and X
represents the vector of unknowns.

Let's solve the above system using the linsolve() function:

>> A=[1 4 -1; 2 2 -14; 1 8 4]


>> B=[6; -36; 29]
>> X=linsolve(A,B)

Numerical analysis

Department of Basic Sciences


7
9

c) Limit Calculation
The function indicated to calculate all types of limits in Matlab is limit() .

See the following example.

Example A.21

Calculate the following limits:


sin ( x ) 2. lim ln+ ( x ) x 2 -4 4. lim e x 1
1. lim
-

x→ x→0 3. Lim 5. lim


0 x →- ∞ x
x→ 2 -
x-2

It could be written like


this:

>> syms x
>> limit(sin(x)/x,x,0)
>> limit(log(x)/x,x,0,'right') limit((x^2-4)/(x-
>> 2),x,2,'left') limit(exp(-x),x ,inf)
>> limit(1/x,x,-inf)
>>

d) Derivative Calculus
The indicated function to calculate derivatives in Matlab is diff() .

See the following example.

Example A.22

Let f ( x ) = cos ( 4 x 2 + 5 ) . Calculate: a) f ( x ) b) f ( 3 ) ( x )


0

In Matlab you could write like this:

>> syms x
>> diff(sin(4*x.^2+5))
>> diff(sin(4*x.^2+5),3)

Numerical analysis

Department of Basic Sciences


80 Introduction to Matlab

d) Calculus of Integrals
The indicated function to calculate integrals in Matlab is int() .

See the following example.

Example A.23

Calculate the following integrals:


3. Z +∞
1. cos ( x ) dx 0
x dx 4. sec 3 x dx
2. e 0

It could be written like this:

>> syms x
>> int(cos(x))
>> eval(int(exp(sqrt(x))/sqrt(x),x,1,2)) eval(int(exp(-
>> x),x,0,inf))
>> eval(int((sec(x))^3,x,0,1))

A.7 Programming in Matlab

This is an introduction to programming scripts and functions in Matlab. The first question that may arise is what is a script? .
This English term means: writing, script, note; The term script is the one most used in Spanish translations.

Let's remember that in Matlab we work on the Workspace, which is the initial window where we enter commands and execute
them directly. Frequently, a series of commands must be executed several times during the same session; scripts exist to save
us the trouble of continually entering them.

The Function and Script Editor

Functions and scripts are nothing more than ASCII text files, with the extension *.m, containing definitions tion of functions or
sets of commands respectively.

The editor allows you to create and modify these files, as well as execute them step by step to see if they are have errors
(Debug or debugging process, eliminate errors from the program). Matlab also allows us to use any editor (DOS edit, Word,
Notepad, etc.), since the files are only text. The following graphic shows the main Editor/Debugger window.

Numerical analysis

Department of Basic Sciences


8
1

Finally we answer the following questions:

How do we access the editor?

. From the Workspace: » edit.

. From the Menu bar: File -> New -> M-file.

How do you run a script?

You simply have to enter its name on the command line or through the editor.

How is a function executed?

In the same way as a script but its arguments must be passed between parentheses and separated by commas.

Example: my_function(arg1, arg2, ..., argn)

How do you program in Matlab?

The answer to this question is not simple, we will answer it in several steps. Let's know some things regarding the
fundamental variables and statements that we can use in our programs.

Numerical analysis

Department of Basic Sciences


82 Introduction to Matlab

Variable: a variable is made up of a space in the storage system (main memory of a computer) and a symbolic name (an
identifier) that is associated with said space. That space with It has a known or unknown quantity or information, that is, a value.

For example, in Matlab when V=5 is written, it indicates that our letter V becomes a variable that has stored a quantity of 5.

In programming it is common to use many variables, these allow us to create large scripts and functions, which are very useful.

Below are the fundamental sentences: IF , SWITCH , FOR and WHILE .

a) IF Control Statement
In its simplest form, the if statement is described as follows:

if condition statements
end

Note that the condition is not enclosed in parentheses.

There is also multiple branching , in which as many conditions as desired can be concatenated, and which has the form:

if condition_1
block1
elseif condition_2
Block 2
elseif condition_3
block 3

elseif condition_n
n else block
default block end

Note: The use of else is optional. It is used as a default option for when other conditions are not met. tions.

Numerical analysis

Department of Basic Sciences


8
3

b) SWITCH Control Statement


This statement performs the function analogous to a concatenated if...elseif set. Its general form is:

variable switch
case expr_1, block 1
case {expr_2, expr_3, expr_4, ...}, block 2

otherwise, default block end

Note: As with else, the use of otherwise is optional.

At first the variable is evaluated, the result of which must be a scalar number or a string of characters. This result is
compared with the expr_n , and the block of statements that corresponds to that re is executed. sultado. If none is
equal to variable, the statements corresponding to otherwise are executed. It is possible to put multiple expr_i
inside curly braces, as seen above.

c) FOR Repetition Sentence


The for statement repeats a set of statements a predetermined number of times. The next one with struction executes
statements with values of i from 1 to n , varying by one:

for i=1:n statements


end

For nested for :


for i=1:n
for j=1:m statements
end
end

d) WHILE Repetition Sentence


The syntax of the while statement is as follows.

while condition end statements

Numerical analysis

Department of Basic Sciences


84 Introduction to Matlab

Where condition can be a vector or matrix expression. The statements continue to be executed as long as there are non-zero
elements in the condition , that is, as long as there are some true elements. The loop ends when all condition elements are
false (that is, zero).

e) Creation of Functions and Scripts


. Features

The functions allow you to define functions entirely analogous to those of Matlab, with their name, their arguments and
their output values.

Every function in Matlab must have the following syntax:

function [output values]=functionname(input arguments)


.
. Function body
.
end

It is possible to write comments inside a function using the % symbol. To use it, just write the symbol and then write the
comment you want. For example, inside some function you could write:

% The variable X stores the number of years of the person.

It is highly recommended! save the function with the same name assigned to it within the editor, otherwise Matlab will
not be able to execute it.

Below are a series of examples of functions:

Example A.24

Create a function that allows you to calculate the surface area of a rectangular box.

function [as] = BoxSurfaceArea(x,y,z) as=2 * x * y+2 * x * z+2 * y * z;


end

To execute the function, you should write for example:

>>BoxSurfaceArea(3,4,7)

Numerical analysis

Department of Basic Sciences


8
5

Example A.25

Create a function that allows you to decide the largest between two numbers.
function [m] = greater(a,b) if a>b
m=a;

elseif b>a
m=b;
else
m=a;

end
end

To execute the function, you should write for example:

>> major(3.5)

Example A.26

Create a function that allows defining the mathematical model:

( 3 if x = 1
ym = 2 x + 1 if x = 2 , 3 , 4
( ex yes another x
The function would be:
function [ r ] = ym(x) switch x
case 1, r=3;
case {2,3,4}, r=2*x+1;
otherwise r=exp(x); end
end

To execute the function, you should write for example:


>> ym(3)

Numerical analysis

Department of Basic Sciences


86 Introduction to Matlab

Example A.27

Create a function to which any real number is entered and it calculates the sum of the absolute values of the sine function
obtained recursively, until said sum is greater than 2 and returns said sum.

For example, let x 0 = 5 , then

x 1 = | sine ( 5 ) | = 0 . 9589
x2 = | sine ( 0 . 9589 ) | = 0 . 8186
x 3 = | sine ( 0 . 8186 ) | = 0 . 7302

now,

sum= x 1 + x 2 + x 3 = 0 . 9589 + 0 . 8186 + 0 . 7302 = 2 . 5077 > 2

The function should return: 2 . 5077 .

The function would be:

function [sum] = SumMinorQueTwo(x)


sum=0;
while(sum<=2)
x=abs(sin(x));
sum=sum+x;
end
end

To execute the function, you should write for example:

>> SumMenorqueTwo(5)

. Scripts

Unlike functions, scripts do not require a specific syntax, but rather a set of commands with a logical sequence.

Like the function, comments can also be added to the script using the % symbol.

See the two examples below.

Numerical analysis

Department of Basic Sciences


87

Example A.28

Create a script that asks indefinitely for a number other than zero, adding all these numbers until zero is entered. Then
show said accumulated sum.
x=input('Enter number: ');
sum=0;
while(x~=0)
sum=sum+x;
x=input('Enter number: '); end
disp('The sum is: '); addition

To run the script, you must first save it, in this case it will be called SumAcumulada.m , then you should write on the
command line:

>>AccumulatedSum

Example A.29

Create a script that calculates the sum of all the squares of the odd numbers between two given different natural
numbers and then displays it.
x=input('Enter first number: ');
y=input('Enter second number: ');
sum=0;
for i=x:1:y
if mod(i,2)==1
sum=sum+i^2;
end
end
disp('The sum is: '); addition

To run the script, you must first save it, in this case it will be called SumaCuadrados.m , then you should write on the
command line:

>>SumSquare

Numerical analysis

Department of Basic Sciences


Bibliography

[1] Steven Chapra, 2007. Numerical Methods for Engineers , McGraw Hill, 5th Edition.

[2] Neptalí Franco, 2008. Study Guide for Mathematics Curricular Unit V , Universidad Na tional Experimental
Francisco de Miranda.

[3] Jorge Velásquez Z, 2007. Numerical Analysis: Class Notes , Uninorte Editions, 1st Edition.

[4] Carlos J. Pes R, 2013. IEEE Standard 754 , http://www.carlospes.com/curso_representacion_datos/


06_01_estandar_ieee_754.php.

[5] Ivan F. Asmar Ch, 1999. Numerical Methods, A First Course , http://unalmed.edu.co/ifas-mar/libro.html .

[6] Carlos A. Alvarez H., Fernando Ceballos, 2006. Analysis Numeric ,


http://aprendeenlinea.udea.edu.co/lms/moodle/course/view.php?id=229 .

Numerical analysis

Department of Basic Sciences

You might also like