Professional Documents
Culture Documents
Numerical Analysis
Numerical Analysis
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....................................................................
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...........................................................................................................................................
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......................................................................................................................
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)............................................................................................
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. 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
• 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
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 (%).
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.
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
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 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
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 .
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.
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
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
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
Numerical analysis
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
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:
It can be shown that symmetrical rounding generally leads to more accurate results.
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
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.
Example 1.5
-0.00523 = (-l)(O.523) lo x 10 2
l|l|2|0|0|0|0|0|0|0|0|0|0|5|2|3
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
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:
So that,
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
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 :
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
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=((20+2-1)×2-3)10=((1+0,5)×0,125)10=(1,5×0,125)10=0,187510
Therefore,
Numerical analysis
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)(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
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
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
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:
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
0011100100000000000000000000000000000000000000000000
Numerical analysis
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
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:
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 :
8 0 5 c 0 0 0 0
1000 0000 0101 1100 0000 0000 0000 0000
Numerical analysis
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:
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.
Numerical analysis
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:
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
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
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.
Numerical analysis
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.
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
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].
f ( x3 ) = f ( 1.5 ) = -0.1823
Numerical analysis
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
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].
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].
Numerical analysis
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.
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 )
(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
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].
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
f ( x4 ) = f ( 0.5231 ) = 0.0051
Numerical analysis
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
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.
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
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
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:
| g 0 ( x )| < 1, ∀ x ∈ ( c , d )
xi+1=g(xi)
until x i +1 is exactly g ( x i ) .
Numerical analysis
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 .
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
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 .
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.
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
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)
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.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.
Numerical analysis
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
)
f ( x i )( x i - x i - 1 )
xi +1= xi -
f(xi)-f(xi-1)
Numerical analysis
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.
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
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.
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 )
n i-1
P n ( x ) = b 0 +∑ b i ∏( x - x j )
i=1 j=1
Numerical analysis
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 ]
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
Determine the Newton Interpolant polynomial containing the points (3,-1), (5,-3), (7,9) and (8,6).
For ease of understanding the Newton interpolating polynomial algorithm using divided differences, let's do the construction by
levels or columns:
Level b 1 :
Numerical analysis
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
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 )
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.
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
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
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
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:
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
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
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
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
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:
For the equation E 2 , take the pairs (0.6, 0.55), (1.2, 0.211) and (1.4, 0.2) :
For the equation E 3 , take the pairs (1.2, 0.211), (1.4, 0.2) and (1.8, 0.44) :
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
Numerical analysis
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
k 1 = -4, 9588
k 2 = 2, 6128
k 3 = 3, 4740
k4 = 0.2713
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
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:
+ 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
To determine P 3 ( x ) , take the rows [1.2, 0.211, 2.6128] and [1.4, 0.2, 3.4740] :
Numerical analysis
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)
To determine P 5 ( x ) , take the rows [1.8, 0.44, 0.2713] and [2.23, 0.83, 0] :
Therefore the set of all polynomials that fit the data of the initial table, with their respective domains are:
Numerical analysis
-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]
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
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.
ε = 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
= 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
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
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.
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
Numerical analysis
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:
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
Criteria:
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
5(260) - (16)(47)
a =2
5(106) - (16)2
(106)(47) - (206)(16)
b= 5(106) - (16)2 b =3
Numerical analysis
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:
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
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.
Cut Point
Log 10 y = z Log 10 α{ + β Log 10x
| {z} |{z} | {z}
New Var Dep Pte Nueva Var Ind
Numerical analysis
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
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
β = 1.999947
y = 2.9963 x 1.999947
Numerical analysis
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.
. Forward:
f 0 ( x i )' -3 f ( x i )+4 f ( x i + h )- f ( x i +2 h ) + O ( h 2 )
. Backward:
2 hours
f 0 ( x i )' f ( x i + h )- f ( x i - h ) + O ( h 2 )
. Central:
Numerical analysis
. 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 .
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
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.
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.
Consider the function f ( x ) , whose graph is between the extremes x = a and x = b as shown in the figure.
Numerical analysis
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
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=
0.4429715 - 0.5235988
0.4429715
Numerical analysis
· 100 ≈ 18.2%
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
h n-1
I = 2 f ( x 0 )+2∑ f ( x i )+ f ( x n )
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
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
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
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
yn +1 - yn
f ( xn , yn ) = yn = yn + f (xn , yn ) · h
h
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 )
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).
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
Ln |1| =0+ C =⇒ C =0
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
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
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
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
- 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
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
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
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
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
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
. 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)
Numerical analysis
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
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 .
d y
dx = f ( x , y ), y (0) = x 0
Numerical analysis
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 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).
1. Analytical Solution
dy 2 y = ex
= 2xy dx
Numerical analysis
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
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
Numerical analysis
3. Approximate y(0.5)
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
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 .
Numerical analysis
. Clean screen
To clear the screen you must type the clc function on the command line ( >> ).
. Aids
? 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
? 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.
Numerical analysis
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
f) Useful Functions
. vpa(expr,n): Arithmetic Precision Variable, allows calculating the expr with n number of digits.
Numerical analysis
Numerical analysis
a) Matrices
1. Create a Matrix
Example A.1
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.
To Modify an element of an array, simply write the absolute position of the element you want to access.
Numerical analysis
Example A.2
Modify the element at position (2,3) of the previously created matrix M by the quantity 10.
Example A.3
Add the row [1 5 9] to the end of the matrix M, created at the beginning of the matrices section.
Example A.4
Add column [1; 5] to the matrix M , created at the beginning of the matrices section.
Note that it is mandatory to leave a space between the new column and the current matrix .
Numerical analysis
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.
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.
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
Example A.7
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:
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.
Example A.9
Calculate w = 4 v - 9 u , if u = 3 i + 5 j and v = 6 i - 2 j .
Numerical analysis
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
It would be written:
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
Numerical analysis
Graph of Functions in 2D
TO
5
a) In Rectangular or Cartesian Coordinates
To graph functions of a variable with MatLab, we can use the plot() , fplot() and ezplot() functions.
Example A.12
>> 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);
>> y=sin(x);
-0.2
-0.4
-0.6
-0.8
-1
01234567
Numerical analysis
Just write:
>> fplot('sin(x)',[0,2*pi]), grid on
Just write:
>> ezplot('sin(x)',[0,2*pi]), grid on
0123456
x
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
Example A.13
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
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
Example A.15
( 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 θ .
x = r cos ( θ )
y = r sin ( θ )
Numerical analysis
Example A.16
Graph of r = 2 - 4 cos ( θ ) , -π ≤ θ ≤ π.
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:
>> (4*x-5*y)*(256*x-4*y)
That is, the coefficients are followed by the multiplication operator (*).
a) Algebraic Expressions
Matlab has a set of functions that make it easier for us to process algebraic expressions, among them we find:
. factor() : Factors the expression (sometimes) if the argument is a symbolic function. If it is a number, it provides
factorization into prime numbers.
. simple() : Use different simplification tools and select the form that has the least number of characters.
Numerical analysis
pretty() : Displays the expression in a manner similar to that used in regular writing.
Example A.17
1. C = A + B 2. C = A - B 3. C = A 2 - B 3 4. C = A ∗ B
Example A.18
Numerical analysis
Example A.19
>> solve(4*x^5-7*x^2-2*x-5)
Example A.20
x+4y-z=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.
Numerical analysis
c) Limit Calculation
The function indicated to calculate all types of limits in Matlab is limit() .
Example A.21
>> 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() .
Example A.22
>> syms x
>> diff(sin(4*x.^2+5))
>> diff(sin(4*x.^2+5),3)
Numerical analysis
d) Calculus of Integrals
The indicated function to calculate integrals in Matlab is int() .
Example A.23
>> 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))
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.
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
You simply have to enter its name on the command line or through the editor.
In the same way as a script but its arguments must be passed between parentheses and separated by commas.
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
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.
a) IF Control Statement
In its simplest form, the if statement is described as follows:
if condition statements
end
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
variable switch
case expr_1, block 1
case {expr_2, expr_3, expr_4, ...}, block 2
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.
Numerical analysis
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).
The functions allow you to define functions entirely analogous to those of Matlab, with their name, their arguments and
their output values.
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:
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.
Example A.24
Create a function that allows you to calculate the surface area of a rectangular box.
>>BoxSurfaceArea(3,4,7)
Numerical analysis
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
>> major(3.5)
Example A.26
( 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
Numerical analysis
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.
x 1 = | sine ( 5 ) | = 0 . 9589
x2 = | sine ( 0 . 9589 ) | = 0 . 8186
x 3 = | sine ( 0 . 8186 ) | = 0 . 7302
now,
>> 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.
Numerical analysis
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
[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.
[5] Ivan F. Asmar Ch, 1999. Numerical Methods, A First Course , http://unalmed.edu.co/ifas-mar/libro.html .
Numerical analysis