0% found this document useful (0 votes)
42 views

Resolution Numerical Methods of The Chapra Book With Python PDF

This document presents a summary of the main numerical methods for engineers. It begins by explaining the bisection method for solving nonlinear equations, then covers methods for nonlinear and linear systems of equations, such as fixed point, Newton-Raphson, and Gauss-Jordan. It also explains methods of interpolation, numerical integration, ordinary and partial differential equations, including Runge-Kutta, Romberg, and Gaussian quadrature.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Resolution Numerical Methods of The Chapra Book With Python PDF

This document presents a summary of the main numerical methods for engineers. It begins by explaining the bisection method for solving nonlinear equations, then covers methods for nonlinear and linear systems of equations, such as fixed point, Newton-Raphson, and Gauss-Jordan. It also explains methods of interpolation, numerical integration, ordinary and partial differential equations, including Runge-Kutta, Romberg, and Gaussian quadrature.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 160

UNSCH - Numerical Methods

Steven C. Chapra and Raymond P. Canale

Solving numerical methods for


engineers
Version 1.0 – June 24, 2019

Digital magazine
Mathematics, Education and the Internet. (http://www.tec-digital.itcr.ac.cr/revistamatematica/).
NATIONAL UNIVERSITY SAN CRISTÓBAL DE
HUAMANGA
FACULTY OF MINING ENGINEERING GEOLOGY AND
CIVIL
CIVIL ENGINEERING VOCATIONAL TRAINING SCHOOL

TEAM WORK
ING NUMERICAL METHODS FOR ENGINEERS AUTHOR: STEVEN C. CHAPRA
MOND P. CHANNEL
SUBJECT : NUMERICAL METHODS IC-343
TEACHER : Eng. CASTRO PEREZ, Cristian
STUDENTS : 1. AYALA MEDINA, Edson Joel
: 2. BARRA SOLANO, Josué Isaías
: 3. BERROCAL SERNA, Jhon Alexis
: 4. FLOWERS DAYS, Max Limber
: 5. LOPEZ AUCCASI, Erich Von
: 6. MEDINA HUAMAN, Jairo Rosson
: 7. QUICAÑO BELLIDO, Johel
GRADUATE : 2019 - I
SCHOOL
LIVERY DATE : 04/08/2019

AYACUCHO - PERU
2019
3

GENE INDEX
ERA

NON-LINEAR PAGE 1
1.1 Bisection Method 1
1.2 Newton Raphson method 5
1.3 Secant Method 11
1.4 Modified secant 16

2.1 Fixed Point Method 25


2.2 Newton Raphson method 28
2.3 Broyden method 33

2.4 General Gauss Method 35


2.5 Gauss Jordan method 38

4 ITERATIVE METHODS FOR SYSTEMS OF LINEAR EQUATIONS


4.1 Gauss Seidel
PAGE 43
43

4.2 Jacobbi 45
4.3 Issues 47

2.6 Finite differences 53


2.7 Divided Differences - (Newton Polynomial) 56
2.8 Interpolation polynomial 58
2.9 Lagrangian interpolation 60
2.10 Cubic Tracers 62
2.11 Exercises 65
6.1 Newton-Cotes integration formulas 77
trapezoid rule 77 • Simpson's rules 78
6.2 Integration of equations 91
Romberg integration 91 • Gauss quadrature 94 • Gauss quadrature with polynomials of
legendre 96
NUMERICAL INTEGRATION PAGE 77
ORDINARY DIFFERENTIAL EQUATIONS PAGE 107
7.1 EDO with Taylor 107
7.2 Runge-Kutta 2nd Order dy/dx 110
7.3 Runge-Kutta 4th Order dy/dx 112
7.4 Runge Kutta d2y/dx2
8 PARTIAL DIFFERENTIAL EQUATIONS PAGE 117
8.1 EDP Parabolic 117
8.2 EDP Parabolic explicit method 118
8.3 EDP Parabolic implicit method 122
8.4 EDP Ellipticals 126
8.5 EDP elliptical iterative method 127
8.6 EDP Elliptical implicit method 129
8.7 Hyperbolic EDPs 134

Bibliography 137
114
Nonlinear equations

1.1 Bisection Method


The method is based on the intermediate value theorem, known as the bisection method, binary search, interval
partitioning or Bolzano method. It is a type of incremental search in which: the interval is always divided in half.
If the function changes sign over an interval, the value of the function at the midpoint is evaluated.
The position of the root is determined at the midpoint of the subinterval within which a sign change occurs.
The process is repeated until a better approximation is obtained.

Figure 1.1: Bisection Method

ERROR LIMIT
Suppose that f ∈ C [ a , b ] and f ( a ) ∗ f ( b ) < 0, f is a function on the interval [ a , b ] and that it changes sign.

b-a
| Pn - P | ≤ 2n
1.1 Bisection Method
2
where where ≥ 1
The inequality implies that pn converges to p with a convergence ratio O 2 1 n , that is:
1pn=p+O2n

Exercise 1.1 ( Exercise 5.1 page 139)

Determine the real roots of f ( x ) = - 0.5 x 2 + 2.5 x + 4.5 Using the bisection method with three iterations to
determine the largest root. Use x l = 5 and x u = 10 as initial values. Compute the estimated error ² a and the true
error ² t for each iteration.

Code in Matlab %bissection method clear , clc h= input ( 'enter the function to analyze:' ); f=inline(h);
a= input ( 'enter the lower limit of the interval:' ); b= input ( 'enter the upper limit of the interval:' ); tol= input
( 'indicate the desired tolerance:' );

c=0 ; n=0 ; MEP = (ba)/2;


fprintf ( '\tn\t\ta \t\tc \t\tb \t\t MEP\n' )

while (MEP > tol) c = (a+b)/2;


disp ([n,a,c,b,MEP]) if (f(a)*f(c)<0) b=c;
else a=c; end
MEP = (ba)/2;
n=n+1; end
fprintf ( 'root found with a tolerance of %f:\n\t %f\n' ,tol,c)

Solution

The root found with a tolerance of 0.500000 is 6.875000

Exercise 1.2 ( Exercise N 5.2 - page 139)

Determine the real roots of f ( x ) = 5 x 3 - 5 x 2 + 6 x - 2 Using the bisection method to locate the smallest root. Use
the initial values x i = 0 and x u = 1 iterating until the error
1.1 Bisection Method
3

Yo xi xu Ra í zapr ó x . % Mistake
0 5.00 10.00 3.353659 7.5

1 5.00 7.50 3.353659 6.25


2 6.25 6.875 2.801332 6.875

3 6.25 6.5625 2.719341 6.5625


Table 1.1: Table from exercise 5.1 page. 139 method by Bisection

Figure 1.2: Graph of the function solved by Bisection

estimated ² a is below ² s = 10%.

Solution
The root found with a tolerance of 0.100000 is : 0.375000

Yo xi xu Ra í zapr ó x . % Mistake
0 0.00 1.00 0.5 0.5

1 0.00 0.50 0.25 0.25

2 0.25 0.50 0.375 0.125


Table 1.2: Table from exercise 5.2 page. 139 method by Bisection
1.1 Bisection Method
4
Exercise 1.3

Figure 1.3: Graph of the function solved by Bisection

(Exercise N 5.3 page - 139)

Determine the real roots of f ( x ) = - 25182 x - 90 x 2 + 44 x 3 ?8 x 4 + 0.7 x 5 Using the bisection method to locate
the largest root with ² s = 10%. Use x l = 0.5 and x u = 1.0 as initial values.

Solution

Yo xi xu Root approx. %Mistake


0 0.50 1.00 0.75 0.25

1 0.75 1.00 0.875 0.125


Table 1.3: Table from exercise 5.3 page. 139 method by Bisection

The root found with a tolerance of 0.100000 is 0.875000


1.2 Newton Raphson method
5

Figure 1.4: Graph of the function solved by Bisection

1.2 Newton Raphson method

It is deduced from graphical interpretation or through the use of the Taylor series.

125

1.0 1.5 2.0 2.5 3.0 3.5 4.0


Figure 1.5: Newton Raphson method

From the graph, the triangle formed by the tangent line that passes through f ( xi ), with slope f 0 ( xi ) and the x axis is
used.
1.2 Newton Raphson method
6
f ( x ) -0

( )0
f0(xi)=f x i

xi-xi+1
The point x i + 1 is the intersection of the tangent line with the x axis, which is closest to the root of f ( x ), the value
that is used for the next iteration. Rearranging the equation determines the formula for the following point:
f ( xi )
xi+ =xi-
1 f0(xi
) found | xi + 1? xi |
The error is determined as the difference between the successive values

(Exercise 6.3 - page 167)

Use the Newton-Raphson methods to determine a root of f ( x ) = - x 2 + 1.8 x + 2.5 using x 0 = 5. Do the
calculation until ² a a is less than ² s = 0.05%. Also, do an error check on your final answer.

Python code

# Newthon raphson method import numpy as np import matplotlib.pyplot as plt from sympy import
Symbol from sympy import diff from scipy.misc import derivative from math import pi def poli(x):
y = -x ** 2 + 1.8 * x + 2.5 # Add the function return (y) def deri(x):
d = -2*x + 1.8 # Add the derivative of the function
Nonlinear equations-----------------------------------------------------------------------------------------------------------
1.1 Bisection Method---------------------------------------------------------------------------------------------------
1.2 Newton Raphson method------------------------------------------------------------------------------------------
1.3 Secant Method--------------------------------------------------------------------------------------------------------
1.4 Modified secant-------------------------------------------------------------------------------------------------------
Systems of nonlinear equations--------------------------------------------------------------------------------------------
2.1 Fixed Point Method--------------------------------------------------------------------------------------------------
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)-------------------------------------------------------------------------
2.2 Newton Raphson method--------------------------------------------------------------------------------------------
2.3 Broyden method------------------------------------------------------------------------------------------------------
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)-----------------------------------------------------------------------
3.1 General Gauss Method-----------------------------------------------------------------------------------------------
3.2 Gauss Jordan method-------------------------------------------------------------------------------------------------
4.1 Gauss Seidel-------------------------------------------------------------------------------------------------------
Jacobbi----------------------------------------------------------------------------------------------------------------------
Interpolation------------------------------------------------------------------------------------------------------------------
5.1 Finite differences--------------------------------------------------------------------------------------------------
5.2 Differences Divided - (Newton Polynomial)---------------------------------------------------------------------
1.2 Newton Raphson method
7
5.3 Interpolation polynomial--------------------------------------------------------------------------------------------
5.4 Lagrangian interpolation---------------------------------------------------------------------------------------------
5.5 Cubic Tracers---------------------------------------------------------------------------------------------------------
Numerical integration-------------------------------------------------------------------------------------------------------
6.1 Newton-Cotes integration formulas----------------------------------------------------------------------------
6.1.1 trapezoid rule-------------------------------------------------------------------------------------------------
Simpson's Rules--------------------------------------------------------------------------------------------------------
Integration of equations--------------------------------------------------------------------------------------------------
Romberg integration---------------------------------------------------------------------------------------------------
Gaussian quadrature----------------------------------------------------------------------------------------------------
6.2.3 Gauss quadrature with legendre polynomials----------------------------------------------------------------
Ordinary differential equations-------------------------------------------------------------------------------------------
7.1 EDO with Taylor----------------------------------------------------------------------------------------------------
7.2 Runge-Kutta 2nd Order dy/dx-------------------------------------------------------------------------------------
7.3 Runge-Kutta 4th Order dy/dx-------------------------------------------------------------------------------------
7.4 Runge Kutta d2y/dx2-----------------------------------------------------------------------------------------------
partial differential equations----------------------------------------------------------------------------------------------
8.1 EDP Parabolic---------------------------------------------------------------------------------------------------
8.2 EDP Parabolic explicit method------------------------------------------------------------------------------------
8.3 EDP Parabolic implicit method-----------------------------------------------------------------------------------
8.4 EDP Ellipticals--------------------------------------------------------------------------------------------------
8.5 EDP elliptical iterative method--------------------------------------------------------------------------------
8.6 EDP Elliptical implicit method------------------------------------------------------------------------------------
8.7 Hyperbolic EDPs----------------------------------------------------------------------------------------------------

x = float ( input ( "Enter the start value: "))


erroru = float ( input ( "Enter the tolerance: root = [x] i=0 error = 1 "))

print ( " ")


1.2 Newton Raphson method

print ( "{:^5}{:^14}{:^12}{:^12}{:^13}{:^14}" . format ( "i" , "xi" , "f(xi) " , "f'(xi)" , "Approximate root" , "%Error" ))
print ( "---------------------------------------------------------------------------------------------------------------------------------------------" ) while abs
(error) > erroru:
x1 = x - (poly(x)/deri(x))
root.append(x1)
i = i+1
error = abs ((root[i]-root[i-1])/root[i])*100
print (f "| {i} | {x:10.6f} | {poly(x):10.6f} | {deri(x):10.6f} | {x1:10.6f} | {error:10.6f} | " )
x = x1
Nonlinear equations...............................................................................................................................................5
1.1 Bisection Method....................................................................................................................................5
1.2 Newton Raphson method........................................................................................................................5
1.3 Secant Method...........................................................................................................................................15
1.4 Modified secant.........................................................................................................................................18
Systems of nonlinear equations...........................................................................................................................28
2.1 Fixed Point Method...................................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).................................................................................................28
2.2 Newton Raphson method...........................................................................................................................33
2.3 Broyden method.........................................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...............................................................................................38
3.1 General Gauss Method..............................................................................................................................35
3.2 Gauss Jordan method.................................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................................47
Jacobbi.............................................................................................................................................................49
Interpolation........................................................................................................................................................57
5.1 Finite differences..................................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................................59
5.3 Interpolation polynomial...........................................................................................................................63
5.4 Lagrangian interpolation............................................................................................................................65
5.5 Cubic Tracers.............................................................................................................................................67
Numerical integration..........................................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................................77
6.1.1 trapezoid rule.................................................................................................................................77
Simpson's Rules...........................................................................................................................................78
Integration of equations...................................................................................................................................91
Romberg integration....................................................................................................................................91
Gaussian quadrature.....................................................................................................................................94
1.2 Newton Raphson method

6.2.3 Gauss quadrature with legendre polynomials.....................................................................................96


Ordinary differential equations.........................................................................................................................107
7.1 EDO with Taylor.....................................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.................................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx..................................................................................................................112
7.4 Runge Kutta d2y/dx2...............................................................................................................................115
partial differential equations..............................................................................................................................117
8.1 EDP Parabolic.....................................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................................118
8.3 EDP Parabolic implicit method...............................................................................................................121
8.4 EDP Ellipticals...................................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................................127
8.6 EDP Elliptical implicit method................................................................................................................129
8.7 Hyperbolic EDPs.....................................................................................................................................136

N For the following exercises using the Newton Raphson method we will use the same code, we will only change the function and
the derivative.

Solution

Yo xi f(xi) f'(xi) Root approx. %Mistake


1 5.000000 -13.500000 -8.200000 3.353659 49.090909

2 3.353659 -2.710440 -4.907317 2.801332 19.716558


3 2.801332 -0.305064 -3.802665 2.721108 2.948204
4 2.721108 -0.006436 -3.642217 2.719341 0.064980

5 2.719341 -0.000003 -3.638683 2.719341 0.000032


Table 1.4: Table of results of exercise 6.3 - page. 167 by Newton Raphson
Exercise 1.5
The root with an error of 0.000032% is: 2.719341

(Exercise 6.4 - page 167)

Determine the real roots of f ( x ) = - 1 + 5.5 x - 4 x 2 + 0.5 x 3 : graphically, with the Newton-Raphson method to within ² s = 0.01
%.

Solution
1.2 Newton Raphson method

N In the graph we see that it is approaching 0, so we take this value to be able to start the iterations.
1.2 Newton Raphson method
8

Figure 1.6: Graph of the function f ( x ) = - x 2 + 1.8 x + 2.5 solved by Newton Raphson

Figure 1.7: Graph of the function f ( x ) = -1 + 5.5 x - 4 x 2 + 0.5 x 3 : solved by Newton Raphson

The root with an error of 0.000000% is: 0.214333

Exercise 1.6 (Exercise 6.5 - page 167)

Employ the Newton-Raphson method to determine a real root of f ( x ) = - 1 + 5.5 x - 4 x 2 + 0.5 x 3 using initial choices of

1. 4.52,

2. 4.54.
1.2 Newton Raphson method
9

Yo xi f(xi) f'(xi) Root approx. %Mistake


1 0.000000 -1.000000 5.500000 0.181818

2 0.181818 -0.129226 4.095041 0.213375 14.789338


3 0.213375 -0.003696 3.861294 0.214332 0.446594
4 0.214332 -0.000003 3.854250 0.214333 0.000408

5 0.214333 -0.000000 3.854244 0.214333 0.000000


Table 1.5: Table of results of exercise 6.4 - page. 167 by Newton Raphson

Study and use graphical and analytical methods to explain any peculiarities in your results.

Figure 1.8: the graph shows that the roots are 3.

Solution
For the first iteration we will start with 4.52 and present the results in the following table. The root with an error of 0.000001
1.2 Newton Raphson method
1
0
Yo xi f(xi) f'(xi) Root approx. %Mistake
1 4.520000 -11.688896 -0.014400 -807.208889 100.559954

2 -807.208889 -265593867.754186 983842.456563 -537.253200 50.247386


3 -537.253200 -78694170.753678 437265.027567 -357.284152 50.371405
4 -357.284152 -23316586.069076 194341.721476 -237.306898 50.557845
5 -237.306898 -6908481.704991 86375.801219 -157.325218 50.838436
6 -157.325218 -2046867.068002 38390.938190 -104.008806 51.261440
7 -104.008806 -606419.264442 17064.318195 -68.471538 51.900790
8 -68.471538 -179640.315889 7585.799490 -44.790406 52.870991
9 -44.790406 -53200.887998 3373.093953 -29.018273 54.352420
10 -29.018273 -15746.406408 1500.736393 -18.525819 56.636919
11 -18.525819 -4654.801931 668.515527 -11.562927 60.217392
12 -11.562927 -1372.390159 298.555318 -6.966156 65.987179
13 -6.966156 -402.447707 134.020255 -3.963269 75.767934
14 -3.963269 -116.754525 60.767408 -2.041935 94.093835
15 -2.041935 -33.165549 28.089723 -0.861234 137.094014
16 -0.861234 -9.023084 13.502460 -0.192979 346.283155
17 -0.192979 -2.213944 7.099696 0.118857 262.362475
18 0.118857 -0.401954 4.570334 0.206806 42.527169
19 0.206806 -0.029221 3.909707 0.214280 3.487908
20 0.214280 -0.000206 3.854637 0.214333 0.024922

21 0.214333 -0.000000 3.854244 0.214333 0.000001


Table 1.6: Results of exercise 6.5 - page. 167 for the initial value of 4.52

For the second iteration we will start with 4.54 and present the results in the following table. The root with an error of
0.000069% is: 6.305898
1.3 Secant Method

Yo xi f(xi) f'(xi) Root approx. %Mistake


1 4.540000 -11.688068 0.097400 124.540698 96.354605

2 124.540698 904479.551005 22274.752659 83.935105 48.377367


3 83.935105 267945.914388 9901.671861 56.874431 47.579683
4 56.874431 79358.890954 4402.555852 38.848790 46.399492
5 38.848790 23491.604138 1958.552379 26.854419 44.664423
6 26.854419 6945.223663 872.404349 18.893404 42.136475
7 18.893404 2047.172405 389.793846 13.641468 38.499788
8 13.641468 598.937461 175.502727 10.228772 33.363693
9 10.228772 171.853967 80.611486 8.096892 26.329600
10 8.096892 46.709029 39.064362 6.901198 17.325891
11 6.901198 10.790532 21.730221 6.404630 7.753265
12 6.404630 1.505003 15.791893 6.309328 1.510497
13 6.309328 0.050492 14.736808 6.305902 0.054334

14 6.305902 0.000064 14.699383 6.305898 0.000069


Table 1.7: Results of exercise 6.5 - page. 167 for the initial value of 4.54

1.3 Secant Method


A possible drawback in the Newton Raphson method is to implement derivative evaluation, which can be approximated by a
finite difference divided backwards:

f ( xi − 1 ) −f ( xi )
f0(xi)=
xi-1-xi
which is substituted into the equation of the Newton-Raphson method to obtain:

=
f ( xi ) -f ( xi )
x
i+1 Xi-
xi-1 xi
1.3 Secant Method
1
2

Figure 1.9: Secant method

Exercise 1.7 ( Exercise 6.6 - page 167)

Determine the smallest real root of f ( x ) = - 12 - 21 x + 18 x 2 - 2.4 x 3 :

1. in graphic form

2. with the use of the secant method for a value of ² s that corresponds to three significant figures.

Python code
# Secant method import numpy as np import matplotlib.pyplot as plt from sympy import Symbol from sympy import diff
from scipy.misc import derivative from math import sin def poly(x):
y = -12 - 21 * x + 18 * x ** 2 - 2.4 * x ** 3 # Here enter the function to be solved. return (y)
# -------------------------------------------------------------------------------------------------------------------------------------------------------
# Here put the intervals of the function to graph
# -------------------------------------------------------------------------------------------------------------------------------------------------------
x = np.linspace(-2, 7, 101)

plt.plot(x, poly(x))
plt.grid()
plt.show()
#-------------------------------------------------------------------------------------------------------------------------------------------------------
Nonlinear equations...............................................................................................................................................5
1.1 Bisection Method....................................................................................................................................5
1.2 Newton Raphson method........................................................................................................................5
1.3 Secant Method...........................................................................................................................................15
1.4 Modified secant.........................................................................................................................................18
1.3 Secant Method
1
3
Systems of nonlinear equations...........................................................................................................................28
2.1 Fixed Point Method...................................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).................................................................................................28
2.2 Newton Raphson method...........................................................................................................................33
2.3 Broyden method.........................................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...............................................................................................38
3.1 General Gauss Method..............................................................................................................................35
3.2 Gauss Jordan method.................................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................................47
Jacobbi.............................................................................................................................................................49
Interpolation........................................................................................................................................................57
5.1 Finite differences..................................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................................59
5.3 Interpolation polynomial...........................................................................................................................63
5.4 Lagrangian interpolation............................................................................................................................65
5.5 Cubic Tracers.............................................................................................................................................67
Numerical integration..........................................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................................77
6.1.1 trapezoid rule.................................................................................................................................77
Simpson's Rules...........................................................................................................................................78
Integration of equations...................................................................................................................................91
Romberg integration....................................................................................................................................91
Gaussian quadrature.....................................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.....................................................................................96
Ordinary differential equations.........................................................................................................................107
7.1 EDO with Taylor.....................................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.................................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx..................................................................................................................112
7.4 Runge Kutta d2y/dx2...............................................................................................................................115
partial differential equations..............................................................................................................................117
8.1 EDP Parabolic.....................................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................................118
8.3 EDP Parabolic implicit method...............................................................................................................121
8.4 EDP Ellipticals...................................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................................127
1.3 Secant Method
1
4
8.6 EDP Elliptical implicit method................................................................................................................129
8.7 Hyperbolic EDPs.....................................................................................................................................136

print ( "----------------------------------------------------------------------------------------------------------------------------------------------")
print ( "{:^5}{:^12}{:^12}{:^10}{:^16}{:^15}{:^15}" . format ( "i" , "xi-1 " , "xi" ,
"f(xi-1)" , "f(xi)" , "Root approx." , " %Mistake" ))
print ( "--------------------------------------------------------------------------------------------------------------------------------------------")
while abs (error) > erroru: i = i+1 x2 = x1 - (poly(x1) * (x1 - x0)) / (poly(x1) - poly(x0)) root.append(x2) error = abs ((root[i] -
root[i - 1]) / root[i])*100 print (f "|{i:^3}| {x0:^10.6f} | {x1:^10.6f} | {poly(x0):.6f} | {poly(x1):.6f} " f "| {x2:10.06f} | {error:10.06f}
|" ) x0 = x1 x1 = x2
Nonlinear equations...............................................................................................................................................5
1.1 Bisection Method....................................................................................................................................5
1.2 Newton Raphson method........................................................................................................................5
1.3 Secant Method...........................................................................................................................................15
1.4 Modified secant.........................................................................................................................................18
Systems of nonlinear equations...........................................................................................................................28
2.1 Fixed Point Method...................................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).................................................................................................28
2.2 Newton Raphson method...........................................................................................................................33
2.3 Broyden method.........................................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...............................................................................................38
3.1 General Gauss Method..............................................................................................................................35
3.2 Gauss Jordan method.................................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................................47
Jacobbi.............................................................................................................................................................49
Interpolation........................................................................................................................................................57
5.1 Finite differences..................................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................................59
5.3 Interpolation polynomial...........................................................................................................................63
5.4 Lagrangian interpolation............................................................................................................................65
5.5 Cubic Tracers.............................................................................................................................................67
Numerical integration..........................................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................................77
6.1.1 trapezoid rule.................................................................................................................................77
Simpson's Rules...........................................................................................................................................78
Integration of equations...................................................................................................................................91
Romberg integration....................................................................................................................................91
1.3 Secant Method
1
5
Gaussian quadrature.....................................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.....................................................................................96
Ordinary differential equations.........................................................................................................................107
7.1 EDO with Taylor.....................................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.................................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx..................................................................................................................112
7.4 Runge Kutta d2y/dx2...............................................................................................................................115
partial differential equations..............................................................................................................................117
8.1 EDP Parabolic.....................................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................................118
8.3 EDP Parabolic implicit method...............................................................................................................121
8.4 EDP Ellipticals...................................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................................127
8.6 EDP Elliptical implicit method................................................................................................................129
8.7 Hyperbolic EDPs.....................................................................................................................................136

N To solve the exercises consecutive to this one, by the secant method, we will use the same
code, we will only change the function to be solved.
1.3 Secant Method
1
6
Solution
We observe that the smallest real root is in the interval - 1 to 0, so we will use these two values to start the iteration by the secant
method.

Figure 1.10: the graph shows that the roots are 3, but the smallest one is to the left of 0.

Yo xi-1 xi
) f(xi−1) f ( xi ) Root approx. %Mistake
1 0.000000 -1.000000 -12.000000 29.400000 -0.289855 245.000000

2 -1.000000 -0.289855 29.400000 -4.342310 -0.381244 23.971220


3 -0.289855 -0.381244 -4.342310 -1.244644 -0.417964 8.785464
4 -0.381244 -0.417964 -1.244644 0.096971 -0.415310 0.639063
5 -0.417964 -0.415310 0.096971 -0.001891 -0.415361 0.012222

6 -0.415310 -0.415361 -0.001891 -0.000003 -0.415361 0.000018


Table 1.8: Table of results from exercise 6.6 - page. 167 by Newton Raphson

The root with an error of 0.000018% is: -0.415361.

Exercise 1.8 ( Exercise No. 6.7 page 167)

Find the first positive root of f ( x ) = sinx + cos (1 + x 2 ) - 1 where x is in radians. To locate the root, use four iterations of the
secant method with initial values of a ) x i - 1 = 1.0 and x i = 3.0; and b ) x i - 1 = 1.5y x i = 2.5, and c ) x i - 1 = 1.5y x i = 2.25.
1.3 Secant Method
1
7
Solution

Figure 1.11: It is observed that the first positive root is between 1.5 and 2

Yo xi-1 xi ) f(xi−1) f ( xi ) Root approx. %Mistake


1 1.000000 3.000000 -0.574676 -1.697952 -0.023214 13023.080948

2 3.000000 -0.023214 -1.697952 -0.483363 -1.226347 98.107039


3 -0.023214 -1.226347 -0.483363 -2.744750 0.233951 624.189399

4 -1.226347 0.233951 -2.744750 -0.274717 0.396366 40.975929


Table 1.9: First 4 iterations for the initial values of x i -1 = 1.0 and x i = 3.0

Yo xi-1 xi
) f(xi−1) f ( xi ) Root approx. %Mistake
1 1.500000 2.500000 -0.996635 0.166396 2.356929 6.070241

2 2.500000 2.356929 0.166396 0.669842 2.547287 7.472986


3 2.356929 2.547287 0.669842 -0.082828 2.526339 0.829187

4 2.547287 2.526339 -0.082828 0.031471 2.532107 0.227788


Table 1.10: First 4 iterations for the initial values of x i - 1 = 1.5 and x i = 2.5

N In the first two tables it is observed that the first 4 iterations are not enough to reach the solution, which is observed in the
graph, however when we shorten the interval we reach a root closer to that of the graph.
1.4 Modified secant
1
8
Yo xi-1 xi ) f(xi−1) f ( xi ) Root approx. %Mistake
1 1.500000 2.250000 -0.996635 0.753821 1.927018 16.760716

2 2.250000 1.927018 0.753821 -0.061769 1.951479 1.253477

3 1.927018 1.951479 -0.061769 0.024147 1.944604 0.353536

4 1.951479 1.944604 0.024147 -0.000014 1.944608 0.000204


Table 1.11: First 4 iterations for the initial values of x i -1 = 1.5 and x i = 2.25

The root with an error of 0.000204% is: 1.944608

1.4 Modified secant

Exercise 1.9 (Exercise 6.8 page 167)

Determine the real root of x 3.5 = 80, with the modified secant method to within ² s = 0.1%, using an initial choice of x 0 =
3.5 and d = 0.01.

Python code

# Modified secant method import numpy as np


import matplotlib.pyplot as plt from sympy import Symbol from sympy import diff
from scipy.misc import derivative def poly(x):
y = x**(3.5) - 80 # Here enter the function to solve. return (y)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
# Here put the intervals of the function to graph
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
x = np.linspace(2, 4.5, 101)
plt.plot(x, poly(x))
plt.grid()
plt.show()
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
Nonlinear equations...............................................................................................................................................5
1.1 Bisection Method....................................................................................................................................5
1.2 Newton Raphson method........................................................................................................................5
1.3 Secant Method...........................................................................................................................................15
1.4 Modified secant.........................................................................................................................................18
Systems of nonlinear equations...........................................................................................................................28
2.1 Fixed Point Method...................................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).................................................................................................28
2.2 Newton Raphson method...........................................................................................................................33
2.3 Broyden method.........................................................................................................................................38
1.4 Modified secant
1
9
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...............................................................................................38
3.1 General Gauss Method..............................................................................................................................35
3.2 Gauss Jordan method.................................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................................47
Jacobbi.............................................................................................................................................................49
Interpolation........................................................................................................................................................57
5.1 Finite differences..................................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................................59
5.3 Interpolation polynomial...........................................................................................................................63
5.4 Lagrangian interpolation............................................................................................................................65
5.5 Cubic Tracers.............................................................................................................................................67
Numerical integration..........................................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................................77
6.1.1 trapezoid rule.................................................................................................................................77
Simpson's Rules...........................................................................................................................................78
Integration of equations...................................................................................................................................91
Romberg integration....................................................................................................................................91
Gaussian quadrature.....................................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.....................................................................................96
Ordinary differential equations.........................................................................................................................107
7.1 EDO with Taylor.....................................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.................................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx..................................................................................................................112
7.4 Runge Kutta d2y/dx2...............................................................................................................................115
partial differential equations..............................................................................................................................117
8.1 EDP Parabolic.....................................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................................118
8.3 EDP Parabolic implicit method...............................................................................................................121
8.4 EDP Ellipticals...................................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................................127
8.6 EDP Elliptical implicit method................................................................................................................129
8.7 Hyperbolic EDPs.....................................................................................................................................136

delta = float ( input ( 'Enter the starting delta value: ' ))


1.4 Modified secant
2
0
x0 = float ( input ( 'Enter the starting value x0: ' )) erroru = float ( input ( 'Enter the tolerance: ' )) root = [x0] #
Enter the true root or x0.
#root.insert(0,0) i=0 error = 1 print ( " " ) print ( "------------------------------------------------------------------------------------")
print ( "{:^5}{:^12}{:^12}{:^10}{:^16}{:^15}{:^15}" . format ( "i" , "xi-1 " , "xi-1+ d*xi-1 " , "f(xi-1)" , "f(xi-1+&xi-1)" ,
"Approximate root" , " %Error" )) print ( "----------------------------------------------------------------------------------------------")
while abs (error) > erroru: i=i+1 x1 = x0 - (delta * x0 * poly(x0))/(poly(x0 + delta * x0) - poly(x0)) root.append(x1) # error =
abs((root[0] - x1)/x1) # When there is a true root. error = abs ((root[i] - root[i - 1]) / root[i])*100 # When
#there is no true root. print (f "{i:^3}& {x0:^10.6f} & {(x0 + delta * x0):^10.6f} & {poly(x0):.6f} & {poly(x0 + delta
* x0):.6f} " f "& {x1:10.06f} & {error:10.06f} " ) x0 = x1
print ( "---------------------------------------------------------------------------------------------------------------------------------------------")
print (f " | The root with an error of {error:.6f} % is: {x1:.6f} | " ) print ( "------------------------------------------- ")

N To solve the following exercises we will only change the function in the code.

Solution

Figure 1.12: function x 3.5 =


80
1.4 Modified secant
2
1
f(xi−1+δ∗xi−1
Yo xi-1 xi-1+δ∗xi-1 f(xi−1) ) Root approx. %Mistake
1 3.500000 3.535000 0.211780 3.054461 3.497392 0.074556

2 3.497392 3.532366 0.002822 2.838097 3.497358 0.000995


Table 1.12: Results of the iteration with ² s = 0.1%

The root with an error of 0.000995% is: 3.497358

Exercise 1.1 (Exercise 6.9 - page 167)


0
Determine the largest real root of f ( x ) = 0.95 x 3 - 5.9 x 2 + 10.9 x - 6:

1. In graphic form.

2. With the use of the Newton-Raphson method (three iterations, x i = 3.5).

3. With the secant method (three iterations, xi ?1 = 2.5 and xi = 3.5).

4. By means of the modified secant method (three iterations, x i = 3.5, δ = 0.01).

Solution

Figure 1.13: function f ( x ) = 0.95 x 3 - 5.9 x 2 + 10.9 x -


6:
Yo xi f(xi) f'(xi) Root approx. %Mistake
1 3.500000 0.606250 4.512500 3.365651 3.991770

2 3.365651 0.071249 3.468997 3.345112 0.613991

3 3.345112 0.001549 3.318537 3.344645 0.013958


Table 1.13: Results by the Newton Raphson method - 3 iterations The root with an error of 0.013958 % is: 3.344645
1.4 Modified secant
2
2
Yo xi-1 xi f(xi-1 f ( xi ) Root approx. %Mistake
1 2.500000 3.500000 -0.781250 0.606250 3.063063 14.264706

2 3.500000 3.063063 0.606250 -0.666700 3.291906 6.951692

3 3.063063 3.291906 -0.666700 -0.164874 3.367092 2.232961


Table 1.14: 3 iterations by the secant method The root with an error of 2.232961 % is: 3.367092

+ ∗
Yo xi-1 x
i-1 δ x
i-1) f ( x i − 1 ) f ( x i − 1 + δ ∗ x i − 1 ) Root approx. %Mistake
1 3.500000 3.535000 0.606250 0.769220 3.369800 3.863739

2 3.369800 3.403498 0.085704 0.207879 3.346161 0.706446

3 3.346161 3.379623 0.005033 0.120439 3.344702 0.043632


Table 1.15: Results 3 iterations per modified secant The root with an error of 0.043632 % is: 3.344702

Exercise 1.1 1 ( Exercise 6.10 - page 167)


Determine the smallest positive root of f ( x ) = 8 sin ( x ) e - x - 1:

1. In graphic form.

2. With the use of the Newton-Raphson method (three iterations, x i = 0.3).

3. With the secant method (three iterations, x i - 1 = 0.5 and x i = 0.3).

4. By means of the modified secant method (five iterations x i = 0.3, δ = 0.01).

Solution

Figure 1.14: function f ( x ) = 8 sin ( x ) e - x - 1


1.4 Modified secant
2
3
Yo xi f(xi) f'(xi) Root approx. %Mistake
1 0.300000 0.751414 3.910431 0.107844 178.180367

2 0.107844 -0.226952 6.367370 0.143487 24.840582

3 0.143487 -0.008952 5.868388 0.145012 1.051965


Table 1.16: Results by Newton Raphson - 3 iterations The root with an error of 1.051965 % is: 0.145012

Yo xi-1 xi f(xi−1) f ( xi ) Root approx. %Mistake


1 0.500000 0.300000 1.326290 0.751414 0.038582 677.557638

2 0.300000 0.038582 0.751414 -0.703097 0.164949 76.609554

3 0.038582 0.164949 -0.703097 0.113865 0.147337 11.953900


Table 1.17: Result of 3 iterations using the secant method The root with an error of 11.953900 % is: 0.147337

Yo xi-1 xi-1+δ∗xi-1) f(xi−1) f ( x i − 1 + δ ∗ x i − 1 ) Root approx. %Mistake


1 0.300000 0.303000 0.751414 0.763094 0.107007 180.356798

2 0.107007 0.108077 -0.232287 -0.225469 0.143463 25.411611


3 0.143463 0.144897 -0.009093 -0.000687 0.145015 1.070182

4 0.145015 0.146465 -0.000001 0.008464 0.145015 0.000147

5 0.145015 0.146465 0.000000 0.008465 0.145015 0.000000


Table 1.18: Results by the modified secant - 5 iterations The root with an error of 0.000000 % is: 0.145015

N We observe that using the three methods: Newton Raphson, secant, and modified secant, we obtain different results and the
largest error with three iterations was obtained in 1.17 Cua dro 1.17.

Exercise 1.1 2 ( Exercise 6.23 - page 168)

a) Apply the Newton-Raphson method to the function f ( x ) = tanh ( x 2 - 9) to evaluate its known real root at x = 3. Use an
initial value of x 0 = 3.2 and do a minimum of four iterations. b) Does the method converge to its real root? Sketch the graph
with the results for each iteration you obtain.
1.4 Modified secant
2
4

Figure 1.15: function f ( x ) = t anh ( x 2 - 9)

Yo xi f(xi) f'(xi) Root approx. %Mistake


1 3.200000 2.911930 60.667747 3.152002 1.522778

2 3.152002 1.355289 17.883251 3.076217 2.463591

3 3.076217 0.499327 7.686401 3.011254 2.157318

4 3.011254 0.067756 6.050157 3.000055 0.373294

5 3.000055 0.000332 6.000111 3.000000 0.001842


Table 1.19: Table of results of exercise 6.23 by Newton Raphson The root with an error of 0.001842% is: 3.000000

Solution

The root converges towards its real solution according to the graph.

Exercise 1.1 3 ( Exercise nº 6.24 - page 168)

The polynomial f ( x ) = 0.0074 x 4 - 0.284 x 3 + 3.355 x 2 - 12.183 x + 5 has a real root between 15 and 20.
Apply the Newton-Raphson method to said function with initial value x 0 = 16.15. Explain your results.

Solution

N It is observed that although we started with 13.15 the iterations take us to the root 0.468480
1.4 Modified secant
2
5
Yo xi f(xi) f'(xi) Root approx. %Mistake
1 16.150000 -9.574455 -1.353682 9.077102 77.920214

2 9.077102 8.678763 0.662596 -4.021010 325.741874

3 -4.021010 128.631764 -54.863959 -1.676451 139.852487


4 -1.676451 36.249950 -25.965987 -0.280396 497.887089
5 -0.280396 8.686147 -14.132095 0.334244 183.889653
6 0.334244 1.292213 -10.034304 0.463023 27.812748
7 0.463023 0.050416 -9.255836 0.468470 1.162708

8 0.468470 0.000088 -9.223505 0.468480 0.002038


Table 1.20: Iterations of f ( x ) = 0.0074 x 4 - 0.284 x 3 + 3.355 x 2 - 12.183 x + 5
The root with an error of 0.002038% is: 0.468480

Figure 1.16: function f ( x ) = 0.0074 x 4 - 0.284 x 3 + 3.355 x 2 - 12.183 x


+5

Suppose the reader is designing a spherical storage tank (see Figure 1.14).
of water for a small town in a developing country. The volume of the liquid that can where V = volume [ ft e 3 ], h = depth of water in
the tank [ft], and R = radius of the tank [ft].
contain is calculated with:
V = π ∗ h 2 [3 R - h ]
3
If R = 3 m , to what depth must the tank be filled so that it contains 30 m3 ? Do three iterations of the Newton-Raphson method to
determine the answer. Find the error re approximate lative after each iteration. Note that the initial value of R will always converge.
1.4 Modified secant
2
6

Figure 1.17: Spherical tank

Solution

[3 R - h ]
V=π∗h2
3
Solving the equation
f ( h ) = πRh2 - ( π3 ) h3 - V

We substitute the data values given to us into this equation and set it equal to 0.
f ( h ) = 3 π h 2 − ( π 3 ) h 3 − 30

Now we graph and iterate using the Newton Raphson method

Figure 1.18: function f ( h ) = 3 πh 2 - ( π 3 ) 30


h3
1.4 Modified secant
2
7
Yo xi f(xi) f'(xi) Root approx. %Mistake
1 2.500000 37.627202 82.466807 2.043729 22.325408

2 2.043729 1.279453 76.204476 2.026939 0.828329


3 2.026939 0.002555 75.899180 2.026906 0.001661

4 2.026906 0.000000 75.898562 2.026906 0.000000


Table 1.21: Table of results of exercise nº 6.26 -page. 169 by Newton Raphson The root with an error of 0.000000 % is:
2.026906

Answer: It must be filled to a depth of 2.026906 meters to obtain 30 m 3 of water.


2
8

Systems of nonlinear equations

2.1 Fixed Point Method

Exercise 2.1 ( Exercise No. 6.12 / Page. 168)

Determine the roots of the following simultaneous nonlinear equations, by means of the methods two of a) fixed point
iteration, and b) Newton-Raphson:

y = - x 2 + x + 0.75
.e2
y+5x=x

Use initial values of x = y = 1.2, and analyze the results.

Solution

N To solve this problem by the fixed point method we will solve the equations as follows:
x = x - y + 0.75

y = x 2 - 5 xy

since otherwise the solution diverges. We will also use a program made in Python to get the iterations, for which we will
use the true value of x=1.372065, y =0.2395019, with which we will get the ² t .

Python code - Fixed point for nonlinear systems

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

"""To use this method we have to isolate x from the first

equation, and from the second


equation and z of the third equation in a convenient way, we must also enter the true values of x, y, z""" def pfix2(u, v,
x0, y0, n=6, true_value np.zeros(n) y = np.zeros(n) x[0] = x0 y[0] = y0 # # The function to graph
2.1 Fixed Point Method
2
# ---------------------------------------------------------------------------------------------------------------------------------- x = 9
np.linspace(-1, 2, 150)
plt.plot(x, x - x**2 + 0.75) plt.plot(x, x ** 2 / (5*x + 1)) plt.grid() plt.show() print ( "------------")
print ( " Fixed point for 2 equations " )
print ( "-----------------------------------------------------------------------------------------------------------------")
print ( "{:^5}{:^14}{:^13}{:^13}{:^13}" . format ( "i" , "xi" , "yi" , " %Et_x." , " %Et_y" )) print ( " ")
print (f " 0 & {x0:10.6f} & {y0:10.6f} " ) for i in range (1, n): x[i] = u(x0, y0) y[i] = v(x [i], y0) x0 = x[i] y0 = y[i] Etx
= abs (((verdx_value - x[i]) / (verdx_value)) * 100) Ety = abs (((verdy_value - y[i ]) / (verdy_value)) )
print (f " {i:^3} & {x[i]:10.6f} & {y[i]:10.6f} & {Etx:10.6f} & {Ety:10.6f} " )
print ( "--------------------------------------------------------------------------------------------------------------------------------")
return pfix2

def pfix3(u, v, w, x0, y0, z0, n=6, verd_value=2, verdy_value =2, verdz_value =2):
x = np.zeros(n)
y = np.zeros(n) z = np.zeros(n) x[0] = x0 y[0] = y0

Nonlinear equations--------------------------------------------------------------------------------------------------------------5
1.1 Bisection Method------------------------------------------------------------------------------------------------------5
1.2 Newton Raphson method---------------------------------------------------------------------------------------------5
1.3 Secant Method------------------------------------------------------------------------------------------------------------15
1.4 Modified secant-----------------------------------------------------------------------------------------------------------18
Systems of nonlinear equations------------------------------------------------------------------------------------------------28
2.1 Fixed Point Method------------------------------------------------------------------------------------------------------28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)----------------------------------------------------------------------------28
2.2 Newton Raphson method-----------------------------------------------------------------------------------------------33
2.3 Broyden method----------------------------------------------------------------------------------------------------------38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)---------------------------------------------------------------------------38
3.1 General Gauss Method--------------------------------------------------------------------------------------------------35
3.2 Gauss Jordan method----------------------------------------------------------------------------------------------------40
4.1 Gauss Seidel-----------------------------------------------------------------------------------------------------------47
Jacobbi-------------------------------------------------------------------------------------------------------------------------49
Interpolation----------------------------------------------------------------------------------------------------------------------57
5.1 Finite differences-----------------------------------------------------------------------------------------------------57
5.2 Differences Divided - (Newton Polynomial)-------------------------------------------------------------------------59
5.3 Interpolation polynomial------------------------------------------------------------------------------------------------63
5.4 Lagrangian interpolation------------------------------------------------------------------------------------------------65
5.5 Cubic Tracers-------------------------------------------------------------------------------------------------------------67
Numerical integration-----------------------------------------------------------------------------------------------------------77
6.1 Newton-Cotes integration formulas--------------------------------------------------------------------------------77
2.1 Fixed Point Method
3
6.1.1
0
trapezoid rule----------------------------------------------------------------------------------------------------77
Simpson's Rules------------------------------------------------------------------------------------------------------------78
Integration of equations------------------------------------------------------------------------------------------------------91
Romberg integration-------------------------------------------------------------------------------------------------------91
Gaussian quadrature-------------------------------------------------------------------------------------------------------94
6.2.3 Gauss quadrature with legendre polynomials-------------------------------------------------------------------96
Ordinary differential equations-----------------------------------------------------------------------------------------------107
7.1 EDO with Taylor-------------------------------------------------------------------------------------------------------107
7.2 Runge-Kutta 2nd Order dy/dx----------------------------------------------------------------------------------------112
7.3 Runge-Kutta 4th Order dy/dx-----------------------------------------------------------------------------------------112
7.4 Runge Kutta d2y/dx2---------------------------------------------------------------------------------------------------115
partial differential equations--------------------------------------------------------------------------------------------------117
8.1 EDP Parabolic-------------------------------------------------------------------------------------------------------117
8.2 EDP Parabolic explicit method---------------------------------------------------------------------------------------118
8.3 EDP Parabolic implicit method---------------------------------------------------------------------------------------121
8.4 EDP Ellipticals------------------------------------------------------------------------------------------------------126
8.5 EDP elliptical iterative method-----------------------------------------------------------------------------------127
8.6 EDP Elliptical implicit method---------------------------------------------------------------------------------------129
8.7 Hyperbolic EDPs-------------------------------------------------------------------------------------------------------136

print (f " 0 & {x0:10.6f} & {y0:10.6f} & {z0:10. 6f}" )


for i in range (1, n):
x[i] = u(x0, y0, z0)
y[i] = v(x[i], y0, z0)
z[i] = w(x[i], y[i], z0)
x0 = x[i]
y0 = y[i]
z0 = z[i]
Etx = abs (((verdx_value - x[i]) / (value _verdx)) * 100)
Ety = abs (((verdy_value - y[i]) / (verdy_value)) )
Etz = abs (((verdz_value - z[i]) / (value _verdz)) )
print (f " {i:^3} & {x[i]:10.6f} & {y[i]:10.6f} & {z[i]:10.6f}
Nonlinear equations...................................................................................................................................................5
1.1 Bisection Method.........................................................................................................................................5
1.2 Newton Raphson method............................................................................................................................5
1.3 Secant Method................................................................................................................................................15
1.4 Modified secant..............................................................................................................................................18
Systems of nonlinear equations................................................................................................................................28
2.1 Fixed Point Method
3
1
2.1 Fixed Point Method........................................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)......................................................................................................28
2.2 Newton Raphson method...............................................................................................................................33
2.3 Broyden method.............................................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)....................................................................................................38
3.1 General Gauss Method...................................................................................................................................35
3.2 Gauss Jordan method......................................................................................................................................40
4.1 Gauss Seidel..............................................................................................................................................47
Jacobbi..................................................................................................................................................................49
Interpolation.............................................................................................................................................................57
5.1 Finite differences.......................................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).................................................................................................59
5.3 Interpolation polynomial................................................................................................................................63
5.4 Lagrangian interpolation................................................................................................................................65
5.5 Cubic Tracers..................................................................................................................................................67
Numerical integration...............................................................................................................................................77
6.1 Newton-Cotes integration formulas..........................................................................................................77
6.1.1 trapezoid rule......................................................................................................................................77
Simpson's Rules................................................................................................................................................78
Integration of equations........................................................................................................................................91
Romberg integration.........................................................................................................................................91
Gaussian quadrature..........................................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials..........................................................................................96
Ordinary differential equations..............................................................................................................................107
7.1 EDO with Taylor..........................................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx......................................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx.......................................................................................................................112
7.4 Runge Kutta d2y/dx2....................................................................................................................................115
partial differential equations..................................................................................................................................117
8.1 EDP Parabolic.........................................................................................................................................117
8.2 EDP Parabolic explicit method....................................................................................................................118
8.3 EDP Parabolic implicit method....................................................................................................................121
8.4 EDP Ellipticals........................................................................................................................................126
8.5 EDP elliptical iterative method...............................................................................................................127
8.6 EDP Elliptical implicit method....................................................................................................................129
2.1 Fixed Point Method
3
2
8.7 Hyperbolic EDPs..........................................................................................................................................136

return pfix3

print (pfix2( lambda x, y: np.sqrt(x - y + 0.75), lambda x, y: (x**2 - y) / (5*x),


1.2, 1.2))

Yo xi and i %Etx %Ety


0 1.200000 1.200000

1 0.866025 -0.103923 36.881605 1.433913


2 1.311468 0.278142 4.416478 0.161335
3 1.335412 0.225426 2.671351 0.058771
4 1.363813 0.239704 0.601421 0.000846

5 1.368981 0.238777 0.224778 0.003028


Table 2.1: Fixed point for 2x2 nonlinear systems
2.2 Newton Raphson method
3
2.2 Newton Raphson method 3
(Exercise No. 6.12 / Page. 168)

Determine the roots of the following simultaneous nonlinear equations, by means of the methods two of a) fixed
point iteration, and b) Newton-Raphson:

y = - x 2 + x + 0.75
.e2
y+5x=x

Use initial values of x = y = 1.2, and analyze the results.

Python code - Newton Raphson for nonlinear systems

"""Newton Raphson method for nonlinear systems""" import numpy as np


import matplotlib.pyplot as plt

def Newton_RapshonSNL(u,v,u_x, u_y, v_x, v_y, x0, y0, n=5):


x = np.zeros(n+1)
y = np.zeros(n+1)
x[0] = x0
y[0] = y0
print ( "-" *115)
print ( " Newton Raphson for 2 equations " )
print ( "-" *115)
print ( "{:^5}{:^12}{:^12}{:^12}{:^12}{:^12}{:^12}{:^12} {:^12}{: ^12}" . format ( "i" , "xi" , "yi" ,
"ui" ,
"vi" , "u_x" , "u_y" , "v_x" , "v_y" , "Jacobian" )) print ( "-" *115)
for i in range (0, n): Jacobian = u_x(x[i],y[i]) * v_y(x[i],y[i]) - u_y(x[i],y[i]) * v_x(x[i],y[i]) x[i+1] =
x[i] - (u(x[i],y[i]) * v_y(x[i],y[i] ) - v(x[i],y[i]) * u_y(x[i],y[i])) / Jacobian y[i+1] = y[i] -
( v(x[i], y[i]) * u_x(x[i],y[i]) -u(x[i],y[i]) * v_x(x[i],y[i])) / Jacobian print (f "{i+1:^3} &
{x[i]:.8f} & {y[i]:.8f} & {u(x[i],y[i]):^.6f} & { v(x[i],y[i]):^.6f} & " f "{u_x(x[i],y[i]):^.8f} &
{u_y(x[i],y[ i]):^.6f} & {v_x(x[i],y[i]):^.6f} & " f "{v_y(x[i],y[i]):^.6f} & {jacobian:^.8f}" )
2.2 Newton Raphson method
3
print ( "-" *115) 4
# Here enter the functions and initial data.
x
print (Newton_RapshonSNL( lambda x, an -and- **2 +x+0.75,
d:
lambda x, an x**2 -and- 5 * x * y,
d:
lambda x, an -2 * x + 1,
d:
lambda x, an -1,
d:
lambda x, an 2*x- 5*y,
d:
lambda x, an -5 * x - 1,
d:
1.2, 1 2))
Solution

Yo xi and i ui saw ∂u / ∂x ∂u/∂y ∂v / ∂x ∂v / ∂y Jacobi anus


1 1.200000 1.200000 -0.690000 -6.960000 -1.400000 -1.000000 -3.600000 -7.000000 6.200000

2 1.543548 0.029032 -0.118025 2.129446 -2.087097 -1.000000 2.941935 -8.717742 21.136707


3 1.394123 0.222872 -0.022328 0.167151 -1.788246 -1.000000 1.673885 -7.970615 15.927304
4 1.372455 0.239293 -0.000470 0.002249 -1.744909 -1.000000 1.548447 -7.862273 15.267399

5 1.372066 0.239502 -0.000000 0.000001 -1.744131 -1.000000 1.546622 -7.860328 15.256063


Table 2.2: Newton Raphson for 2x2 nonlinear systems The solution is x = 1.372066 and y = 0.239502

To solve the following problems in this section on 2x2 linear systems in Crapra's book we will use the same code, but we will
change the data requested in the function.

Exercise 2.3 ( Exercise No. 6.13 / Page. 168)

Find the roots of the following simultaneous equations:

( x - 4) 2 + ( y - 4) 2 = 5

x 2 + y 2 = 16

Use a graphical approach to obtain the initial values. Find refined estimates using the Newton-Raphson method for two equations,
described in section 6.5.2.

N Since we are asked to graph to obtain the initial values, we will add the following to our code.

Python code

import numpy as np
import matplotlib.pyplot as plt # Plotting the system.
2.2 Newton Raphson method
3
x = np.linspace(-5, 7, 100) 5
plt.plot(x, np.sqrt(5 - (x - 4)**2) + 4)
plt.plot(x, np.sqrt(16 - x**2))
plt.plot(x, -np.sqrt(5 - (x - 4) ** 2) + 4) plt.plot(x, -np.sqrt(16 - x ** 2)) plt.xlabel( 'x ' )
plt.ylabel( 'y' )
plt.grid()
plt.show()

Figure 2.1: function y = 5 - ( x - 4) 2 + 4 and y = - 16 - x 2

import numpy as np
import matplotlib.pyplot as plt # Plotting the system.

x = np.linspace(1.6, 3.8, 100)


plt.plot(x, np.sqrt(16 - x**2))
plt.plot(x, -np.sqrt(5 - (x - 4) ** 2) + 4)
plt.xlabel( 'x' )
plt.ylabel( 'y' )
plt.grid()
plt.show()
2.2 Newton Raphson method
3
6

Figure 2.2: function y = p 5 - ( x - 4) 2 + 4 and y = - p 16 - x 2

First we graph the function by solving for and and zoom in to see the value where they intersect.
We observe that they approach x = 2.0 and y = 3.5 and also at x = 3.5 and y = 2.0

Yo xi and i ui saw ∂u / ∂x ∂u/∂y ∂v / ∂x ∂v / ∂y Jacobian


1 2.000000 3.500000 -0.750000 0.250000 -4.000000 -1.000000 4.000000 7.000000 -24.000000

2 1.791667 3.583333 0.050347 0.050347 -4.416667 -0.833333 3.583333 7.166667 -28.666667


3 1.805717 3.569283 0.000395 0.000395 -4.388566 -0.861434 3.611434 7.138566 -28.217054
4 1.805829 3.569171 0.000000 0.000000 -4.388342 -0.861658 3.611658 7.138342 -28.213472

5 1.805829 3.569171 0.000000 0.000000 -4.388342 -0.861658 3.611658 7.138342 -28.213472


Table 2.3: Newton Raphson for 2x2 nonlinear systems The solution is x = 1.805829 and y = 3.569171

Yo xi and i ui saw ∂u / ∂x ∂u/∂y ∂v / ∂x ∂v / ∂y Jacobian


1 3.500000 2.000000 -0.750000 0.250000 -1.000000 -4.000000 7.000000 4.000000 24.000000

2 3.583333 1.791667 0.050347 0.050347 -0.833333 -4.416667 7.166667 3.583333 28.666667


3 3.569283 1.805717 0.000395 0.000395 -0.861434 -4.388566 7.138566 3.611434 28.217054
4 3.569171 1.805829 0.000000 0.000000 -0.861658 -4.388342 7.138342 3.611658 28.213472

5 3.569171 1.805829 0.000000 0.000000 -0.861658 -4.388342 7.138342 3.611658 28.213472


Table 2.4: Newton Raphson for 2x2 nonlinear systems The solution is x = 3.569171 and y = 1.805829
Exercise 2.4 ( Exercise No. 6.14 / Page. 168)

Repeat Problem 6.13, except that


y=x2+1

y = 2cos x

Solution
2.2 Newton Raphson method
3
First we graph the function by solving for and and zoom in to see the value where they intersect. 7
We observe that they approach x = 0.6 and y = 1.6

Python code

import numpy as np
import matplotlib.pyplot as plt

##########################
# Graphing the system.

x = np.linspace(0, 1, 100) plt.plot(x, x ** 2 + 1) plt.plot(x, 2*np.cos(x)) plt.xlabel( 'x' )


plt.ylabel( 'y' ) plt.grid() plt.show()

Figure 2.3: function y = x 2 + 1 and y = 2cos x


2.3 Broyden method
3
8
Yo xi and i ui saw ∂u / ∂x ∂u/∂y ∂v / ∂x ∂v / ∂y Jacobi anus
1 0.600000 1.600000 -0.240000 0.050671 1.200000 -1.000000 -1.129285 -1.000000 -2.329285

2 0.724790 1.509748 0.015573 -0.012470 1.449580 -1.000000 -1.325956 -1.000000 -2.775536


3 0.714686 1.510674 0.000102 -0.000077 1.429373 -1.000000 -1.310761 -1.000000 -2.740134
4 0.714621 1.510683 0.000000 -0.000000 1.429242 -1.000000 -1.310662 -1.000000 -2.739905

5 0.714621 1.510683 0.000000 -0.000000 1.429242 -1.000000 -1.310662 -1.000000 -2.739905


Table 2.5: Newton Raphson for 2x2 nonlinear systems The solution is x = 0.714621 and y = 1.510683

2.3 Broyden method

Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)

Determine the roots of the following simultaneous nonlinear equations, using the methods two of a) fixed point iteration, and
b) Newton-Raphson:

y = - x 2 + x + 0.75

.e2
y+5x=x

Use initial values of x = y = 1.2, and analyze the results.

Exercise 2.6 | (Exercise No. 6.13 / Page. 168)

Find the roots of the following simultaneous equations:

( x - 4) 2 + ( y - 4) 2 = 5

x 2 + y 2 = 16

Use a graphical approach to obtain the initial values. Find refined estimates using the Newton-Raphson method for two
equations, described in section 6.5.2.

Exercise 2.7 ( Exercise No. 6.14 / Page. 168)


2.3 Broyden method
3
Repeat Problem 6.13, except that
y=x2+1
9
y = 2cos x
3
5

Direct methods for systems of equations


linear tions

3.1 General Gauss Method


For the solution with Gauss proceed with the augmented matrix to apply: Forward elimination of unknowns:

ak , i
Ak=Ak-Ai
a
i,i

backward substitution, using the formula:

bi i-1 P ni - 1 j =ai +x 1j
ij
xi=

i - 1 ii
for i = n - 1, n - 2

Exercise 3.1 ( Exercise No. 9.8 page. 280)

Given the following equations:

10 x 1 + 2 x 2 - x 3 = 27
- 3 x 1 - 6 x 2 + 2 x 3 = - 61.5
x1+x2+5x3= -21,5

Solve by simple Gaussian elimination. a) Carry out all the steps of the calculation. b) Substitute the results into
the original equations in order to check your answers.

Code in Matlab
%GAUSS METHOD GENERAL
clc , clear
n= input ( 'Enter the number of equations: ' )
disp ( 'Enter the coefficients of the equations: ' ) for i=1:n for j=1:n
fprintf ( 'A ( %d, %d): ' ,i,j)

A(i,j)= input ( '' );


end
end
3.1 General Gauss Method
3
disp ( 'Enter the independent terms of the equations: ' ) for k=1:n
6
fprintf ( 'A ( %d, %d): ' ,k,n+1)
A(k,n+1)= input ( '' );
end

disp ( 'The expanded array that was formed is as follows: ' )


A=A

disp ( 'The forward deletion will then be performed. ' ) x=1;


while (x<n) for s=1:n-1 for l=x:n-1
A(l+1,:)=A(s,:)*(-A(l+1,s)/A(s,s))+A(l+1,:); end x=x+1; end end
disp ( 'The upper triangular matrix that was formed was the following: ' ) A=A
X(n)=A(n,n+1)/A(n,n); for h=n-1:-1:1 S=A(h,n+1); for f=n:-1:1 S=SA(h,f)*X(f); end S=S/A(h,h);
X(h)=S; end
disp ( 'Result:' ) disp ( '- ’)
for r=1:n fprintf ( 'X%d = %f ' ,r,X(r)) end

N For the following exercises we will use the same code, but we will change the functions to be analyzed.

Solution The expanded matrix that was formed is the following:


3.1 General Gauss Method
3
‘ 10,0000 27,0000 )
7
2,0000 -1,0000
A= -3,0000 -6,0000 2,0000 •61,5000
1,0000 1,0000 5,0000 21,5000 )
Next, forward deletion will be performed. The upper triangular matrix that was formed was the following:

‘ 10,0000 2,0000 -1,0000 27,0000 )


A= 0 -5,4000 1,7000 -53,4000
0 5,3519 -32,1111 )
0
Result:
X 1 = 0.500000, X 2 = 8.000000, X 3 = - 6.000000

Exercise 3.2 ( Exercise 9.9 page-280)

Use Gaussian elimination to solve the following system:

Nonlinear equations..................................................................................................................................5
1.1 Bisection Method.......................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers................................................................................................................................67
Numerical integration.............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
3.1 General Gauss Method
3
8
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136

Next, forward deletion will be performed. The upper triangular matrix that formed
was the following:
‘ 8,0000 2,0000 -2,0000 -2,0000 )

=, 0
-0,5000 6,5000 6,5000
-4,0000 )
A 0 0 -8,0000
Result:

Nonlinear equations..................................................................................................................................5
1.1 Bisection Method.......................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.1 General Gauss Method
3
9
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers................................................................................................................................67
Numerical integration.............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136

a) Calculate the determinant. b) Use Cramer's rule to find what the values of the
3.2 Gauss Jordan method
4
x. c) Use Gaussian elimination with partial pivoting to obtain what the x values would be.
0
Solution

Exercise 3.4 ( Exercise 9.11/page-281)

Given the equations


2x1-6x2-x3= -38
- 3 x 1 - x 2 + 7 x 3 = - 34
- 8 x 1 + x 2 - 2 x 3 = - 20

a) Solve by Gaussian elimination with partial pivoting. Carry out all the calculation steps. b) Substitute the results
into the original equations to check your answers.

Solution
Part A)
The expanded matrix that was formed is the following:

‘2 -6 -1 -38 )
A= -3 -1 7 -34
, -8 1 -2 -20 )

Next, forward deletion will be performed. The upper triangular matrix that was formed was the following:

‘ 2,0000 -6,0000 -1,0000 -38,0000 )

0
A= -10,0000 5,5000 -91,0000
37,3000 )
0
,
0 -18,6500
Result:

X 1 = 4.000000, X 2 = 8.000000, X 3 = - 2.000000


Part b): We substitute the results into the equations

2(4) - 6(8) - (-2) = -38


-3(4) - (8) + 7(-2) = -34
-8(4) + (8) - 2(-2) = -20

3.2 Gauss Jordan method


For the solution with Gauss-Jordan, we proceed with the augmented matrix to apply:
forward elimination of unknowns.
backward elimination of unknowns.
The augmented matrix AB is created. For the forward elimination procedure, in each row the diagonal element will
be the pivot, with which the coefficient for forward term elimination is created.
For backward elimination, the row is normalized by making the diagonal element one, to calculate the coefficient.
3.2 Gauss Jordan method
4
(Exercise n 9.12/page-281)
1
Exercise 3.5 Gauss-Jordan elimination to solve the following system:
Use the
2x1+x2-x3=1
5x1+2x2+2x3=-4
3x1+x2+x3=5

Do not use pivoting. Check your answers with the substitution in the original equations.

Code in Matlab
% Gauss method Jordan clear all ;
clc ;
fprintf ( 'give me the augmented metric\n\n' );
f= input ( 'how many rows does the matrix have: ' ); c= input ( 'how many columns does the matrix
have: ' ); for k=1:c for j=1:f fprintf ( 'row : %x\n' ,j) fprintf ( 'column : %x' ,k) r= input ( 'number of this
row and column: ' ) ;
a(j,k)=r;
j=j+1;
end
k=k+1; end to
pause for k=1:c-1
a(k,:)=a(k,:)/a(k,k);
for j=k+1:f
a(j,:)=a(j,:)-a(k,:)*a(j,k);
j=j+1; to
pause end k=k+1; to
pause

end for k=f:-1:2 for j=k-1:-1:1 a(j,:)=a(j,:)-a(k,:)*a(j,k);


3.2 Gauss Jordan method
4
j=j-1;
2
a pause end k=k-1; to pause end
fprintf ( 'result\n' );

Solution

2 1 -1 1
5 2 2 -4
3 1 1 5

A=

( 0,5000 -0,5000 0,5000 )


1,0000 0 -0,5000 4,5000 -6,5000
3,0000 1,0000 1,0000 5,0000 J.
Y
( 1,0000 0,5000 -0,5000 0,5000 N
0 -0,5000 4,5000 -6,5000
0 -0,5000 2,5000 3,5000 J.
T
( 1,0000 0,5000 -0,5000 0,5000
O
0 -0,5000 4,5000 -6,5000
0 -0,5000 2,5000 3,5000
T
( 1,0000
0
0,5000 -0,5000
1,0000 -9,0000
0,5000
13,0000
O

0 0 -2,0000 10,0000
1
( 1,0000
0
0,5000 -0,5000
1,0000 -9,0000
0,5000
13,0000
\

0 0 -2,0000 10,0000
1
( 1,0000 0,5000 -0,5000 0,5000 \
0 1,0000 -9,0000 13,0000
0 0 1,0000 -5,0000 J.
1
( 1,0000 0,5000 -0,5000 0,5000 \
0 1,0000 0 -32,0000
0 0 1,0000 -5,0000 J.

( 1,0000
0
0,5000
1,0000
0
0
-2,0000
-32,0000
\

0 0 1,0000 -5,0000
1
( 1,0000
0
0,5000
1,0000
0
0
-2,0000
-32,0000
\

0 0 1,0000 -5,0000
1 Result
)

A=

‘ 1 0 0 14 )
A=

aT==
A
O
3.2 Gauss Jordan method
4
A= 0 10 -32
3
, 0 0 1 -5 )
Substituting into the equations

2(14) + (-32) - (-5) = 1


5(14) + 2(-32) + 2(-5) = -4
3(14) + (-32) + (-5) =5

Exercise 3.6 ( Exercise 9.13 / page-281)

Solve system:
x1+x2-x3=-3
6x1+2x2+2x3=2
-3x1+4x2+x3=1

by means of a) simple Gaussian elimination, b) Gaussian elimination with partial pivoting, and c) Gauss-Jordan
method without partial pivoting.

Solution by Gaussian Elimination The expanded matrix that was formed is the one next:
‘1 1 -1 -3 )
Nonlinear equations..................................................................................................................................5
1.1 Bisection Method.......................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers................................................................................................................................67
3.2 Gauss Jordan method
4
4
Numerical integration.............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136

Result:
X 1 =- 0.250000, X 2 =- 0.500000, X 3 = 2.250000

N For this exercise we use the Matlab code called Gauss General

Solution using Gauss Jordan without partial pivoting


‘1 1 -1 -3 )
Nonlinear equations..................................................................................................................................5
1.1 Bisection Method.......................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
3.2 Gauss Jordan method
4
5
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers................................................................................................................................67
Numerical integration.............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
3.2 Gauss Jordan method
4
6
8.7 Hyperbolic EDPs.........................................................................................................................136

Yo
3.2 Gauss Jordan method
4
7
11 -1 -3
A = 0 -4 8 20
07 -2 -8

11 -1 -3
A = 0 -4 8 20
07 -2 -8

/11 -1 -3 )
A= 01 -2 -5
00 12 27

/11 -1 -3 )
A= 01 -2 -5
00 12 27

1,0000 1,0000 -1,0000 -3,0000 )


A= 0 1,0000 -2,0000 -5,0000
0 0 1,0000 2,2500

1,0000 1,0000 -1,0000 -3,0000 )


A= 0 1,0000 0 -0,5000
0 0 1,0000 2,2500

( 1,0000 1,0000 0 -0,7500 )


A= 0 1,0000 0 -0,5000
0 1,0000 2,2500
0
f
1.0000 1,0000 0 -0,7500 )
A= 0 1,0000 0 -0,5000
0 1,0000 2,2500
0
f
1.0000 0 0 -0,2500 )
A= 0 1,0000 0 -0,5000
0 1,0000 2,2500
Result
0
( 1,0000 0 0 -0,2500 )
A= 0 1,0000 0 -0,5000
0 1,0000 2,2500
0
4
8

Iterative methods for systems of equations


linear tions

4.1 Gauss Seidel


The method is described with a system of 3 unknowns and 3 equations.
Note the index usage adjusted to the python description for first row=0 and first column=0:

a 00 x 0 + a 01 x 1 + a 02 x 2 = b 0

a 10 x 0 + a 11 x 1 + a 12 x 2 = b 1

a 20 x 0 + a 21 x 1 + a 22 x 2 = b 2

The matrix form of the original system of equations is:[ A ] X = B


The indices i for row and j for column are used, thus one element of the matrix is a ij .
To obtain the values of each x i , one is solved for each row i :

x 0 = ( b 0 - a 01 x 1 - a 02 x 2 )/ a 00

x 1 = ( b 1 - a 10 x 0 - a 12 x 2 )/ a 11

x 2 = ( b 2 - a 20 x 0 - a 21 x 1 )/ a 22

Notice the pattern of each formula used to determine x [ i ]:

n1bi-X Ai,jXj
xi= j = 0, j 6= i A
ii

the sum for each term of A [ i , j ] in row i , except for the


diagonal term A [ i , i ]
If you have knowledge of the problem posed and you can "intuit or assume" a solution for the vector X. For example,
starting with the zero vector, it is possible to calculate a new vector X using the equations for each X [ i ] found.

X = [0,0,0]

With each new value, the vector differences between the vector X and each new calculated value X [ i ] are calculated.

The error that draws attention is the highest value of the differences, and is taken as a condition to repeat the
evaluation of each vector.
4.1 Gauss Seidel
4
new = [0, 0, 0]
9
X = [ x 0, x 1, x 2]

difference = [ | 0 - x 0 | , | 0 - x 1 | , | 0 - x 2 | ]

er rate = max ( difference )

The error results for each iteration are observed, related to convergence. If after "many" iterations it is found that
(error>tolerates), the process is stopped.
With what has been described, a way to implement the algorithm as a function and a test example with data from the
known problem are shown.

Code in Python - Gauss Seidel

# Gauss-Seidel algorithm,
# arrays, iterative methods
# enter iteramax if more iterations are required

import numpy as np

def gauss_seidel(A,B,tolera,X,iteramax=100): size = np.shape(A) n = size[0] m = size[1] difference =


np.ones(n, dtype= float ) wrong = n.p. max (difference)

iterate = 0
while not (error<=tolera or itera>iteramax): for i in range (0,n,1): new = B[i] for j in range
(0,m,1):
if (i!=j): # except diagonal of A new = new-A[i,j]*X[j]
new = new/A[i,i] difference[i] = np. abs (new-X[i]) X[i] = new
wrong = np. max (difference) iterate = iterate + 1
# Vector in column X = np.transpose([X]) # Does not converge if (itera>iteramax):
4.2 Jacobbi
5
0
X=0 return (X)

# Test Program ####### # INCOME


A = np.array([[3,-0.1,-0.2], [0.1,7,-0.3], [0.3,-0.2,10]])

B = np.array([7.85,-19.3,71.4])

tolerate = 0.00001

# PROCEDURE
n = len (B)
X = np.zeros(n, dtype= float )
response = gauss_seidel(A,B,tolerates,X) check = np.dot(A,response)

# EXIT
print ( 'response of AX=B : ' ) print (response) print ( 'verify AX: ' ) print (verify)

To solve the following problems, we will change the input of data in the code.

4.2 Jacobbi
Python code - Jacobbi

import numpy as np

#A = np.asmatrix([[10,-1,2,0],[-1,11,-1,3],[2,-1,10,-1],[0,3,-1 ,8]])
#B = np.asmatrix([[6],[25],[-11],[15]])

A = np.asmatrix([[4, -1, 0],[-1, 4, -1],[0, -1, 4]])


B = np.asmatrix([[2],[6],[2]])

def Jacobi3x3(A,B):
x = np.asmatrix(np.zeros( len (A),1))
xk = np.asmatrix(np.zeros(( len (x),1))) RE = 1

#precision specified in loop - not very specific


while RE >= 0.0009:
xk[0] = (B[0] - (A[0,1] * x[1]) - (A[0,2] * x[2])) / A[0,0]
xk[1] = (B[1] - (A[1,0] * x[0]) - (A[1,2] * x[2])) / A[1,1]
xk[2] = (B[2] - (A[2,0] * x[0]) - (A[2,1] * x[1])) / A[2,2]
RE =(np.linalg.norm(xk - x,np.inf)) / (np.linalg.norm(xk,np.inf) + 0)
x = xk.copy()

print (x[0], "\t" ,x[1], "\t" ,x[2], "\t" ,RE) def Jacobi(A,B):
x = np.asmatrix(np.zeros(( len (A),1)))
xk = np.asmatrix(np.zeros(( len (x),1)))
4.2 Jacobbi
5
1
RE = 1
trial_no = 1
print ( "trial x1 x2 x3 ...")

while RE > 0.0009:


for i in range (0, len (x)):
xk[i] = (B[i] + eqn(A[i], x,i)) / A[i,i]

RE = (np.linalg.norm(xk - x,np.inf)) / (np.linalg.norm(xk,np.inf) x = xk.copy() print ( str (trial_no)+ "\


t" + str (xT)+ "\t" + str (RE)) trial_no +=1
print ( "trial x1 x2 x3 ...")

return x def eqn(w,x,I):

w = -1 * w
res = np.multiply(wT,x)
res[I] = 0
sum = np. sum (res)
return sum

Jacobi(A, B)
4.3 Problems

Exercise 4.1 ( Exercise nº 11.7 / page 324)

a) Use the Gauss-Seidel method to solve the tridiagonal system of Problem 11.1 (²s = 5 %).
b) Repeat part a) but use overrelaxation with λ = 1.2.

Solution
# INCOME
A = np . ar ray ([[0.8, - 0.4, 0],
[-0,4, 0,8, -0,4],
[0, -0,4, 0,8]])

B = np . array ([41,25, 105])


tolerates = 0.05
# EXIT
response from A. X = B :

[[173,70407104]

[244,95407104]

[253,72703552]]

check A. X :
[[40,98162842]

[24,99081421]

[105.]]
4.2 Jacobbi
5
2
Exercise 4.2 ( Exercise No. 11.8 / page 324)

From Problem 10.8, recall that the following system of equations is designed to determine concentrations (c are in
g / m 3 ) in a series of coupled reactors as a function of the amount of mass input to each of them (the right sides
are in g / d ),

15 c 1 - 3 c 2 - c 3 = 3800

- 3 c 1 + 18 c 2 - 6 c 3 = 1200

- 4 c 1 - c 2 + 12 c 3 = 2350

Solve this problem with the Gauss-Seidel method for ² s = 5%.

Solution
# INCOME
A = np . array ([[15, - 3, - 1],

[-3,18,-6],
4.3 Problems
5
3
[-4, -1, 12]])

B = np . array ([3800, 1200,2350])

tolerates = 0.05

# EXIT
response from A. X = B :
[[320,20103859]
[227,19666023]
[321,50006788]]

check A. X :
[[3799,92553032]
[1199,93636106]
[2350.]]

Exercise 4.3 ( Exercise No. 11.10 / page 325)

Use the Gauss-Seidel method to solve the following system until the percent relative error is below ² s = 5%.

10 x 1 + 2 x 2 - x 3 = 27

-3x1-6x2+2x3

x 1 + x 2 + 5 x 3 = - 21.5

Solution
# INCOME
A = np . array ([[10,2, - 1],

[-3,-6,2], [1, -1, 5]])

B = np . array ([27, - 61.5, - 21.5])

tolerates = 0.05

# EXIT
response from A. X = B :
[[0,6289026]
[9,06176403]
[-2,61342771]]
4.3 Problems
5
4
check A. X :
[[27,02598178]
[-61,48414743]
[-21,5]]

Exercise 4.4 I ( Exercise No. 11.11 / page 325)

Use the Gauss-Seidel method (a ) without relaxation, and ( b ) with relaxation ( λ = 0.95), to solve the following system
for a tolerance of ²s = 5%. If necessary, rearrange the equations to achieve convergence.
- 3 x 1 + x 2 - 12 x 3 = 50

6x1-x2-x3=3
6 x 1 + 9 x 2 + x 3 = 40

Solution
# INCOME
A = np . array ([[10,2, - 1],

[-3,-6,2],
[1, -1, 5]])

B = np . array ([27, - 61.5, - 21.5])

tolerates = 0.05

# EXIT
response from A. X = B :
[[0,6289026]

[9,06176403]

[-2,61342771]]

check A. X :
[[27,02598178]

[-61,48414743]

[-21,5]]

Exercise 4.5 ( Exercise No. 11.12 / page 325)


Use the Gauss-Seidel method ( a ) without relaxation, and ( b ) with relaxation ( λ = 1.2), to solve the following system
for a tolerance of ²s = 5%. If necessary, rearrange the equations to achieve convergence.
2 x 1 - 6 x 2 - x 3 = - 38
4.3 Problems
5
5
- 3 x 1 - x 2 - 2 x 3 = - 34

- 8 x 1 + x 2 - 2 x 3 = - 20

Solution
# INCOME
A = np . array ([[ - 8,1, - 2],

[2, -6, -1],

[-3, -1, 7]])

B = np . ar ray ([ - 20, - 38, - 34])

tolerates = 0.05

# EXIT
response from A. X = B :
[[3,99875792]

[7,99945128]

[-2,00061071]]

check A. X :
[[-19,98939064]

[-37,99858113]

[-34.]]

Exercise 4.6 I (Exercise no. 11.23 / page 326)

Develop a user-friendly program in any high-level or macro language of your choice to execute the
Gauss-Seidel method based on Figure 11.6. Try it with the
repetition of the results of example 11.3.
example 11.3.

' 2,01475 -0,020875 'T1 ' ' 4,175


-0,020875 2,01475 -0,020875 T2 0
x
-0,020875 2,01475 -0,020875 T3 0
-0,020875 2,01475 T4 2,0875

Solution
# INCOME
A = np . ar ray ([[2.01475, - 0.020875, 0, 0],

[ - 0.020875, 2.01475, - 0.020875, 0],

[0, - 0.020875,2.01475, - 0.020875],


4.3 Problems
5
6
[0, 0, -0,020875, 2,01475]])

B = np . array ([4,175,0,0,2,0875])

tolerates = 0.05
# EXIT
response from A. T = B :

T1 = 2.07243985
T2 = 0.02147503
T3 = 0.01095774
T4 = 1.03622223

check A. T :

[[4.17499990e + 00 ]
[ - 2.24099053 e - 04]
[ - 2.32190978 e - 06]
[2.08750000e + 00 ] ]

Exercise 4.7 ( Exercise No. 12.34 / pages 345-346)

Three blocks are connected by weightless ropes and left at rest on an inclined plane (see figure P 12.34 a ). Using a
procedure similar to that used in the analysis of the descending parachutist in Example 9.11, we arrive at the following
set of simultaneous equations (free-body diagrams are shown in Figure P 12.34 b ):

100 to + T = 519,72
50 to - T = 216,55
25 to - R = 108,27

Solve for the acceleration a and the tensions T and R in the two ropes.

Solution

(0)
x (0) = 0

( 5,1972 )
x (1) = -216,55

\-108,27
‘ 7,3627 )
x (2) = -64,96

\ 21,66 )
4.3 Problems
5
7

Figure 4.1: Figure 12.34

‘ 5,8468 )
(3) x 173,245
75,7975

‘ 4,83375305 )
(twenty) 32,7020874
x=
10,78948975)
‘ 4,87017913 )
(twenty- 35,92714233
one)
x= 12,57382629 )

a = 4.87017913
T = 35.92714233
R = 12.57382629
5
8

Interpolation

5.1 Finite differences


In the finite difference table, if the values on the x- axis are equally spaced, the difference rence is a constant called h =
Xi+1-Xi
A relationship between derivatives and finite differences is established by:
f ( n
) ( z ) = ∆nf0
hn
For some interval [ x 0 , x n ]

∆nf0
hn
is an approximation for f ( n ) in the interval [ x 0 , x n ] Advanced finite difference interpolation polynomial:

∆f0 ∆2f0 ∆3f0 ∆nf0


Pn ( x ) = f0 + ( x − x 0 ) + 2 ( x - x 0 )( x - x 1 ) + 3 ( x - x 0 )( x - x 1 )( x - x 2 ) + · · · + ( xn - x 0 )( x - x 1 ) ... ( x - x n -
1)n!h
h 2! h 3! h
The polynomial can be done using the finite difference exercise. The symbolic variable is created with sympy, the
polynomial is generated by adding the terms of the operation.
To simplify, the multiplication operations are expanded, and the polynomial is obtained.
If you need to evaluate the formula with a data vector, it is preferably converted to lambda form.

Python Code - Advanced Finite Differences

# Advanced Finite Differences for Polynomial Interpolation


# Reference Rodriguez 6.6.4 Pdf.221
# Task: Check vector size
# consider points not equidistant on the x axis
import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
# INCOME, Test data
xi = np.array([0.10, 0.2, 0.3, 0.4]) fi = np.array([1.45, 1.6, 1.7, 2.0])

# PROCEDURE
# Finite difference table title = [ 'i' , 'xi' , 'fi' ] n = len (xi) # change to column form
5.1 Finite differences
5
i = np.arange(1,n+1,1)
9
i = np.transpose([i])
xi = np.transpose([xi]) fi = np.transpose([fi]) # Add dfinite difference matrix =
np.zeros(shape=(n,n),dtype= float ) table = np.concatenate(( i,xi,fi,dfinite), axis=1) # On difference
matrix, by columns [n,m] = np.shape(table) c=3
diagonal = n-1 while (c<m):
# Increase the title for each column title.append( 'df' + str (c-2))
# calculate each difference per row f=0 while (f < diagonal):
table[f,c] = table[f+1,c-1]-table[f,c-1] f = f+1

diagonal = diagonal - 1 c = c+1

# POLYNOMIAL with finite differences


# case: points on x-axis equidistant dfinite = table[:,3:]
n = len (dfinite)
x = sym.Symbol( 'x' ) h = xi[1,0]-xi[0,0] polynomial = fi[0,0] for c in range (1,n,1):
denominator = np.math.factorial(c) * (h ** c) factor = dfinite[0,c-1]/denominator term=1
for f in range (0,c,1):
term = term * (x-xi[f])

polynomial = polynomial + term * factor # simplify polynomial, multiply the (x-xi) polynomial
= polynomial.expand() # for numerical evaluation px = sym.lambdify(x,polynomial)

# Points for the graph a = np. min (xi) b = np. max (xi) samples = 101
xi_p = np.linspace(a,b,samples) fi_p = px(xi_p)

# EXIT
print ([title])
print (table)
print ( 'polynomial:' ) print (polynomial)

# Graph
plt.title( 'Polynomial interpolation' ) plt.plot(xi,fi, 'o' , label = 'Points' ) plt.plot(xi_p,fi_p, label =
'Polynomial' ) plt.legend() plt.show( )

resulting:

Yo xi f ( xi ) 1DD 2DD 3DD


1. 0.1 1.45 0.15 -0.05 0.25

2. 0.2 1.6 0.1 0.2 0.


3. 0.3 1.7 0.3 0. 0.

4. 0.4 2. 0. 0. 0.

Table 5.1: Advanced finite differences for polynomials


5.1 Finite differences
6
0
polynomial:

1.5 ∗.1) + 1.3

expanded polynomial:

41.6666666666667 x 3 - 27.5 x 2 + 6.83333333333335 x + 0.999999999999999

Figure 5.1: Finite interpolation with 4 points

5.2 Differences Divided - (Newton Polynomial)


It is used in the case that the points on the x- axis are arbitrarily spaced and come from an unknown but supposedly
differentiable function.
The nth finite divided difference is:
f [ x n , x n - 1 , ··· , x 1 ] - f [ x n - 1 , x n - 2 , ··· , x 0 ] f [ x n , x n - 1 , ··· ,
x1,x0]=
xn-x0
For which you must interpret the divided differences table:

Yo x i f [ xi ] 1DD 2DD 3DD

0 x0 f [ x0 ] f [ x1 , x0 ] f [ x2 , x1 , x0 ] f [ x3 , x2 , x1 , x0 ]
1 x1 f [ x1 ] f [ x2 , x1 ] f [ x3 , x2 , x1 ]

2 x2 f [ x2 ] f [ x3 , x2 ]

3 x3 f [ x3 ]
Table 5.2: Interpolation - Split Differences

The differences are used to evaluate the coefficients and obtain the interpolation polynomial:

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

It is known as the divided difference Newton interpolation polynomial.

Code in Python - Split Differences

print ( "--------------------- Polynomial Interpolation (Newton) -----------------------------------------")


n = int ( input ( "Enter the degree of the polynomial to evaluate: " ))+1
5.1 Finite differences
6
# print "The degree of the polynomial is: ", n matrix = [0.0] * n
1
for i in range (n): array[i] = [0.0] * n

vector = [0.0] * n print (array)


print (vector) for i in range (n):
x = input ( "Enter the value of x: " )
y = input ( "Enter the value of f(" +x+ "): " ) vector[i]= float (x)
array[i][0]= float (y)

print (vector)
print (array)

point_to_evaluate = float ( input ( "Enter the point to be evaluated: " ))

Nonlinear equations....................................................................................................................................5
1.1 Bisection Method.........................................................................................................................5
1.2 Newton Raphson method.............................................................................................................5
1.3 Secant Method.................................................................................................................................15
1.4 Modified secant...............................................................................................................................18
Systems of nonlinear equations................................................................................................................28
2.1 Fixed Point Method.........................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).......................................................................................28
2.2 Newton Raphson method................................................................................................................33
2.3 Broyden method..............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)....................................................................................38
3.1 General Gauss Method....................................................................................................................35
3.2 Gauss Jordan method......................................................................................................................40
4.1 Gauss Seidel...............................................................................................................................47
Jacobbi...................................................................................................................................................49
Interpolation..............................................................................................................................................57
5.1 Finite differences........................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)..................................................................................59
5.3 Interpolation polynomial.................................................................................................................63
5.4 Lagrangian interpolation.................................................................................................................65
5.5 Cubic Tracers..................................................................................................................................67
Numerical integration...............................................................................................................................77
6.1 Newton-Cotes integration formulas...........................................................................................77
6.1.1 trapezoid rule......................................................................................................................77
5.1 Finite differences
6
2
Simpson's Rules.................................................................................................................................78
Integration of equations.........................................................................................................................91
Romberg integration..........................................................................................................................91
Gaussian quadrature..........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials...........................................................................96
Ordinary differential equations...............................................................................................................107
7.1 EDO with Taylor...........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.......................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx........................................................................................................112
7.4 Runge Kutta d2y/dx2....................................................................................................................115
partial differential equations...................................................................................................................117
8.1 EDP Parabolic..........................................................................................................................117
8.2 EDP Parabolic explicit method.....................................................................................................118
8.3 EDP Parabolic implicit method.....................................................................................................121
8.4 EDP Ellipticals.........................................................................................................................126
8.5 EDP elliptical iterative method................................................................................................127
8.6 EDP Elliptical implicit method.....................................................................................................129
8.7 Hyperbolic EDPs...........................................................................................................................136

for j in range (i,n):


print ( "i=" ,i, " j=" ,j)
print ( "(" ,array[j][i-1], "-" ,array[j-1][i-1], ")/\
(" ,vector[j], "-" ,vector[ji], ")" )
matrix[j][i] = ( (matrix[j][i-1]-matrix[j-1][i-1]) /\ (vector[j]-vector[ji]))
print ( "array[" ,j, "][" ,i, "] = " ,(array[j][i-1]-
matrix[j-1][i-1])/(vector[j]-vector[ji]))

Nonlinear equations....................................................................................................................................5
1.1 Bisection Method.........................................................................................................................5
1.2 Newton Raphson method.............................................................................................................5
1.3 Secant Method.................................................................................................................................15
1.4 Modified secant...............................................................................................................................18
Systems of nonlinear equations................................................................................................................28
2.1 Fixed Point Method.........................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).......................................................................................28
2.2 Newton Raphson method................................................................................................................33
2.3 Broyden method..............................................................................................................................38
5.1 Finite differences
6
3
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)....................................................................................38
3.1 General Gauss Method....................................................................................................................35
3.2 Gauss Jordan method......................................................................................................................40
4.1 Gauss Seidel...............................................................................................................................47
Jacobbi...................................................................................................................................................49
Interpolation..............................................................................................................................................57
5.1 Finite differences........................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)..................................................................................59
5.3 Interpolation polynomial.................................................................................................................63
5.4 Lagrangian interpolation.................................................................................................................65
5.5 Cubic Tracers..................................................................................................................................67
Numerical integration...............................................................................................................................77
6.1 Newton-Cotes integration formulas...........................................................................................77
6.1.1 trapezoid rule......................................................................................................................77
Simpson's Rules.................................................................................................................................78
Integration of equations.........................................................................................................................91
Romberg integration..........................................................................................................................91
Gaussian quadrature..........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials...........................................................................96
Ordinary differential equations...............................................................................................................107
7.1 EDO with Taylor...........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.......................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx........................................................................................................112
7.4 Runge Kutta d2y/dx2....................................................................................................................115
partial differential equations...................................................................................................................117
8.1 EDP Parabolic..........................................................................................................................117
8.2 EDP Parabolic explicit method.....................................................................................................118
8.3 EDP Parabolic implicit method.....................................................................................................121
8.4 EDP Ellipticals.........................................................................................................................126
8.5 EDP elliptical iterative method................................................................................................127
8.6 EDP Elliptical implicit method.....................................................................................................129
8.7 Hyperbolic EDPs...........................................................................................................................136

mul = 1.0
for i in range (n):
print ( "array[" ,i, "][" ,i, "]" , "=" ,array[i][i]) mul = array[i][i];
5.1 Finite differences
6
print ( "mul before loop j=" ,mul)
4
for j in range (1,i+1):
mul = mul * (point_to_evaluate - vector[j-1]) print ( "mul in cycle j=" ,mul)
# print aprx
aprx = aprx + mul

print ( "-----------------------------------------------------------------------")
print ( "-----------------------------------------------------------------------")
print ( "The approximate value of f(" ,point_to_evaluate, ") is: " , aprx)

5.3 Interpolation polynomial


Joining n points in the plane using an interpolation polynomial can be done with a polynomial of degree ( n - 1) data
points in the plane.
For example, given the 4 points in the table, it is required to generate a polynomial of degree 3 of the form:

P(x)=a0x3+a1x2+a2x1+a3
x i 0 0.2 0.3 0.4
f i 1 1.6 1.7 2.0
It is possible to generate a system of equations in base p ( x ) that passes through the points, then it is solved with one
of the known methods.

a 0 (0.0) 3 + a 1 (0.0) 2 + a 2 (0.0) 1 + a 3 = 1

a 0 (0.2) 3 + a 1 (0.2) 2 + a 2 (0.2) 1 + a 3 = 1.6

a 0 (0.3) 3 + a 1 (0.3) 2 + a 2 (0.3) 1 + a 3 = 1.7

a 0 (0.4) 3 + a 1 (0.4) 2 + a 2 (0.4) 1 + a 3 = 2.0

For the solution, an algorithm made in python is proposed, resulting in:

Interpolation polynomial:

41.6666666666667 ∗ x ∗ ∗ 3 - 27.5 ∗ x ∗ ∗ 2 + 6.83333333333333 ∗ x + 1.0

Figure 5.2: Interpolation graph (test function)

Python Code - Multiple Trapezoid Rule

# Polynomial interpolation import numpy as np


5.1 Finite differences
6
import matplotlib.pyplot as plt
5
# INCOME, Test data
xi = [0,0.2,0.3,0.4]
fi = [1,1.6,1.7,2.0]

# PROCEDURE
# Numpy Arrays xi = np.array(xi) fi = np.array(fi) n = len (xi)

# Vector B in column
B = np.array(fi)
B = fi[:,np.newaxis]

# Vandermonde D Matrix
D = np.zeros(shape=(n,n),dtype = float ) for f in range (0,n,1):
for c in range (0,n,1): power = (n-1)-c D[f,c] = xi[f]**power

# Solve augmented matrix coefficient = np.linalg.solve(D,B)

# Polynomial in symbolic form import sympy as sym


x = sym.Symbol( 'x' )
polynomial = 0
for i in range (0,n,1):
power = (n-1)-i
term = coefficient[i,0] * ((x ** power)) polynomial = polynomial + term

# Convert polynomial to function for faster evaluation px = sym.lambdify(x,polynomial)

# To graph the polynomial


k = 100
start = np. min (xi)

end = np. max (xi)


xpoints = np.linspace(start,end,k)ypoints = px(xpoints)

# Using symbolic evaluation


##sypoints = np.zeros(k,dtype=float)
# #for j in range(0,k,1):
# # ypoints[j] = polynomial.subs(x,xpoints[j])

# EXIT
print ( 'Vandermonde Matrix: ' )
print (D)
print ( 'the coefficients of the polynomial: ' ) print (coefficient)
print ( 'Interpolation polynomial: ' ) print (polynomial)

# Graph
plt.plot(xi,fi, 'o' )
5.1 Finite differences
6
plt.plot(xpoints,ypoints)
6
plt.show()

5.4 Lagrangian interpolation


The Lagrangian interpolation polynomial is a reformulation of the Newton polynomial that avoids the calculation of
divided differences. It is created as:
n
fn ( x ) = Li(x)f(xi)
i=0
n
AND xx j
-
j = 0, j 6= i x i x
j

Example:
Given the 4 points in the table, it is required to generate a polynomial of degree 3 of the form:

P(x)=a0x3+a1x2+a2x1+a3
x i 0 0.2 0.3 0.4
f i 1 1.6 1.7 2.0
Python code - Interpolation - Lagrange polynomial

# Lagrange interpolation
# Polynomial in symbolic form import numpy as np

import sympy as sym


import matplotlib.pyplot as plt

# INCOME, Test data


xi = np.array([0, 0.2, 0.3, 0.4]) fi = np.array([1, 1.6, 1.7, 2.0])

# PROCEDURE
n = len (xi)
x = sym.Symbol( 'x' )
# Polynomial
polynomial = 0
for i in range (0,n,1):
# Lagrangian term
term = 1
for j in range (0,n,1): if (j!=i):
term = term * (x-xi[j])/(xi[i]-xi[j]) polynomial = polynomial + term * fi[i]
# Expands the polynomial px = polinomial.expand() # for numerical evaluation pxn =
sym.lambdify(x,polynomial)

# Points for the graph


a = np. min (xi)
b = np. max (xi)
samples = 101
xi_p = np.linspace(a,b,samples) fi_p = pxn(xi_p)
5.1 Finite differences
6
# Exit
7
print ( 'Lagrange polynomial, expressions' ) print (polynomial)
print ()
print ( 'Lagrange polynomial: ' ) print (px)

# Graph
plt.title( 'Lagrange Interpolation' ) plt.plot(xi,fi, 'o' , label = 'Points' ) plt.plot(xi_p,fi_p, label =
'Polynomial' ) plt.legend()
plt.show()
5.5 Cubic Tracers
6
8

Figure 5.3: Lagrange interpolation for 4 points

5.5 Cubic Tracers


The aim is to obtain a third degree polynomial for each interval between nodes:

Sj(xj)=aj+bj(x-xj)+cj(x-xj)2+dj(x-xj)3

For each j = 0, 1, · · · , n - 1

Figure 5.4: Natural cubic tracer - splines

For n data, there are n - 1 intervals, four unknowns to evaluate for each interval, resulting in 4( n - 1) unknowns.
For terms ( x j + 1 - x j ) that are used several times in the development, they simplify to:

h j = xj + 1 - x j

If a n = f ( x n ):

aj+1=aj+bjhj+cjh2j+djh3j

The conditions for the systems of equations are:


1. Function values must be equal in interior nodes

Sj(xj+1)=f(xj+1)=Sj+1(xj+1)
5.5 Cubic Tracers
6
2. The first derivatives at the interior nodes must be equal
9
S0j(xj+1)=S0j+1(xj+1)

3. The second derivatives at the interior nodes must be equal

S0j0(xj+1)=S0j0+1(xj+1)
4. The first and last functions must pass through the extreme points
5. One of the boundary conditions is satisfied:
5 a . free or natural boundary: The second derivatives at the extreme nodes are zero

S 00 ( x 0 ) = S 00 ( x n ) = 0

5b . subject boundary: the first derivatives at the extreme nodes are known

S0(x0)=f0(x0)

S0(xn)=f0(xn)

Following the proposed development, the following algorithm is obtained:

Python Code - Cubic Splines

# Natural cubic tracer


import numpy as np
import sympy as sym

def trace3natural(xi,yi):
n = len (xi)

# h values
h = np.zeros(n-1, dtype = float ) for j in range (0,n-1,1):
h[j] = xi[j+1] - xi[j]

# System of equations
A = np.zeros(shape=(n-2,n-2), dtype = float )
B = np.zeros(n-2, dtype = float )
S = np.zeros(n, dtype = float )
A[0,0] = 2*(h[0]+h[1])
A[0,1] = h[1]
B[0] = 6*((yi[2]-yi[1])/h[1] - (yi[1]-yi[0])/h[0])

for i in range (1,n-3,1):


A[i,i-1] = h[i]
A[i,i] = 2*(h[i]+h[i+1])
A[i,i+1] = h[i+1]
B[i] = 6*((yi[i+2]-yi[i+1])/h[i+1] - (yi[i+1]-yi[i])/h[i])
A[n-3,n-4] = h[n-3]
A[n-3,n-3] = 2*(h[n-3]+h[n-2])
B[n-3] = 6*((yi[n-1]-yi[n-2])/h[n-2] - (yi[n-2]-yi[n-3])/h [n-3])
5.5 Cubic Tracers
7
# Solve system of equations r = np.linalg.solve(A,B)
0
# Yes
for j in range (1,n-1,1):
S[j] = r[j-1]
S[0] = 0
S[n-1] = 0

# Coefficients
a = np.zeros(n-1, dtype = float )
b = np.zeros(n-1, dtype = float ) c = np.zeros(n-1, dtype = float ) d = np.zeros(n-1, dtype = float ) for j in range
(0,n -1,1): a[j] = (S[j+1]-S[j])/(6*h[j]) b[j] = S[j]/2
c[j] = (yi[j+1]-yi[j])/h[j] - (2*h[j]*S[j]+h[j]*S[j+1])/ 6 d[j] = yi[j]

# tracer polynomial
x = sym.Symbol( 'x' ) polynomial = [] for j in range (0,n-1,1): prange = a[j]*(x-xi[j])**3 + b[j ]*(x-xi[j])**2 + c[j]*(x-
xi[j])+ d[j] psection = psection.expand() polynomial.append(psection)

return (polynomial)

# TEST PROGRAM
# INCOME, Test data
xi = np.array([0.1 , 0.2, 0.3, 0.4])
fi = np.array([1.45, 1.8, 1.7, 2.0])
resolution = 10 # between each pair of points

# PROCEDURE
5.6 Exercises
7
n = len (xi)
1
# Obtains the piecewise polynomials polynomial = trace3natural(xi,fi)

# EXIT
print ( 'Sectional polynomials: ' ) for section in range (1,n,1):
print ( ' x = [' + str (xi[leg-1])+ ',' + str (xi[leg-1])+ ']' ) print ( str (polynomial[leg-1]))

# GRAPH
# Points for graph in each section xplot = np.array([]) yplot = np.array([]) section = 1
while not (leg>=n):
a = xi[span-1] b = xi[span] xspan = np.linspace(a,b,resolution)

pleg = polynomial[leg-1]
pxstretch = sym.lambdify( 'x' ,pstretch) ystretch = pxstretch(xstretch)

xpath = np.concatenate((xpath,xpath))
yplot = np.concatenate((yplot,yplot))
span = span + 1

# Graph
import matplotlib.pyplot as plt
plt.title( 'Natural cubic plot (splines)' ) plt.plot(xplot,yplot) plt.plot(xi,fi, 'o' )
plt.show()

5.6 Exercises

(Exercise n 18.2 / page. 537)

Fit a second-order Newton interpolation polynomial to estimate log 10, using the data from Problem
18.1 at x = 8.9 and 11. Calculate the true percent relative error.

Exercise 18.1
5.6 Exercises
7
Estimate Estimate the natural logarithm of 10 using linear interpolation.
2
a) Interpolate between log 8 = 0.9030900 and log 12 = 1.0791812.
b) Interpolate between log9 = 0.9542425 and log 11 = 1.0413927.
For each of the interpolations, calculate the relative percentage error based on the value see givenro.

Resolution a)

Figure 5.5: Interpolation using the Vandermonde matrix

8. 1.
Vandermonde Matrix: 12. 1.
The coefficients of the polynomial: 0,0440228
Interpolation polynomial: [ 0,5509076
0.0440228 x + 0.5509076
Finding ln10:
0,0440228 ∗ 10 + 0,5509076 = 0,9911356

,
1 - 0.9911356 The ² = = 0,8864 %

t1
Resolution b)

Yo xi f ( xi ) 1DD 2DD
1. 9. 0.9542425 0.0871502 0.

2. 11. 1.0413927 0. 0.
Table 5.3: Finite differences

polynomial:

0.0435751 x + 0.5620666
Gives ln10 = 0.9978176 with a ² t = 0.21824 %
5.6 Exercises
7
3

Figure 5.6: Interpolating by finite differences

Exercise 5.2 I ( Exercise 18.3 / page 537)

Fit a third-order Newton interpolation polynomial to estimate log 10 with the data from Problem 18.1.
Exercise 18.1
Estimate Estimate the natural logarithm of 10 using linear interpolation.
a) Interpolate between log 8 = 0.9030900 and log 12 = 1.0791812.
b) Interpolate between log 9 = 0.9542425 and log 11 = 1.0413927.
For each of the interpolations, calculate the relative percentage error based on the value see givenro.

Solution

Figure 5.7: Graph made by finite differences

polynomial:
5.6 Exercises
7
4
Yo x i f [ xi ] 1DD 2DD 3DD
0 8 0.90309 0 0 0

1 9 0.9542425 0.051152500000000045 0 0
2 11 1.0413927 0.04357510000000003 -0.002525800000000004 0

3 12 1.0791812 0.0377885 -0.001928866666666677 0.00014923333333333172


Table 5.4: Interpolation – Divided Differences

f n ( x ) = 0.90309 + ( x - 8)0.0511525 + ( x - 8)( x - 9) - 0.0025258 + ( x - 8)( x - 9)( x - 11)0 .000149

expanded polynomial:

41.6666666666667 x 3 - 27.5 x 2 + 6.83333333333335 x + 0.999999999999999

Exercise 5.3 (Exercise no. 18.4 / page 537)

Given the data:


x 1,6 2 2,5 3,2 4 4,5

f(x) 2 8 14 15 8 2
a) Calculate f (2,8) using Newton interpolation polynomials of orders 1 to 3. Choose the se most appropriate sequence of
points to achieve the greatest possible accuracy for your estimates.

Solution

Yo xi f [ xi ] 1DD 2DD
1 2.0 0.0 0.0 0.0

2 8.0 15.000000000000004 0.0 0.0


3 14.0 12.0 -3.3333333333333375 0.0

4 15.0 1.4285714285714282 -8.809523809523808 -3.4226190476190443


Table 5.5: Interpolation – Divided Differences

Newton's polynomial is
575 x 3 1965 x 2 + 2479 37
x+
168 112 168
enter the value of x to interpolate, x = 2.8 the interpolated value is 15.81
5.6 Exercises
7
5
(1965 x 2 )/112 - (2479 x)/168 - (575 x 3 )/168 - 37/7

1.6 1.8 2 2.2 2.4 2.6 2.8 3 3.2


x

Figure 5.8: Graph of the interpolated polynomial

Exercise 5.4 I (Exercise n 18.5 / page. 537)

Given the data: x 123 5 7 8

f ( x )3 6 19 99 291 444

Compute f (4) using Newton interpolation polynomials of orders 1 to 4. Choose base points to obtain good
accuracy. What do the results indicate regarding the order of the polynomial used to generate the data in the table?

Solution
The divided difference matrix is:

Yo xi f [ xi ] 1DD 2DD 3DD


1 3 0 0 0 0

2 6 3 0 0 0
3 19 13 5 0 0
4 99 40 9 1 0

5 291 96 14 1 0
Table 5.6: Interpolation – Divided Differences

Newton's polynomial is x 3 - x 2 - x + 4
enter the value of x to interpolate, x = 4 the interpolated value is 48.00
5.6 Exercises
7
6
x3-x2-x+4

450

400

350

300

250

200

150

100

50

2345678
x

Figure 5.9: Graph of the interpolated


polynomial

Exercise 5.5 (Exercise no. 18.6 / page 537)

Repeat problems 18.1 to 18.3, using the Lagrange polynomial.

Solution Problem 18.1 par tea )


Lagrange polynomial:
0.0440228 x + 0.5509076

Figure 5.10: Interpolation using the Lagrange polynomial

Solution Problem 18.1 par teb )


Lagrange polynomial:
0.0435751 x + 0.5620666
5.6 Exercises
7
7

Figure 5.11: Interpolation using the Lagrange polynomial

Solution Problem 18.2


Lagrange polynomial, expressions

- 0.30103 ∗ ( - x + 9) ∗ ( x - 11) + 0.52069635 ∗ ( x /3 - 8/3) ∗ ( x - 9) - 0.47712125 ∗ ( x - 11) ∗ ( x - 8)

Lagrange polynomial:

- 0.00252580000000002 ∗ x ∗ ∗ 2 + 0.0940911 ∗ x + 0.3120124

Ellog10 = 1.000343399999998 with an error ² t = 0.03434 %

Figure 5.12: Interpolation using the Lagrange polynomial

Solution Problem 18.3


Lagrange polynomial, expressions
0.0752575( - x + 9)( x - 12)( x - 11) + 0.359727066666667( x /4 - 2)( x - 11)( x - 9) - 0.52069635( x /3 - 8 /3)( x - 12)( x -
9) + 0.159040416666667( x - 12)( x - 11)( x - 8)
Lagrange polynomial:

0.000149233333333332 x 3 - 0.00670433333333342 x 2 + 0.132742533333335 x + 0.193819600000012


5.6 Exercises
7
8

Figure 5.13: Interpolation using the Lagrange polynomial

Exercise 5.6 ( Exercise nº 18.7 / page 537)

Redo Problem 18.5 using Lagrangian polynomials of orders 1 to 3.

Solution
Lagrange polynomial, expressions
( - x + 2) ∗ ( x - 8) ∗ ( x - 7) ∗ ( x - 5) ∗ ( x - 3)/112 + 74 ∗ ( x /7 - 1/7) ∗ ( x - 7) ∗ ( x - 5) ∗ ( x - 3) ∗ ( x - 2)/15 - 291
∗ ( x /6 - 1/6) ∗ ( x - 8) ∗ ( x - 5) ∗ ( x - 3) ∗ ( x - 2)/40 + 11 ∗ ( x /4 - 1/4) ∗ ( x - 8) ∗ ( x - 7) ∗ ( x - 3) ∗ ( x - 2)/4 -
19 ∗ ( x /2 - 1/2) ∗ ( x - 8) ∗ ( x - 7) ∗ ( x - 5) ∗ ( x - 2)/40 + ( x - 8) ∗ ( x - 7) ∗ ( x - 5) ∗ ( x - 3) ∗ ( x - 1)/15
Lagrange polynomial:
x3-x2-x+4

Obtaining as a result f (4) = 48

Figure 5.14: Interpolation using the Lagrange polynomial


Exercise 5.7 ( Exercise nº 18.8 / page 537)

Employ inverse interpolation, using a cubic interpolation and bisection polynomial, to determine the value of x
that corresponds to f ( x ) = 0.23, for the following tabulated data:

Solution
5.6 Exercises
7
9
Piecewise polynomials:
x = [2, 3]
0.0154459330143541 ∗ x ∗∗ 3 - 0.0926755980861244 ∗ x ∗∗ 2 + 0.00320526315789479 ∗ x +
0.740724401913876 x = [3.4]
0.00617033492822966 ∗ x ∗∗ 3 - 0.00919521531100471 ∗ x ∗∗ 2 - 0.247235885167464 ∗ x +
0.991165550239235 x = [4.5]
- 0.0902272727272727 ∗ x ∗ ∗ 3 + 1.14757607655502 ∗ x ∗ ∗ 2 - 4.87432105263158 ∗ x + 7.16061244019139 x =
[5, 6]
0.338138755980861 ∗ x ∗ ∗ 3 - 5.27791435406699 ∗ x ∗ ∗ 2 + 27.2531311004785 ∗ x - 46.3851411483254 x =
[6.7]
- 0.269527751196172 ∗ x ∗ ∗ 3 + 5.66008277511962 ∗ x ∗ ∗ 2 - 38.3748516746411 ∗ x + 84.8708244019139

Figure 5.15: Cubic Spline Interpolation

Exercise 5.8 ( Exercise No. 18.20 / page 538)

Develop, debug, and test a program in any high-level or macro language of your choice to implement segmental
cubic interpolation based on Figure 18.18. Prue be the program with the repetition of example 18.10. and predict
f (3.4) and f (2.2).
Data

x 1,6 2 2,5 3,2 4 4,5

f(x) 2 8 14 15 8 2

Solution

Piecewise polynomials:
x = [1.6, 2.0]
- 1.69815123233442 ∗ x ∗ ∗ 3 + 8.15112591520521 ∗ x ∗ ∗ 2 + 2.22990273284518 ∗ x - 15.4790992678358
x = [2.0, 2.5]
- 5.75080346500936 ∗ x ∗ ∗ 3 + 32.4670393112548 ∗ x ∗ ∗ 2 - 46.4019240592541 ∗ x + 16.9421185935637
x = [2.5, 3.2]
1.60745831407652 ∗ x ∗ ∗ 3 - 22.7199240318892 ∗ x ∗ ∗ 2 + 91.5654842986061 ∗ x - 98.0307217046531
x = [3.2, 4.0]
5.6 Exercises
8
0
2.40870229759067 ∗ x ∗ ∗ 3 - 30.4118662736251 ∗ x ∗ ∗ 2 + 116.179699472161 ∗ x - 124.285884556445
x = [4,0,4,5]
1.00495913502469 ∗ x ∗ ∗ 3 - 13.5669483228333 ∗ x ∗ ∗ 2 + 48.8000276689936 ∗ x - 34.4463221522219

Figure 5.16: Cubic Spline Interpolation

Obtaining as a result f (3,4) = 13.83555463029991 and f (2,2) = 16.9421185935637

Exercise 5.9 ( Exercise No. 18.21 / page 538)

Use the software developed in Problem 18.20 to fit cubic tracers for the data in Problems 18.4 and 18.5. For both
cases, predict f (2.25).

Solution for exercise 18.4

N We saw the case of problem 18.4 in the previous problem, for that reason we will only do f (2.25)

In the section x = [2.0, 2.5]


- 5.75080346500936 ∗ x ∗ ∗ 3 + 32.4670393112548 ∗ x ∗ ∗ 2 - 46.4019240592541 ∗ x + 16.9421185935637
The result of f (2.25) = 16.9421185935637
Solution for exercise 18.5

Piecewise polynomials :
x = [1,2]
1.80786026200873 ∗ x ∗ ∗ 3 - 5.4235807860262 ∗ x ∗ ∗ 2 + 6.61572052401747 ∗ x
x = [2, 3]
0.960698689956332 ∗ x ∗ ∗ 3 - 0.340611353711791 ∗ x ∗ ∗ 2 - 3.55021834061136 ∗ x + 6.77729257641922
x = [3, 5]
0.760917030567686 ∗ x ∗ ∗ 3 + 1.45742358078603 ∗ x ∗ ∗ 2 - 8.9443231441048 ∗ x + 12.1713973799127
x = [5,7]
1.88973799126638 ∗ x ∗ ∗ 3 - 15.4748908296943 ∗ x ∗ ∗ 2 + 75.7172489082969 ∗ x - 128.931222707424
x = [7,8]
- 8.06986899563319 ∗ x ∗ ∗ 3 + 193.676855895196 ∗ x ∗ ∗ 2 - 1388.34497816594 ∗ x + 3287.21397379913
5.6 Exercises
8
1

Figure 5.17: Cubic Spline Interpolation

The result of

f (2.25) = 8.007914847161564
7
7

Numerical integration

6.1 Newton-Cotes integration formulas


6.1.1 trapezoid rule
It is the first of the closed Newton-Cotes formulas, using a first degree polynomial.

Figure 6.1: Trapezoid Rule

Zbf (
x) Z b
f1(x)
I= to dx ∼ to dx
=
Using the range between [ a , b ] the polynomial is approximated with a straight line:

f ( b ) -f ( a )
f1(x)=f(a)+ ( for )
b-a
The area under this straight line is an approximation of the integral of f ( x ) between the limits a and b .
The result of the integral is the trapezoid rule:

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

which is interpreted as the multiplication between the base and average height of a trapezoid.
6.1 Newton-Cotes integration formulas
7
6.1.2 Simpson's Rules
8
simpsons 1/3
It is the result when an interpolation is performed with a second degree polynomial.

Figure 6.2: Simpson's Rule 1/3

Zbf (
x) Z b
f2(x)
I= to dx ∼ to dx
=

Using a second degree Lagrange polynomial:


x 2
( x - x : 1)( x - x 2 ) ( x - x 0 )( x - x 2 ) ( x - x 0 )( x - x 1 )
I=
x0 ( x 0 - x 1 )( x 0 - x 2 )
f(x0)+
( x 1 - x 0 )( x 1 - x 2 )
f(x1)+ ( )
( x 2 - x 0 )( x 2 - x 1 ) f x 2

which results for a single section:

h
I∼=3£f(x0)+4f(x1)+f(x
2 )¤
Being

b - ah
=
2

simpsons 3/8
It is the result when an interpolation is performed with a third degree polynomial.
Zbf (
x) Z b
f3(x)
I= to dx ∼ to dx
=
Using a third degree Lagrange polynomial:

3 hours
I∼= f ( x0 ) + 3f ( x1 ) + 3f (x2 ) + f ( x3 )
8
Being
6.1 Newton-Cotes integration formulas
7
b-a
9
h=
3

Figure 6.3: Simpson's Rule

Evaluate the following


integral: Z 0 (1 - e - 2 x dx )

a ) in analytical form; b ) with a single application of the trapezoid rule; c ) with multiple application of the
trapezoid rule, with n = 2 and 4; d ) with a single application of Simpson's 1/3 rule; e) with the multiple
application of Simpson's 1/3 rule, with n = 4; f ) with a single application of Simpson's 3/8 rule, and g ) with
multiple application of Simpson's rule, with n = 5. For parts b ) to g ), determine the relative percentage error of
each of the numerical estimates, based on the result of part a).

Solution in analytical form


123 7
Z 4
(1 - e - 2 x ) dx = [ x + e - 2 x ] = (4 + e - 2(4)
) - (0 + e - 2(0) ) = 4 + e - 8 - = + e - 8 = 3.500168
0 20 2 2 22 22
Solution with a single application of the trapezoid rule

(0) +
(4)
1(4 - 0) ∗ f 2
f
, f (0) = 0, f (4)
20
3= (4) ∗
6.1 Newton-Cotes integration formulas
8
0
Z4
(1 - e - 2 x dx ) =
I = 20∗ (1 - e - 8 )
I = 1.999329

Solution with multiple application of the trapezoid rule n = 2


n-1
f ( x0 ) +2 f ( xi ) + f (xn )
I=(b-a) i = 1
,n=2
2n

f ( x0 ) + 2f ( x1 ) + f ( x2 )
I=(b-a)
4

a = x 0 = 0, b = x 2 = 4, x 1 = 2, f ( x 0 ) = 0, f ( x 1 ) = 0.981684, f ( x 2 ) = 0.999665

0 + 2(0,981684) + (0,999665)
I = (4 0) 4

I = 2.963033

Solution with multiple application of the trapezoid rule n = 4


n-1
f ( x0 ) + 2f ( xi ) + f ( xn )
I= (b-a) i = 1
,n=4
2n

f ( x 0 ) + 2( f ( x 1 ) + f ( x 2 ) + f ( x 3 )) + f ( x 4 )
I= (b-a)
8

a=x0 = 0, b = x 4 = 4, x 1 = 1, x 2 = 2, x 3 = 3

f ( x0 ) = 0, f ( x1 ) = 0.864665 f (x2 ) = 0.981684 , f ( x3 ) = 0.997521, f ( x4 ) = 0.999665

0+ 2(0,864665 + 0,981684 + 0,997521) + 0,999665


(4-0) + (, + , + , ) + ,
8
-8

-8
3,343703
6.1 Newton-Cotes integration formulas
8
1
Solution with a single application of Simpson's rule 1/3
f ( x0 ) + 4f ( x1 ) + f ( x2 ) )
I=(b-a)
6

a = x 0 = 0, b = x 2 = 4, x 1 = 2

f ( x0 ) = 0, f ( x1 ) = 0.981684 , f ( x2 ) = 0.999665

0 + 4 ∗ 0,981684 + 0,999665
, ,
I = (4 - 0)
6

I = 3.284268

Solution with the multiple application of Simpson's rule 1/3 , with n = 4


f ( x 0 ) + 4( f ( x 1 ) + f ( x 3 )) + 2( f ( x 2 )) + f ( x 4 ) ( b - a )
12

x 0 = a = 0, x1 = 1, x2 = 2, x 3 = 3, x4=b=4

f ( x0 ) = 0, f ( x1 ) = 0.864665 f (x2 ) = 0.981684 , f ( x3 ) = 0.997521, f ( x4 ) = 0.999665

0 + 4(0,864665 + 0,997521) + 2(0,981684) + 0,999665


(4 0) 12

3,470592

Solution with a single application of Simpson's 3/8 rule


f ( x0 ) + 3f ( x1 ) + 3f (x2 ) + f ( x3 )
(b-a)

x 0 = a = 0, x 1 = 1.333333, x 2 = 2.666666, x 3 = b = 4

f ( x0 ) = 0, f ( x1 ) = 0.930516 f ( x2 ) = 0.995172 , f ( x3 ) = 0.999665


6.1 Newton-Cotes integration formulas
8
0 + 3 ∗ 0,930516 + 3 ∗ 0,995172 + 0,999665
2
, , ,
I = (4 - 0)

I = 3.388365

For the following exercises we will use Python codes to solve the problems and we will present it in a box.

Python Code - Simple Trapezoid Rule

# Newton Cotes Integration Formulas


# Trapezoid rule a single Segmentto import numpy as np
import matplotlib.pyplot as plt

print ( "Trapezoid rule: Single segment" )

# g = input("Enter the function: ")


a = float ( input ( "Enter the value a: " )) b = float ( input ( "Enter the value b: " )) h=ba #Enter the
function here def f(x):
y = 0.2 + 25*x -200*x**2 + 675*x**3 - 900*x**4 + 400*x**5 return y

Trap = h * (f(a) + f(b))/2

print (f "I= {Trap:06f}" )


print (f "f(a)= {f(a):06f}" )
print (f "f(b)= {f(b):06f}" )

Python Code - Multiple Trapezoid Rule

# Newton Cotes Integration Formulas


# Trapezoid rule for multiple applications

import numpy as np

import matplotlib.pyplot as plt

print ( "Trapezoid rule for multiple application" ) a = float ( input ( "Enter the lower limit a: " )) b =
float ( input ( "Enter the upper limit b: " )) green_value = float ( input ( " The true value: " )) n = int
( input ( "Enter the number of trapezoids n: " ))

#Enter the function here def function(x):


6.1 Newton-Cotes integration formulas
8
y = 1 - np.exp(-2 * x)
3
return and

print ( "{:^10}{:^10}{:^12}{:^12}" . format ( "n" , "h" , "I" , "Ea(%)" )) while n> 0:


x = np.zeros([n + 1])
h = (b - a) / n
x[0] = a
x[1] = b sum = 0 for i in np.arange(1,n): x[i] = x[i - 1] + h sum += function(x[i]) trapm = (h/2 ) *

(function(x[0]) + 2 * sum + function(x[n])) error = abs ((verd_value - trapm) / verd_value) * 100 print (f
"{n:^10}{h: ^10.6}{trapm:.6f} {error:.6f}" ) n = int ( input ( "Enter the number of trapezoids n: " ))
else :
print ( "Execution ended" )

Python code - Simpson's rule 1/3

import numpy as np
import matplotlib.pyplot as plt

#Enter the function here def f(x):


y = 0.2 + 25*x - 200*x**2 + 675*x**3 - 900*x**4 + 400*x**5 return y
# Graphing the function.
# x = np.linspace(0, np.pi, 101)
#plt.plot(x, f(x))
#plt.grid()
#plt.show()

a = float ( input ( "Enter the lower limit: " )) b = float ( input ( "Enter the upper limit: " )) n = int ( input (
"Enter the number of segments: " )) vt = float ( input ( "Enter the true value: " )) h = (b - a) / n sum = 0

def simpson13(f, a, b):


m = (a + b)/2
integral = (b - a) / 6 * (f(a) + 4 * f(m) + f(b)) return integral
print ( "{:^10}{:^10}{:^12}{:^12}" . format ( "n" , "h" , "I" , "Et(%)" ))

for i in range (n):


b=a+h
area = simpson13(f, a, b) sum += area a=b
#vt = np.exp(np.pi) / 2 + 1 / 2
errorPercent = abs ((vt - sum) / vt) * 100
print (f "{n:^10} {h:.6f} {sum:.6f} {errorPercent:.6f}" )

Python code - Simpson's rule 3/8

import numpy as np
import matplotlib.pyplot as plt

a = float ( input ( "Enter the lower limit: " )) b = float ( input ( "Enter the upper limit: " )) n = int ( input (
"Enter the number of segments: " )) vt = float ( input ( "Enter the true value: " )) h = (b - a) / n #Enter
6.1 Newton-Cotes integration formulas
8
the function here def f(x):
4
y = 0.2 + 25*x - 200*x**2 + 675*x**3 - 900*x**4 + 400*x**5 return y

def simpson38(f, a, b):


m1 = (2 * a + b) / 3
m2 = (a + 2 * b) / 3
integral = (b - a) / 8 * (f(a) + 3 * f(m1) + 3 * f(m2) + f(b)) return integral
sum = 0
print ( "{:^10}{:^10}{:^12}{:^12}" . format ( "n" , "h" , "I" , "Et(%)" ))
6.1 Newton-Cotes integration formulas
8
for i in range (n):
5
b=a+h
area = simpson38(f, a, b) sum += area
a=b
#vt = np.exp(np.pi) / 2 + 1 / 2
errorPercent = abs ((vt - sum) / vt) * 100
print (f "{n:^10} {h:.6f} {sum:.6f} {errorPercent:.6f}" )

Exercise 6.2 ( Exercise nº21.2 / Page. 646)

Evaluate the following integral: π

2
0 (6 + 3cos x ) dx

a) in analytical form; b) with a single application of the trapezoid rule; c) with multiple application of the
trapezoid rule, with n = 2 and 4; d) with a single application of Simpson's 1/3 rule; e) with multiple application
of Simpson's 1/3 rule, with n = 4; f) with a single application of Simpson's 3/8 rule; and g) with multiple
application of Simpson's rule, with n = 5. For each of the numerical estimates in parts b) to g), determine the
relative percentage error based on part a).

Solution
to)

Y (6 + 3cos x ) dx (6.1)
o 0
(6 x + sin x ) | 0 π /2
(6.2)
Y (6 ∗ π 2 + sin π 2 ) - (6 ∗ 0 + sin 0)
o 7,42373408883 (6.3)

Y (6.4)
b)
o

I = 11.780972
f ( a ) = 9.000000
f ( b ) = 6.000000

c)

n h Yo Ea(%)

2 0.785398 13.447054 81.135981

4 0.392699 12.975174 74.779617


Table 6.1: Trapezoid rule results, with n = 2 and 4
6.1 Newton-Cotes integration formulas
8
6
n h Yo Ea(%)

2 0.785398 12.425182 67.371050


Table 6.2: Results from a single application of Simpson's rule 1/3

n h Yo Ea(%)
4 0.392699 12.424803 67.365947

d), e), f) and


g)

Table 6.3: Results from a single application of Simpson's rule 1/3

n h Yo Ea(%)

4 0.392699 12.424789 67.365761


5 0.314159 12.424782 67.365672

Table 6.4: Results from a single application of Simpson's 3/8 rule, n= 4 and 5

Exercise 6.3 ( Exercise No. 21.3/Page.


646)
Evaluate the following integral:
Z
(1 - x - 4 x + 2 x ) dx

a) in analytical form; b) with a single application of the trapezoid rule; c) with the compound trapezoid rule,
with n = 2 and 4; d) with a single application of Simpson's 1/3 rule; e) with Simpson's 3/8 rule; and f) with
Boole's rule. For each of the numerical estimates in parts b) to f), determine the relative percentage error based
on part a).

Solution

Z
-2
Y (1 - x - 4 x + 2 x ) dx (6.5)
o
x6 x2
+x (6.6)
Y -2
o 4 (3-4
6
42 (-2)6
2 + 4) - ( ( 2)
-
(6.7)
- (-2) -
4 2
+ (-2))
Y 11,04
2
(6.8)
6.1 Newton-Cotes integration formulas
8
f(a)= -29.000000 f(b)= 1789.000000 I= 5280.000000
7
Table 6.5: Results with a single application of the trapezoid rule
n h Yo Ea(%)

2 3.0 -48.000000 534.782609

4 1.5 175.875000 1493.070652

Table 6.6: Results with the compound trapezoid rule, with n = 2 and 4

n h Yo Ea(%)
3 2.000000 1112.000000 9972.463768
Table 6.7: Results with a single application of Simpson's 1/3 rule

n h Yo Ea(%)
3 2.000000 1107.555556 9932.206119
Table 6.8: Results with Simpson's 3/8 rule

Exercise 6.4 ( Exercise nº21.4/Page. 646)

Integrate the following function analytically and using the trapezoid rule, with n = 1,2,3 and 4:
1 ( x + 2/ x ) 2 dx

Use the analytical solution to calculate the true percent relative errors to evaluate the accuracy of the trapezoid
rule approximations.

Solution

Z
1 ( x + 2/ x ) 2 dx h 3 ∗ x 3 4 i 2 1 ³ 3 ∗ 2 3 4 ´ - ³ 3 ∗ 1 3 4 ´
Y (6.9)
o
5,25
(6.10)
Y
o (6.11)
(6.12)
Y N We observe that with n = 1 we cannot apply the trapezoid rule, for that reason we start
from n = 2
6.1 Newton-Cotes integration formulas
8
8
n h Yo Ea(%)
2 0.5 3.093750 41.071429

3 0.333333 3.791667 27.777778


4 0.25 4.148438 20.982143

5 0.2 4.365000 16.857143


Table 6.9: Trapezoid rule results, with n = 1, 2, 3 and 4:

Exercise 6.5 ( Exercise No. 21.5/Page. 646)

Integrate the following function both analytically and with Simpson's rule, with n = 4 and 5.

Analyze the results. Z


-3
(4 x - 3) 3 dx

Solution

Z
3 (4 x - 3) 3 dx h 3
Y (6.13)
o 3 3
∗x 4
i 2
1
³
3∗2 4
(6.14)
Y 3
´
- 3∗1
³
4
´
o (6.15)
2056 (6.16)
Y

n h Yo Ea(%)
4 2.000000 2056.000000 0.000000

5 1.600000 2056.000000 0.000000

Table 6.10: Simpson's rule results, with n = 4 and 5

(Exercise nº21.6/Page.
646)
Integrate the following function both analytically and numerically. Use the rules of the trap cio and Simpson
1/3 to numerically integrate the function. For both cases, use the multiple application version, with n = 4.
Calculate the percent relative errors for the numerical results.
Z
x 2 e x dx
0
6.1 Newton-Cotes integration formulas
8
9
Solution

I= Z
x 2 e x dx (6.17)
0

I= £
( x 2 - 2 x + 2) e x ¤ 3 0 (6.18)
I= £
(3 2 - 2(3) + 2) e 3 ¤ - £ (0 2 - 2(0) + 2) e 0 ¤ (6.19)

I= 98,427685 (6.20)

n h Yo Ea(%)
4 0.75 44.479707 54.809760

5 0.6 53.118771 46.032693

6 0.5 59.455537 39.594701


Table 6.11: Trapezoid rule results, with n = 4, 5 and 6

n h Yo Ea(%)

4 0.750000 98.497751 0.071185

5 0.600000 98.456688 0.029466

6 0.500000 98.441752 0.014292


Table 6.12: Simpson 1/3 results, with n = 4, 5 and 6

N We observe that we did for the same values of “n” in the two methods: Multi-trapezoids
ples and Simpson 1/3, but the most efficient method is Simpson 1/3 with a lower error.

Exercise 6.7 ' (Exercise No. 21.7/Page. 646)

Integrate the following function both analytically and numerically. For numerical evaluations cas use a) a single
application of the trapezoid rule, b) Simpson's 1/3 rule, c) Sim's rule pson 3/8, d) Boole's rule, e) the midpoint
method, f) the 3-segment, 2-point open integration formula, and g) the 4-segment, 3-point open integration
formula. Calculate the relative percentage errors of the numerical results.
Z 1.5
14 2 x dx
0,5
6.1 Newton-Cotes integration formulas
9
0
1,5
Solution
14 2x

2log14 0.5
142(1,5) 1,5
142(0,5)
Y 14 2 x dx
2log14 - 2log14 0,5
o

Y
o
(6.21)

Y
o (6.22)

Y
o (6.23)

517,230143 (6.24)

Solution for a single application of the trapezoid rule

f ( a ) = 14.000000 (6.25)
f ( b ) = 2744.000000 (6.26)
I = 1379.000000 (6.27)

n h Yo Ea(%)
3 0.333333 187.070295 63.832291

4 0.25 247.312013 52.185305

5 0.2 290.091968 43.914334


Table 6.13: Trapezium rule results

n h Yo Ea(%)
3 0.333333 518.804319 0.304347

4 0.250000 517.747624 0.100049


6.1 Newton-Cotes integration formulas
9
5 0.200000 517.445968 0.041727 1
Table 6.14: Simpson's rule results 1/3
6.2 Integration of equations
92
6.2 Integration of equations
6.2.1 Romberg integration

Exercise 6.8 ( Exercise No. 22.1/Page. 666)

Use Romberg integration to evaluate

3
Z
1

2x+x ¶2
dx

with an accuracy of ² s = 0.5%. You should present your results in the form of Figure 22.3. uti Use the analytical
solution of the integral to determine the relative percentage error of the result
obtained with the Romberg integration. Verify that ²t is less than the stopping criterion ² s

.
Solution in analytical form

I= 3 (6.28)
Z
1

2x+x ¶2
dx
I= 4 9 (6.29)
·
x + 12 x -
3 ¸2

I= 4 9 4 9 (6.30)
·
(2)3+12(2)- ¸
- ·
3(1) +12(1)-
3
1
¸

I= (6.31)
32
Python Code - Romberg Integration

# Romberg Integration print ( "Romberg Integration" ) def print_row(lst):


print ( ' ' .join( ' %11.8f' % x for x in lst))

def romberg(f, a, b, eps = 1E-8):


"""Approximate the definite integral of f from a to b by Romberg's method.
eps is the desired accuracy."""
R = [[0.5 * (b - a) * (f(a) + f(b))]] # R[0][0] print_row(R[0]) n=1
while True:
h = float (ba)/2 ** n
R.append((n+1)*[None]) # Add an empty row.
R[n][0] = 0.5*R[n-1][0] + h* sum (f(a+(2*k-1)*h)
for k in range (1, 2 ** (n-1)+1)) # for proper limits for m in range (1, n+1):
R[n][m] = R[n][m-1] + (R[n][m-1] - R[n-1][m-1]) / (4 ** m - 1)
6.2 Integration of equations
93
print_row(R[n])
if abs (R[n][n-1] - R[n][n]) < eps: return R[n][n] n += 1

from math import *

# In this example, the error function erf(1) is evaluated. #print(romberg(lambda t: 2/sqrt(pi)*exp(-


t*t), 0, 1)) print (romberg( lambda x: (2*x + 3/x)**2, 1, 2))

Romberg integration

N The mistakes
25,833333333 - 25,83335064
²t =
25,833333333 ∗ 100%

² t = - 6.69936774194 E 5

(Exercise No. 22.2 / Page.


666)
Use Romberg integration of order h 8 to evaluate xe x dx

Compare ² a and ² t .

Solution in analytical form

Z 0 xe x dx = 41.1710738464

j hj T 1, j T 2, j T 3, j T 4, j
0 1 27.62500000

1 0.5 26.31250000 25.87500000


2 0.25 25.95594388 25.83709184 25.83456463

3 0.125 25.86418765 25.83360224 25.83336961 25.83335064


Table 6.15: Romberg integration results
6.2 Integration of equations
94

j hj T 1, j T 2, j T 3, j T 4, j
0 3 90.38491615

1 1.5 55.27625849 43.57337260


2 0.75 44.83949598 41.36057514 41.21305531

3 0.375 42.09765130 41.18370307 41.17191160 41.17125852


Table 6.16: Romberg integration results

The 41,1710738464 - 41,17125852


mistake is
²t =
41,1710738464
∗ 100%
² t = - 4.48551817446 E 4

Exercise 6.1 0 ( Exercise nº 22.3 / Page. 667)

Use Romberg integration to evaluate


Z2 e x sin x dx
1+x2

with an accuracy of ² s = 0.5%. You should present your results in the form of Figure 22.3.

Solution in analytical form


Z2 e x sin x
dx = 1.94013
01+x2

j hj T 1, j T 2, j T 3, j
0 2 1.34376994

1 1 1.81556261 1.97282684

2 0.5 1.91172038 1.94377297 1.94183605


Table 6.17: Romberg integration results
² t = - 8.79348291 E 4

The
mistake is
6.2 Integration of equations
95
6.1 1 (Exercise No. 22.11 / Page. 667)

Develop a user friendly computer program for Rom integration berg, based on figure 22.4. Try it by replicating the
results of Examples 22.3 and 22.4, and the function of Problem 22.10.
Problem 22.10
1
x 0 ,1(1,2 - x )(1 e 20( x - 1) )
0

Use the true value of 0.602298 to calculate ²t

Solution

j hj T 1, j T 2, j T 3, j T 4, j
1 1 0.00000000

2 0.5 0.32654672 0.43539563


3 0.25 0.47860222 0.52928739 0.53554684

4 0.125 0.54727033 0.57015970 0.57288453 0.57347719


Table 6.18: Romberg integration results

The mistake is
² t = 4.78515246332 E - 2

6.2.2 Gaussian quadrature


Gaussian quadrature approximates the integral of a function on an interval [ a , b ] centered on zero me compared to a
numerical calculation with fewer operations and evaluations of the function. It is represented as a weighted sum:

I∼=c0f(x0)+c1f(x1)

For the two-point formula we obtain:

c0=c1=1

eleven
x0=- ,x1=
33
For an evaluation interval shifted on the x- axis, it is required to convert the points to the new range. The zero point is
moved to the center of the interval [ a , b ] and we obtain:
6.2 Integration of equations
96

x = b+a+b-ax

b-a
xb= 2 ( f ( xa ) + f ( xb ) )

whose formula is similar to a better approximation of a trapezoid, whose average heights are internal points of [ a , b ],
a concept shown in the graph.
Python Code - Gaussian Quadrature

# Graph for each subsection x0 = -1/np.sqrt(3) ) fi = np.array([]) xat = np.array([]) xbt =


np.array([]) straight = np.array([]) for i in range (0, sections, 1): at = subsection[i] bt =
subsection[i+1]
xit = np.linspace(at,bt,samplesection) fit = functionx(xit)
xi = np.concatenate((xi,xit)) fi = np.concatenate((fi,fit)) # points xa and xb per
section xa = (bt+at)/2 + (bt-at)/2*( x0)

xb = (bt+at)/2 + (bt-at)/2*(x1) xat = np.concatenate((xat,[xa])) xbt =


np.concatenate((xbt,[xb])) # Line between points x0 and (straight, line))
# Markers 'o' of xa and xb by sections pointx = np.concatenate((xat,xbt)) pointy =
functionx(pointx)

# Line drawing
plt.plot(xi,straight, label = 'grade 1' , color = 'tab:orange' ) plt.fill_between(xi,0,straight,
color= 'tab:olive' ) plt.plot(xi,fi, label = 'f(x)' , color = 'b' ) # Verticals to divide the sections for
j in range (0, len (subsection),1): plt.axvline(subsection[j], color= 'tab:gray ' )
# Piecewise xa and xb point markers for j in range (0, len (xat),1): plt.axvline(xat[j], color=
'w' ) plt.axvline(xbt[j], color= ' w' ) plt.plot(xpoint,ypoint, 'o' , color= 'g' ) plt.title( 'Integral:
6.2 Integration of equations
97
Gaussian Quadrature' ) plt.xlabel( 'x' ) plt.legend() plt.show()

6.2.3 Gauss quadrature with legendre polynomials

(Exercise nº22.4 / Page. 667)

Obtain an estimate of the integral from Problem 22.1, but use the Gauss-Legendre formulas with two, three, and
four points. Calculate ²t for each case based on the analytical solution.
Problem 22.1
3
Z
1
2 µ
2x+x ¶
dx = 25.833333333

Solution
6.2 Integration of equations
98

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.57735027 -0.78867513 28.95729026 1.00000000 28.95729026

2 -0.57735027 -0.21132487 213.70937641 1.00000000 213.70937641


X
= 242.66666668
Table 6.19: Gauss integration with legendre polynomials for n = 2
The integral is: I = 121.33333334
The error is ² t = 369.677419418 %

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.00000000 -0.50000000 49.00000000 0.88888889 43.55555556

2 0.77459667 -0.88729833 26.58069218 0.55555556 14.76705121


3 -0.77459667 -0.11270167 720.61930756 0.55555556 400.34405979
X
= 458.66666656
Table 6.20: Gauss integration with legendre polynomials for n = 3
The integral is: I = 229.33333328
The error is ² t = 787.741935392 %

Figure 6.5: Piecewise Gauss Quadrature

N We observe that the Gauss - Legendre method does not work for this problem since the
error increases, when the value of " n " increases.
6.2 Integration of equations
99

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.86113631 -0.93056816 25.85695301 0.34785485 8.99446639

2 -0.86113631 -0.06943184 1878.93670180 0.34785485 653.59723536


3 0.33998104 -0.66999052 33.84512515 0.65214515 22.07193437
4 -0.33998104 -0.33000948 95.07550592 0.65214515 62.00303052
X
= 746.66666663
Table 6.21: Gauss integration with legendre polynomials for n = 4
The integral is: I = 373.33333332
The error is ² t = 1345.16129046%

Exercise 6.1 3 ( Exercise nº 22.5a / Page. 667)

Obtain an estimate of the integral from Problem 22.3, but use Gauss-Legendre formulas with two, three, and
four points. Calculate ²t for each case based on the analytical solution.
Problem 22.3
Z2 e x sin x dx = 1.94013 0 1 + x 2

Solution

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.57735027 1.57735027 1.38817589 1.00000000 1.38817589

2 -0.57735027 0.42264973 0.53106634 1.00000000 0.53106634


X
= 1.91924223
Table 6.22: Gauss integration with legendre polynomials for n = 2
The integral is: I = 1.91924223
The error is ² t = 1.07661703082%

N In this example we notice that the Gauss - Legendre method works, since the error is smaller
to 128% for n = 2, 3, 4.

Exercise 6.1 (Exercise No. 22.6 / Page.


4 667)

Obtain an estimate of the integral of Problem 22.2 using the Gauss formula.
Legendre with five points.
Problem 22.2
6.2 Integration of equations
10
0
Yo zi xi f ( xi ) wi wi∗f(xi)
1| 0.00000000 1.00000000 1.14367764 0.88888889 1.01660235

2 0.77459667 1.77459667 1.39203968 0.55555556 0.77335538


3 -0.77459667 0.22540333 0.26646807 0.55555556 0.14803782
X
= 1.93799555

Table 6.23: Gauss integration with legendre polynomials for n = 3


The integral is: I = 1.93799555
The error is ² t = 0.110015823682%

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.86113631 1.86113631 1.38040240 0.34785485 0.48017966

2 -0.86113631 0.13886369 0.15602886 0.34785485 0.05427540


3 0.33998104 1.33998104 1.32986133 0.65214515 0.86726262
4 -0.33998104 0.66001896 0.82633364 0.65214515 0.53888948
X
= 1.94060716

Table 6.24: Gauss integration with legendre polynomials for n = 4


The integral is: I = 1.94060716
The error is ² t = 0.024594228 %
Solution

Yo zi xi f ( xi ) wi wi∗f(xi)
Z3
1 0.00000000 xe dx = 41.1710738464
x
1.50000000 6.72253361 0.56888889 3.82437467
0
2 0.53846931 2.30770397 23.19547093 0.47862867 11.10201741
3 -0.53846931 0.69229603 1.38341408 0.47862867 0.66214164
4 0.90617985 2.85926977 49.89077099 0.23692689 11.82046497
5 -0.90617985 0.14073023 0.16199655 0.23692689 0.03838134
X
= 27.44738003
Table 6.25: Gauss integration with legendre polynomials for n = 5
The integral is: I = 41.17107005
The error is ² t = 9.22103711495 E - 6%
6.2 Integration of equations
10
1
Exercise 6.1 5 ( Exercise No. 22.8 / Page
667)
Use two- to six-point Gauss-Legendre formulas to solve

4
3
( ) dx
- 3 1 + x5

Solution

Figure 6.6: 1+ 1 x 2

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.57735027 4.73205081 0.04274910 1.00000000 0.04274910

2 -0.57735027 1.26794919 0.38348041 1.00000000 0.38348041


X
= 0.42622951

Table 6.26: Gauss integration with legendre polynomials for n = 2 The integral is: I = 2.55737704
The error is ² t = 9.22103711495 E - 6%

1
I= ( Z
) dx (6.32)
-31+x2
1 1 (6.33)
Yo = 0
( ) dx +6 ( ) dx
-3 1+x2 0 1+x2
(6.34)

(6.35)
1 1 (6.36)
4 = 0
( ) dx + 3
( ) dx
(6.37)
-31+x2 01+x 2

| {z} | {z}
AB
A=B
5I = 2A
6.2 Integration of equations
10
2
Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.00000000 1.50000000 0.30769231 0.88888889 0.27350427

2 0.77459667 2.66189500 0.12367536 0.55555556 0.06870853


3 -0.77459667 0.33810500 0.89741232 0.55555556 0.49856240
X
= 0.84077521

Table 6.27: Gauss integration with legendre polynomials for n = 3


The integral is: I = 2.52232562
The error is ² t = 0.009698 %

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.86113631 2.79170447 0.11371889 0.34785485 0.03955767

2 -0.86113631 0.20829553 0.95841713 0.34785485 0.33339004


3 0.33998104 2.00997157 0.19841326 0.65214515 0.12939425
4 -0.33998104 0.99002843 0.50501064 0.65214515 0.32934024
X
= 0.83168220

Table 6.28: Gauss integration with legendre polynomials for n = 4


The integral is: I = 2.4950466
The error is ² t = 0.001222 %

Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.23861919 1.85792878 0.22462323 0.46791393 0.10510434

2 -0.23861919 1.14207122 0.43396625 0.46791393 0.20305885


3 0.66120939 2.49181408 0.13871285 0.36076157 0.05004227
4 -0.66120939 0.50818592 0.79475277 0.36076157 0.28671626

5 0.93246951 2.89870427 0.10635485 0.17132449 0.01822119

6 -0.93246951 0.10129573 0.98984339 0.17132449 0.16958442


X
= 0.83272732

Table 6.29: Gauss integration with legendre polynomials for n = 6 The integral is: I = 2.49818198
The error is ² t = 3.28169 e - 05%

6I = 2.4981
6.2 Integration of equations
10
3
Exercise 6.1 6 ( Exercise nº 22.12 / Page. 667)

Develop a user-friendly computer program for Gaussian quadrature. Try it by duplicating the results of Examples
22.3 and 22.4, and the function of Problem 22.10.

Solution
Python code - Gauss Quadrature with Legendre Polynomials, using Gauss tables -
weight

import numpy as np
import matplotlib.pyplot as plt
m2 = np.array([(0.5773502692,1.0000000000), (-0.5773502692,1.0000000000)])

m3 = np.array([(0,0.8888888889), (0.7745966692, 0.5555555556), (-0.7745966692,0.5555555556) ])

m4 = np.array([(0.8611363116, 0.3478548451), (-0.8611363116,0.3478548451), (0.3399810436,


0.6521451547), (-0.3399810436, 0.6521451547 ) ])

m5 = np.array([(0, 0.5688888889), (0.5384693101, 0.4786286705), (-0.5384693101, 0.4786286705),


(0.9061798459, 0.2369268851), (-0.906179 8459, 0.2369268851), ])

m6 = np.array([(0.2386191861, 0.4679139346), (-0.2386191861, 0.4679139346), (0.6612093864,


0.3607615730), (-0.6612093864, 0.360761573 0), (0.9324695142, 0.1713244924), (-0.9324695142,
0.1713244924) ])

m7 = np.array([(0,0.4179591837), (0.4058451514, 0.3818300505), (-0.4058451514, 0.3818300505),


(0.7415311856, 0.2797053915),

(-0.7415311856, 0.2797053915), (0.9491079123, 0.1294849662) ])

m8 = np.array([(0.9602898565, 0.1012285363), (-0.9602898565, 0.1012285363), (0.7966664774,


0.2223810344), (-0.7966664774, 0.222381034 4), (0.5255324099, 0.3137066458), (-0.5255324099,
0.3137066458), (0.1834346425, 0.3626837834 ), (-0.1834346425, 0.3626837834)
])

m9 = np.array([(0,0.3302393550), (0.3242534234, 0.3123470770), (-0.3242534234, 0.3123470770),


(0.6133714327, 0.2606106964), (-0.6133714 327, 0.2606106964), (0.8360311073, 0.1806481606),
(-0.8360311073, 0.1806481606 ), (0.9681602395, 0.0812743884), (-0.9681602395,
0.0812743884) ])

m10 = np.array([(0.1488743390, 0.2955242247), (-0.1488743390, 0.2955242247), (0.4333953941,


0.2692667193), (-0.4333953941, 0.26926671 93), (0.6794095683, 0.2190863625), (-0.6794095683,
0.2190863625), (0.9650633667, 0.1494513492 ), (-0.9650633667, 0.1494513492), (0.9739065285,
0.0666713443), (-0.9739065285, 0.0666713443) ])

m12 = np.array([(0.1252334085, 0.2491470458), (-0.1252334085, 0.2491470458), (0.3678314990,


6.2 Integration of equations
10
4
0.2334925365), (-0.3678314990, 0.23349253 65), (0.5873179543, 0.2031674267), (-0.5873179543,
0.2031674267), (0.7699026742, 0.1600783285 ), (-0.7699026742, 0.1600783285),

(0.9041172564, 0.1069393260), (-0.9041172564, 0.1069393260), (0.9815606342, 0.041753363), (-


0.9815606342, 0.041753363) ])

"""Enter the values of a: Lower limit, b: Upper limit and n: number of points"""
a = -3 # Lower limit
b=0 # Upper limit
n=6

"""Change in y = 'function to evaluate' def f(x):


y = 1 / (1 + x ** 2 ) return y
x = np.linspace ( -4, 4, 101 ) plt.plot(x, f(x)) plt.grid() plt.show()

print ( "-------------------------------------------------------------------------------------------------------------------------------------")
print ( "{:^5}{:^14}{:^13}{:^14}{:^14}{:^14}" . format ( "i" , "Zi" , "Xi" , " f(Xi)" , "Wi" , "Wi*f(Xi)" ))
print ( "-------------------------------------------------------------------------------------------------------------------------------------")
sum = 0 if n == 2:
for i in range (1,n+1):
x_0 = 0.5 * (b - a) * m2[i-1][0] + 0.5 * (b - a) funcEval = f(x_0) w_0 = m2[i-1][1] sum += w_0 *
funcEval
print (f "{i:^5}| {m2[i-1][0]:.8f} | {x_0:.8f} | {funcEval:.8f} | {w_0:.8f} | {w_0* funcEval:.8f} |" )
I = 0.5 * (b - a) * sum
Nonlinear equations...............................................................................................................................5
1.1 Bisection Method....................................................................................................................5
1.2 Newton Raphson method........................................................................................................5
1.3 Secant Method............................................................................................................................15
1.4 Modified secant..........................................................................................................................18
Systems of nonlinear equations............................................................................................................28
2.1 Fixed Point Method....................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)..................................................................................28
2.2 Newton Raphson method...........................................................................................................33
2.3 Broyden method.........................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)................................................................................38
3.1 General Gauss Method...............................................................................................................35
3.2 Gauss Jordan method..................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................47
Jacobbi..............................................................................................................................................49
Interpolation.........................................................................................................................................57
6.2 Integration of equations
10
5
5.1 Finite differences...................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................59
5.3 Interpolation polynomial............................................................................................................63
5.4 Lagrangian interpolation............................................................................................................65
5.5 Cubic Tracers.............................................................................................................................67
Numerical integration..........................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................77
6.1.1 trapezoid rule..................................................................................................................77
Simpson's Rules............................................................................................................................78
Integration of equations....................................................................................................................91
Romberg integration.....................................................................................................................91
Gaussian quadrature......................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials......................................................................96
Ordinary differential equations..........................................................................................................107
7.1 EDO with Taylor......................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx..................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx...................................................................................................112
7.4 Runge Kutta d2y/dx2................................................................................................................115
partial differential equations..............................................................................................................117
8.1 EDP Parabolic.....................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................118
8.3 EDP Parabolic implicit method................................................................................................121
8.4 EDP Ellipticals....................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................127
8.6 EDP Elliptical implicit method................................................................................................129
8.7 Hyperbolic EDPs......................................................................................................................136

for i in range (1,n+1):

x_0 = 0.5 * (b - a) * m4[i-1][0] + 0.5 * (b - a) funcEval = f(x_0) w_0 = m4[i-1][1]


sum += w_0 * funcEval
print (f "{i:^5}| {m4[i-1][0]:.8f} | {x_0:.8f} | {funcEval:.8f} | {w_0:.8f} | {w_0* funcEval:.8f} |" )
I = 0.5 * (b - a) * sum
Nonlinear equations...............................................................................................................................5
1.1 Bisection Method....................................................................................................................5
1.2 Newton Raphson method........................................................................................................5
1.3 Secant Method............................................................................................................................15
6.2 Integration of equations
10
6
1.4 Modified secant..........................................................................................................................18
Systems of nonlinear equations............................................................................................................28
2.1 Fixed Point Method....................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)..................................................................................28
2.2 Newton Raphson method...........................................................................................................33
2.3 Broyden method.........................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)................................................................................38
3.1 General Gauss Method...............................................................................................................35
3.2 Gauss Jordan method..................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................47
Jacobbi..............................................................................................................................................49
Interpolation.........................................................................................................................................57
5.1 Finite differences...................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................59
5.3 Interpolation polynomial............................................................................................................63
5.4 Lagrangian interpolation............................................................................................................65
5.5 Cubic Tracers.............................................................................................................................67
Numerical integration..........................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................77
6.1.1 trapezoid rule..................................................................................................................77
Simpson's Rules............................................................................................................................78
Integration of equations....................................................................................................................91
Romberg integration.....................................................................................................................91
Gaussian quadrature......................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials......................................................................96
Ordinary differential equations..........................................................................................................107
7.1 EDO with Taylor......................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx..................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx...................................................................................................112
7.4 Runge Kutta d2y/dx2................................................................................................................115
partial differential equations..............................................................................................................117
8.1 EDP Parabolic.....................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................118
8.3 EDP Parabolic implicit method................................................................................................121
8.4 EDP Ellipticals....................................................................................................................126
6.2 Integration of equations
10
7
8.5 EDP elliptical iterative method...........................................................................................127
8.6 EDP Elliptical implicit method................................................................................................129
8.7 Hyperbolic EDPs......................................................................................................................136

"""To use a different number of points change: n == 'points' and the matrices m(n) """
if n == 6:
for i in range (1,n+1):
x_0 = 0.5 * (b - a) * m6[i-1][0] + 0.5 * (b - a) funcEval = f(x_0) w_0 = m6[i-1][1] sum += w_0 *
funcEval
print (f "{i:^5}| {m6[i-1][0]:.8f} | {x_0:.8f} | {funcEval:.8f} | {w_0:.8f} | {w_0* funcEval:.8f} |" )
I = 0.5 * (b - a) * sum
Nonlinear equations...............................................................................................................................5
1.1 Bisection Method....................................................................................................................5
1.2 Newton Raphson method........................................................................................................5
1.3 Secant Method............................................................................................................................15
1.4 Modified secant..........................................................................................................................18
Systems of nonlinear equations............................................................................................................28
2.1 Fixed Point Method....................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168)..................................................................................28
2.2 Newton Raphson method...........................................................................................................33
2.3 Broyden method.........................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)................................................................................38
3.1 General Gauss Method...............................................................................................................35
3.2 Gauss Jordan method..................................................................................................................40
4.1 Gauss Seidel..........................................................................................................................47
Jacobbi..............................................................................................................................................49
Interpolation.........................................................................................................................................57
5.1 Finite differences...................................................................................................................57
5.2 Differences Divided - (Newton Polynomial).............................................................................59
5.3 Interpolation polynomial............................................................................................................63
5.4 Lagrangian interpolation............................................................................................................65
5.5 Cubic Tracers.............................................................................................................................67
Numerical integration..........................................................................................................................77
6.1 Newton-Cotes integration formulas......................................................................................77
6.2 Integration of equations
10
8
6.1.1 trapezoid rule..................................................................................................................77
Simpson's Rules............................................................................................................................78
Integration of equations....................................................................................................................91
Romberg integration.....................................................................................................................91
Gaussian quadrature......................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials......................................................................96
Ordinary differential equations..........................................................................................................107
7.1 EDO with Taylor......................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx..................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx...................................................................................................112
7.4 Runge Kutta d2y/dx2................................................................................................................115
partial differential equations..............................................................................................................117
8.1 EDP Parabolic.....................................................................................................................117
8.2 EDP Parabolic explicit method................................................................................................118
8.3 EDP Parabolic implicit method................................................................................................121
8.4 EDP Ellipticals....................................................................................................................126
8.5 EDP elliptical iterative method...........................................................................................127
8.6 EDP Elliptical implicit method................................................................................................129
8.7 Hyperbolic EDPs......................................................................................................................136
1
0
7

Ordinary differential equations

7.1 EDO with Taylor


In Taylor methods, the result is approximated in terms of the series, for which the problem expression is adjusted to
the corresponding derivative.

Exercise 7.1

You are asked to find solution points in the differential equation using the first three terms. us of the Taylor series
with h = 0.1 and initial point x 0 = 0, y 0 = 1

y-y-x+x-1=0

The solution begins using the Taylor Series for three terms:

h2
y i + 1 = y i + hy i 0 + 2! and i 00

xi+1=xi+h

E = h 3 3 ! y000 ( z ) = O ( h3 )

Then, by removing the value of y? from the given formula, you can obtain y» by combining the equations.

y0=y-x2+x+1

y 00 = y 0 - 2 x + 1

= ( y - x + x + 1) - 2 x + 1

y=y-x-x+2

equations that allow estimating new values y i + 1 for new sample points distanced by i ∗ h from the initial point.
We begin by evaluating the new point at a distance x 1 = x 0 + h from the point of origin, obtaining y 1 , repeating the
process for the next point successively.
Algorithm with Python

To simplify the calculations, a function edo t aylor 3 t () is created to find the values for a can number of samples
distanced from each other h times from the initial point [ x 0, y 0]
Python code - EDO with Taylor
7.1 EDO with Taylor
10
#
#
EDO. Taylor method 3 terms
estimate the solution for samples spaced h on the x axis
8
# initial values x0,y0
# returns array [[x,y]] import numpy as np

def edo_taylor3t(d1y,d2y,x0,y0,h,samples):
size = samples + 1
estimate = np.zeros(shape=(size,2),dtype= float ) # includes point [x0,y0] estimate[0] =
[x0,y0] x = x0
y = y0
for i in range (1, size, 1):
y = y + h * d1y(x,y) + ((h ** 2)/2) * d2y(x,y) x = x+h
estimate[i] = [x,y] return (estimate)

The function can be used in a program that aims to solve the proposed problem:

# TEST PROGRAM
# Ref Rodriguez 9.1.1 p335 example.
# test y'-y-x+(x ** 2)-1 =0, y(0)=1

# INCOME.
Nonlinear equations..................................................................................................................................5
1.1 Bisection Method........................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers.................................................................................................................................67
Numerical integration..............................................................................................................................77
7.1 EDO with Taylor
10
6.1 Newton-Cotes integration formulas.........................................................................................77 9
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136

x0 = 0
y0 = 1 h = 0.1 samples = 5

# PROCEDURE
points = edo_taylor3t(d1y,d2y,x0,y0,h,samples)
xi = points[:,0]
yi = points[:,1]

# EXIT
print ( 'estimated[xi,yi]' )

print (dots)

Resulting in

Nonlinear equations 5
1.1 Bisection Method 5
1.2 Newton Raphson method 5
1.3 Secant Method 15
1.4 Modified secant 18
Systems of nonlinear equations 28
7.1 EDO with Taylor
11
2.1 Fixed Point Method 28 0
Exercise 2.1 ( Exercise No. 6.12 / Page. 168) 28
2.2 Newton Raphson method 33
2.3 Broyden method 38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168) 38
3.1 General Gauss Method 35
3.2 Gauss Jordan method 40
4.1 Gauss Seidel 47
Jacobbi 49
Interpolation 57
5.1 Finite differences 57
5.2 Differences Divided - (Newton Polynomial) 59
5.3 Interpolation polynomial 63
5.4 Lagrangian interpolation 65
5.5 Cubic Tracers 67
Numerical integration 77
6.1 Newton-Cotes integration formulas 77
6.1.1 trapezoid rule 77
Simpson's Rules 78
Integration of equations 91
Romberg integration 91
Gaussian quadrature 94
6.2.3 Gauss quadrature with legendre polynomials 96
Ordinary differential equations 107
7.1 EDO with Taylor 107
7.2 Runge-Kutta 2nd Order dy/dx 112
7.3 Runge-Kutta 4th Order dy/dx 112
7.4 Runge Kutta d2y/dx2 115
partial differential equations 117
8.1 EDP Parabolic 117
8.2 EDP Parabolic explicit method 118
8.3 EDP Parabolic implicit method 121
8.4 EDP Ellipticals 126
8.5 EDP elliptical iterative method 127
8.6 EDP Elliptical implicit method 129
8.7 Hyperbolic EDPs 136

0.2 1.461025
7.1 EDO with Taylor
11
0.3 1.73923262
1
0.4 2.05090205
0.5 2.39744677

Table 7.1: ODE with Taylor

Error Calculation
The ordinary differential equation in the exercise has a known solution, which allows us to find the real error at each
point with respect to the estimated approximation.

y=ex+x+x2

Note that the error grows as you move away from the initial point.

# BUG vs known solution


y_sol = lambda x: ((np.e) ** x) + x + x ** 2

yi_psol = y_sol(xi)
errors = yi_psol - yi
errormax = np. max ( np.abs (errors))

# EXIT
print ( 'Estimated maximum error: ' ,errormax)
print ( 'between dots: ' )
print (errors)

Graph
To visualize the results, the estimated points and many more points are used for the co-solution. known in the study
interval. With the graph you can see the differences between the solutions found.

# GRAPH [a,b+2*h]
a = x0
b = h * samples+2 * h
sampling = 10 * samples+2

xis = np.linspace(a,b,sampling) yis = y_sol(xis)

# Graphic
import matplotlib.pyplot as plt
plt.plot(xis,yis, label= 'and known' )
plt.plot(xi[0],yi[0], 'o' , color= 'r' , label = '[x0,y0]' ) plt.plot(xi[1:],yi[1:], 'o' , color= 'g' , label = 'y
estimated' ) plt.title( 'EDO: Solution with Taylor 3 terms' ) plt.xlabel( 'x' )
plt.ylabel( 'y' )
plt.legend()
plt.grid()
plt.show()
7.1 EDO with Taylor
11
2

Figure 7.1: Edo with Taylor

7.2 Runge-Kutta 2nd Order dy/dx


Runge-Kutta methods improve the approximation to the response without requiring determination of the
expressions. sions of higher-order derivatives, use a correction to the derivative by taking values from points around
it.
For example, 2nd Order Runge-Kutta uses the average between the increments x i and x i + h , calculated as terms K 1
and K 2.
Python code - Runge Kutta

# EDO. RungeKutta 2nd Order Method


# estimate the solution for samples spaced h on the x axis
7.2 Runge-Kutta 2nd Order dy/dx

# initial values x0,y0 # returns array [[x,y]]

def rungekutta2(d1y,x0,y0,h,samples):
size = samples + 1
estimate = np.zeros(shape=(size,2),dtype= float ) # includes point [x0,y0] estimate[0] = [x0,y0] xi = x0
yi = y0
for i in range (1, size, 1):
K1 = h * d1y(xi,yi)
K2 = h * d1y(xi+h, yi + K1) yi = yi + (K1+K2)/2 xi = xi + h estimated[i] = [xi,yi] return
(estimated)

# INCOME.
# d1y = y' = f, d2y = y'' = f' d1y = lambda x,y: y -x ** 2 + x + 1 x0 = 0
y0 = 1
h = 0.1
samples = 5

# PROCEDURE
pointsRK2 = rungekutta2(d1y,x0,y0,h,samples)
xi = pointsRK2[:,0]
yiRK2 = pointsRK2[:,1]

# EXIT
print ( 'estimated[xi,yi]' ) print (RK2points)

# BUG vs known solution


y_sol = lambda x: ((np.e) ** x) + x + x ** 2

yi_psol = y_sol(xi)
errors = yi_psol - yiRK2
errormax = np. max ( np.abs (errors))
# EXIT
7.3 Runge-Kutta 4th Order dy/dx
11
print ( 'Estimated maximum error: ' ,errormax) print ( 'between points: ' ) print (errors)
2
# GRAPH [a,b+2*h]
a = x0
b = h * samples+2 * h
sampling = 10 * samples+2
xis = np.linspace(a,b,sampling) yis = y_sol(xis)

# Graphic
import matplotlib.pyplot as plt
plt.plot(xis,yis, label= 'and known' )
plt.plot(xi[0],yiRK2[0], 'o' , color= 'r' , label = '[x0,y0]' ) plt.plot(xi[1:],yiRK2[1:], 'o' , color= 'm'
, label = 'y Runge-Kutta 2 Order' )
plt.title( 'EDO: Solution with Runge-Kutta 2nd Order' ) plt.xlabel( 'x' ) plt.ylabel( 'y' )
plt.legend()
plt.grid()
plt.show()

Exercise 7.2

To test the algorithm, the same equation of the problem presented in ?EDO with Taylor?

y-y-x+x-1=0

which applied with Runge Kutta, we obtain: Obtaining as a result

7.3 Runge-Kutta 4th Order dy/dx


For a first-order differential equation with a starting condition, the 4th -order Runge-Kutta formula is obtained from
the expression with five terms

y i + 1 = y i + aK 1 + bK 2 + cK 3 + dK 4

being:
y0 ( x ) = f ( xi , yi )
y ( x0 ) = y0
7.3 Runge-Kutta 4th Order dy/dx
11
xiyi
3
0.0 1.0
0.1 1.2145
0.2 1.4599725
0.3 1.73756961
0.4 2.0485644
0.5 2.39436369

Table 7.2: Runge - Kutta


Estimated maximum error: 0.00435758459732

must be equivalent to the 5-term Taylor series:

y i + 1 = y i + hf ( x i , y i ) + h 2! f 0 ( x i , y i ) + h 3! f 00 ( xx i , y i ) + h 4! f 000 ( x i , y i ) + O ( h 5 )

xi+1=xi+h

that using derivative approximations, we obtain:


Python code - Runge Kutta 4

def rungekutta4(d1y,x0,y0,h,samples):
# 4th Order Runge Kutta
size = samples + 1
estimate = np.zeros(shape=(size,2),dtype= float )

# includes point [x0,y0] estimated[0] = [x0,y0] xi = x0


yi = y0
for i in range (1, size, 1):
K1 =h * d1y(xi,yi)
Nonlinear equations..................................................................................................................................5
1.1 Bisection Method........................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
7.3 Runge-Kutta 4th Order dy/dx
11
Jacobbi.................................................................................................................................................49 4
Interpolation............................................................................................................................................57
5.1 Finite differences......................................................................................................................57
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers.................................................................................................................................67
Numerical integration..............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136

yi = yi + (1/6)*(K1+2*K2+2*K3 +K4) xi = xi + h

estimated[i] return (estimated)


[xi,yi]
7.4 Runge Kutta d2y/dx2
11
7.4 Runge Kutta d2y/dx2
5
For systems of ordinary differential equations with starting conditions at x 0 , y 0 , y 0 0

δ 2 and δ y
2 = δ x + etc
δ x

Standardized form of the equation:

y 00 = y 0 + etc

Converts to:
y0=z=fx(x,y,z)
z 0 = ( y 0 ) 0 = z + etc = g x ( x , y , z )

with the starting conditions at x 0 , y 0 , z 0 and the methods for first derivatives can be reused, for example Runge-
Kutta of 2nd and 4th order .
Runge-Kutta 2nd Order has truncation error O ( h 3)
Runge-Kutta 4th Order has truncation error O ( h 5)

Code in Python - Runge-Kutta 2 do Order d 2 y / dx 2

import numpy as np

def rungekutta2_fg(f,g,x0,y0,z0,h,samples): size = samples + 1


estimate = np.zeros(shape=(size,3),dtype= float ) # includes point [x0,y0] estimate[0] = [x0,y0,z0] xi = x0 yi =
y0 zi = z0
for i in range (1, size, 1):
K1y = h * f(xi,yi,zi)
K1z = h * g(xi,yi,zi)

K2y = h * f(xi+h, yi + K1y, zi + K1z)


K2z = h * g(xi+h, yi + K1y, zi + K1z)

yi = yi + (K1y+K2y)/2
zi = zi + (K1z+K2z)/2
xi = xi + h

estimate[i] = [xi,yi,zi]
return (estimated)

Code in Python - Runge-Kutta 4 do Order d 2 y / dx 2


def rungekutta4_fg(fx,gx,x0,y0,z0,h,samples): size = samples + 1
estimate = np.zeros(shape=(size,3),dtype= float ) # includes point [x0,y0] estimate[0] = [x0,y0,z0] xi = x0
yi = y0 zi = z0 for i in range (1,size,1): K1y = h * fx(xi,yi,zi)
K1z = h * gx(xi,yi,zi)

K2y = h * fx(xi+h/2, yi + K1y/2, zi + K1z/2)


K2z = h * gx(xi+h/2, yi + K1y/2, zi + K1z/2)

K3y = h * fx(xi+h/2, yi + K2y/2, zi + K2z/2)


K3z = h * gx(xi+h/2, yi + K2y/2, zi + K2z/2)
7.4 Runge Kutta d2y/dx2
11
K4y =
K4z =
h
h
*

*
fx(xi+h, yi +
gx(xi+h, yi +
K3y, zi + K3z)
K3y, zi + K3z)
6
yi = yi + (K1y+2 * K2y+2 * K3y+K4y)/6 zi = zi + (K1z+2*K2z+2*K3z+K4z)/6 xi = xi + h

estimate[i] = [xi,yi,zi] return (estimate)


1
1
7

partial differential equations

8.1 EDP Parabolic


Parabolic Partial Differential Equations similar to the one shown, represent the heat equation for an isolated bar
subjected to heat sources at each end.
The temperature is represented in the exercise as u [ x , t ]
d2u du
=K
dx 2 dt

For the numerical solution, the equation is discretized using divided finite differences that are substituted into the
equation, for example:
-
u
i + 1, j 2 u i , j + u i - 1, j u
i,j+1
− u
i,j
=K
(∆x) 2
∆t

To better interpret the result, a mesh is used that at each node represents the temperature as the values u [ xi , tj ].
To simplify nomenclature, the subscripts i are used for the x- axis and j for the t- axis, leaving u [ i , j ].

Figure 8.1: Metal Bar

In the problem statement they had established the values on the borders:
– temperatures at the extremes Ta , Tb
- the initial temperature of the bar T 0,
- The parameter for the K bar.
8.2 EDP Parabolic explicit method
11
The result obtained is interpreted as the temperature values along the bar after trans worked a long time. The
8
temperatures at the ends of the bar vary between Ta and Tb over time.
The terms are regrouped to find the unknown values in the nodes.

Figure 8.2: Parabolic EDP

Using the mesh, there would be some ways to pose the solution, depending on the finite difference used: centered,
forward, backward.

8.2 EDP Parabolic explicit method


2u
d
du
=K
2
dx dt
Divided differences are used, where two values are required for the derivative of order one and three values for the
derivative of order two:
-
d2u u
i + 1, j 2 u i , j + u i - 1, j
dx 2 (∆x)2
du u i , j + 1 − u i , j
dt ∆t
The selection of the divided differences corresponds to the points that you want to use for the calculation, they are
best seen in the mesh graph.
The reference line is time t 0
Then they are substituted into the problem equation, obtaining:
-
u
i + 1, j 2 u i , j + u i - 1, j u
i,j+1
− u
i,j
=K
(∆x) 2
∆t
From the graph it stands out that there is only one unknown value in the formula, highlighted by the yellow dot
inside the triangle. In the equation it is represented by U [ i , j + 1].
8.2 EDP Parabolic explicit method
11
9

Figure 8.3: Metal Bar

∆t
Solving the equation, constant terms are grouped: λ = K ( ∆ x ) 2
leaving the equation, with the terms ordered from left to right as in the graph:

=
u
i,j+1 λ u i − 1, j + (1 − 2 λ ) u i , j + λ u i + 1, j

When solving it is found that each value in a yellow dot is calculated as a weighted sum of known values, so the
expansion is known as the explicit method. The weighting is determined by the terms P , Q , and R.
∆t
λ=
K ( ∆x ) 2
P=∆
Q=1-2∆
R=∆
=
u
i,j+1 P u i - 1, j + Q u i , j + R u i + 1, j
Formulas that are developed using an algorithm and considering that by decreasing the values of ? x and ? t the
number of operations increases.

Figure 8.4: Parabolic EDP


Python Code - Explicit Method

# Parabolic EDP d2u/dx2 = K du/dt


# explicit method, using finite differences
# Reference: Chapra 30.2 p.888 pdf.912
8.2 EDP Parabolic explicit method
12
# Rodriguez 10.2 p.406 import numpy as np import matplotlib.pyplot as plt
0
# INCOME
# Boundary values
Ta = 60
TB = 40
T0 = 25
# length at x a=0
b=1
# Constant K
K=4
# Step size dx = 0.1 dt = dx/10
# iterations in time n = 200

# PROCEDURE
# iterations in length xi = np.arange(a,b+dx,dx) m = len (xi) ultimox = m-1

# Results in table u[x,t]


u = np.zeros(shape=(m,n), dtype= float )

# initial values of u[:,j] j=0


ultimot = n-1
u[0,j]= Ta
u[1:ultimox,j] = T0
u[ultimox,j] = Tb

# factors P,Q,R lamb = dt/(K * dx ** 2) P = lamb


# = 1 - 2 * lamb

R = lamb

# Calculate U for each time + dt j=0


while not (j>=ultimot):
u[0,j+1] = Ta
for i in range (1,ultimox,1):
u[i,j+1] = P*u[i-1,j] + Q*u[i,j] + R*u[i+1,j] u[m-1,j+1] = Tb j=j+1

# EXIT
print ( 'Results table' )
np.set_printoptions(precision=2) print (u)

# Graph
jump = int (n/10)
if (jump == 0):
jump = 1
for j in range (0,n,jump):
vector = u[:,j]
8.2 EDP Parabolic explicit method
12
plt.plot(xi,vector)
1
plt.plot(xi,vector, '.r' )
plt.xlabel( 'x[i]' )
plt.ylabel( 't[j]' )
plt.title( 'Parabolic EDP Solution' ) plt.show()

If the number of points increases as ∆ x and ∆ t decrease, it is better to decrease the number of curves to use in the
graph to avoid overlapping them. The parameter ?jump? is used. to indicate how often calculated curves are
incorporated into the graph.
Note that in the graph the coordinates are taken as x vs t , while for the solution of the mesh in the matrix rows and
columns are used. In the matrix the first index is row and the second index is column.
A summary table of results is shown as an example.
Results table

60. 60. 60. .. ., 60. 60. 60.


25. 33,75 38,12 .. ., 57,93 57,93 57,93
25. 25. 27,19 .. ., 55,86 55,86 55,87

...,
2. 25. 25,94 .. 43,86 43,87
., 43,86
25. 28,75 30,62 .. ., 41,93 41,93 41,93
40. 40. 40. .. ., 40. 40. 40.
8.3 EDP Parabolic implicit method
The same exercise presented in the explicit method is used.

Nonlinear equations..................................................................................................................................5
1.1 Bisection Method........................................................................................................................5
1.2 Newton Raphson method...........................................................................................................5
1.3 Secant Method...............................................................................................................................15
1.4 Modified secant.............................................................................................................................18
Systems of nonlinear equations...............................................................................................................28
2.1 Fixed Point Method.......................................................................................................................28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168).....................................................................................28
2.2 Newton Raphson method..............................................................................................................33
2.3 Broyden method............................................................................................................................38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168)...................................................................................38
3.1 General Gauss Method..................................................................................................................35
3.2 Gauss Jordan method.....................................................................................................................40
4.1 Gauss Seidel.............................................................................................................................47
Jacobbi.................................................................................................................................................49
Interpolation............................................................................................................................................57
8.2 EDP Parabolic explicit method
12
5.1 Finite differences......................................................................................................................57
2
5.2 Differences Divided - (Newton Polynomial)................................................................................59
5.3 Interpolation polynomial...............................................................................................................63
5.4 Lagrangian interpolation...............................................................................................................65
5.5 Cubic Tracers.................................................................................................................................67
Numerical integration..............................................................................................................................77
6.1 Newton-Cotes integration formulas.........................................................................................77
6.1.1 trapezoid rule.....................................................................................................................77
Simpson's Rules...............................................................................................................................78
Integration of equations.......................................................................................................................91
Romberg integration........................................................................................................................91
Gaussian quadrature.........................................................................................................................94
6.2.3 Gauss quadrature with legendre polynomials.........................................................................96
Ordinary differential equations.............................................................................................................107
7.1 EDO with Taylor.........................................................................................................................107
7.2 Runge-Kutta 2nd Order dy/dx.....................................................................................................112
7.3 Runge-Kutta 4th Order dy/dx......................................................................................................112
7.4 Runge Kutta d2y/dx2...................................................................................................................115
partial differential equations.................................................................................................................117
8.1 EDP Parabolic........................................................................................................................117
8.2 EDP Parabolic explicit method...................................................................................................118
8.3 EDP Parabolic implicit method...................................................................................................121
8.4 EDP Ellipticals.......................................................................................................................126
8.5 EDP elliptical iterative method..............................................................................................127
8.6 EDP Elliptical implicit method...................................................................................................129
8.7 Hyperbolic EDPs.........................................................................................................................136


du u
i,j u
i,j−1
dt ∆ t
The selection of the divided differences corresponds to the points that you want to use for the calculation, they are
best seen in the mesh graph.
8.2 EDP Parabolic explicit method
12
3

Figure 8.5: Metal Bar

Then they are substituted into the problem equation, obtaining:


-
u
i + 1, j 2 u i , j + u i - 1, j u
i,j
− u
i,j−1
=K
(∆x) 2
∆t
From the graph it stands out that in the formula there are only TWO unknown values , highlighted by the yellow
dots inside the triangle. In the equation it is represented by U [ i , j ] and U [ i + 1, j ]. Therefore, it will be necessary
to create a system of equations over the entire timeline t 1 to solve the problem.

Figure 8.6: Implicit EDP

∆t
Solving the equation, constant terms are grouped: λ = K ( ∆ x ) 2

λ u i − 1, j + ( − 1 − 2 λ ) u i , j + λ u i + 1, j = − u i , j − 1

The parameters P, Q and R are determined in a similar way to the explicit method:

P=λ

Q =- 1 - 2 λ

R=λ

P u i - 1, j + Q u i , j + R u i + 1, j = - u i , j - 1

The values at the extremes are known, for the intermediate points a system of equations is created and then use the
form Ax = B and solve the values for each u ( xi , t j ).
For example, with four sections between ends we have: time index is 1 and x index is 1. i = 1, j = 1

Pu 0 , 1 + Q u 1 , 1 + R u 2 , 1 = − u 1, 0 i = 2, j = 1

Pu 1.1 + Qu 2.1 + Ru 3.1 = - u 2.0 i = 3, j = 1


8.2 EDP Parabolic explicit method
12
P u
2.1
+
Q u
3.1
+
R u
4.1
= - u
3.0 grouping equations and substituting known
4
values:

Yo Qu 1.1 + Ru 2.1 + 0 = - T 0 - PT A
P u 1,1 + Qu 2,1 + Ru 3,1 = -T0
[ 0 + Pu 2.1 + Qu 3.1 = - T or - RT B
which generates the matrix to be solved:

QR 0 - T 0 - PT A
PQR -T0
0 PQ - T 0 - RT B
Use one of the methods from unit 3 to solve the system and obtain the corresponding values. Due to the extension of
the solution, it is convenient to use an algorithm and convert the relevant steps or parts you to functions.
Python code - for the solution with the implicit method.

# Parabolic EDP d2u/dx2 = K du/dt


# implicit method
# Reference: Chapra 30.3 p.895 pdf.917
# Rodriguez 10.2.5 p.417
import numpy as np
import matplotlib.pyplot as plt
# INCOME
# Boundary values
Ta = 60
TB = 40
T0 = 25
# length at x a=0
b=1
# Constant K
K=4
# Step size dx = 0.1 dt = 0.01
# iterations in time n = 100

# PROCEDURE
# x values
xi = np.arange(a,b+dx,dx) m = len (xi)
ultimox = m-1

# Results in u table
u = np.zeros(shape=(m,n), dtype= float )
# initial values of u[:,j] j=0
ultimot = n-1
u[0,j]= Ta
u[1:ultimox,j] = T0
u[ultimox,j] = Tb

# factors P,Q,R lamb = dt/(K * dx ** 2) P = lamb


8.2 EDP Parabolic explicit method
12
Q = -1 -2 * lamb
5
R = lamb

# Calculate U for each time + dt j=1


while not (j>=n):
u[0,j] = Ta
u[m-1,j] = Tb

# Matrix of equations

size = m-2
A = np.zeros(shape=(size,size), dtype = float ) B = np.zeros(size, dtype = float ) for f in range
(0,size,1): if (f>0): A [f,f-1]=PA[f,f] = Q if (f<(size-1)):
A[f,f+1]=R
B[f] = -u[f+1,j-1]
B[0] = B[0]-P*u[0,j]
B[size-1] = B[size-1]-R * u[m-1,j] # Solve system of equations C = np.linalg.solve(A, B)

# copy results au[i,j] for f in range (0,size,1): u[f+1,j] = C[f]

# next iteration j=j+1

# EXIT
print ( 'Results table' ) np.set_printoptions(precision=2) print (u) # Plot jump = int (n/10) if (jump == 0): jump =
1 for j in range (0,n, jump): vector = u[:,j] plt.plot(xi,vector) plt.plot(xi,vector, '.m' ) plt.xlabel( 'x[i]' ) plt.ylabel(
't[ j]' ) plt.title( 'Parabolic EDP Solution' ) plt.show()

Results table
8.4 EDP
12
6
Ellipticals

60. 60. 60. .. ., 60. 60. 60.


25. 31,01 35,25 .. ., 57,06 57,09 57,11
25. 26,03 27,49 .. ., 54,22 54,26 54,31

.. .,
25. 25,44 42,27 42,31
26,07 .. ., 42,22
25. 27,57 29,39 .. ., 41,07 41,09 41,11
40. 40. 40. .. ., 40. 40. 40.

8.4 EDP Ellipticals


The elliptic type Partial Differential Equations similar to the one shown:

δ2u δu=
2
δx δ and 2

(Laplace equation, Poisson equation with f ( x , y ) = 0)


It is interpreted as a metal plate of dimensions Lx and Ly , thin with insulator covering the faces of the plate, and
subject to conditions at the boundaries:
Lx = x dimension of metal plate
Ly = dimension y of metal plate
u[0,y] = Ta
u[Lx,y] = Tb
u[x,0] = Tc
u[x,Ly] = Td
Result that is interpreted as a mesh with each node as a combination of values u [ xi , yj ], to simplify nomenclature
the subscripts i are used for the x axis and j for the t axis, leaving u [ i , j ] .

Figure 8.7: Metal Plate

The equation is discretized using divided finite differences which are substituted into the equation, e.g. pl:
-
u
2 u i - 1, j u i , j + 1 − 2 u i , j + u i , j − 1
i + 1, j
(∆x)2 +

=
0
(∆y)2
the terms of the differentials could be grouped:
8.5 EDP elliptical iterative method
12
7
( ∆ y ) 2u − +u + − + =
2 ( i + 1, j 2 i , j
u
i − 1, j ) u i , j + 1 2 u i , j u i , j − 1 0 ( ∆ x )

and suppose that (∆y)2


λ= =1
(∆x)2
or that the deltas are the same

-
u
i + 1, j 4 u i , j + u i - 1, j + u i , j + 1 + u i , j - 1 = 0

8.5 EDP elliptical iterative method


With the result developed in elliptical EDP for:

δuδu
δx2δy2
and with the assumption
that: (∆y)2
λ =
( ∆ x ) 2 =1

It can be stated that:

-
u
i + 1, j 4 u i , j + u i - 1, j + u i , j + 1 + u i , j - 1 = 0
1
=
u
i,j 4 [ u i + 1, j + u i - 1, j + u i , j + 1 + u i , j - 1 ]

which represents that each point is the result of the average of the points around the rhombus.
The result can be calculated iteratively by making several passes through the array, averaging each point. Iteration
control requires a maximum number of iterations and convergence control. The results graph requires axis
adjustment to graph, since the row index is the x axis, and the columns nas is the y axis. The graphed matrix is
obtained as the transpose of u

Code in Python - EDP elliptical iterative method

# Partial differential equations


# Elliptical. Iterative method import numpy as np

# INCOME
# Initial boundary conditions
Ta = 60
TB = 60
Tc = 50
Td = 70
# plate dimensions
x0 = 0
xn = 2
y0 = 0
yn = 1.5
# discretize, assume dx=dy dx = 0.25
dy = 0.25
8.5 EDP elliptical iterative method
12
8
maxitera = 100
tolerates = 0.0001

# PROCEDURE
xi = np.arange(x0,xn+dx,dx)
yj = np.arange(y0,yn+dy,dy) n = len (xi)
m = len (yj) # Matrix u u = np.zeros(shape=(n,m),dtype = float ) # values in boundaries
u[0,:] = Ta
u[n-1,:] = Tb
u[:,0] = Tc
u[:,m-1] = Td

# average iteration initial value = (Ta+Tb+Tc+Td)/4 u[1:n-1,1:m-1] = average # iterate interior points iterate =
0 converge = 0
while not (itera>=maxitera and converge==1):
iterate = iterate +1 new = np.copy(u) for i in range (1,n-1): for j in range (1,m-1):
u[i,j] = (u[i-1,j]+u[i+1,j]+u[i,j-1]+u[i,j+1])/4 difference = new- or
erroru = np.linalg.norm(np. abs (difference)) if (erroru<tolerance): converge=1

# EXIT
np.set_printoptions(precision=2) print ( 'converge = ' , converge) print ( 'xi=' )
print (xi)
print ( 'yj=' ) print (yj)
8.6 EDP Elliptical implicit method
12
9
print ( 'u array' ) print (u)

# Graph
import matplotlib.pyplot as plt from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

X, Y = np.meshgrid(xi, yj)
U = np.transpose(u) # setting row indices is x figure = plt.figure() ax = Axes3D(figure)
ax.plot_surface(X, Y, U, rstride=1, cstride=1, cmap=cm.Reds) plt.title( 'EDP elliptical' ) plt.xlabel( 'x' )
plt.ylabel( 'y' )
plt.show()

You get the results:

converges = 1 xi=
[ 0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2.]
yi=
[ 0. 0.25 0.5 0.75 1. 1.25 1.5]
Results table, U matrix

50,0 60,00 60,00 60,00 60,00 60,00 70,00


50,0 55,60 58,23 60,00 61,77 64,4 70,00
50,0 54,15 57,34 60,00 62,66 65,85 70,00
50,0 53,67 56,97 60,00 63,03 66,33 70,00
50,0 53,55 56,87 60,00 63,13 66,45 70,00
50,0 53,67 56,97 60,00 63,03 66,33 70,00
50,0 54,15 57,34 60,00 62,66 65,85 70,00
50,0 55,60 58,23 60,00 61,77 64,40 70,00
50,0 60,00 60,00 60,00 60,00 60,00 70,00

8.6 EDP Elliptical implicit method


With the result developed in EDP ellipticals for:
δ2uδu
δx2δy2

and with the assumption


that (∆y)2
λ =
( ∆ x ) 2 =1
8.6 EDP Elliptical implicit method
13
0
Elliptical
EDP

70.0 '
67.5 '
65.0 '
62.5 '
60.0 '
57.5 '
55.0
52.5
50.0

14
\
1.2 *
1.0 %
0.8 *
and 0.6 %
1.75
— 1.50
0.4
0.2
M—1.001.25 2.00
0 .25
0.500.75 x
n nn

Figure 8.8: function x 3.5 = 80

It can be stated that:

-
4 + + + =
u
i + 1, j u
i,j u
i - 1, j u
i,j+1 u
i,j-1 0

Therefore, for the implicit method, a system of equations is proposed to determine the values at each unknown point.
j = 1, i = 1

-
u
2.1 4 u 1.1 + u 0.1 + u 1.2 + u 1.0 = 0
u 2.1 - 4 u 1.1 + T a + u 1.2 + T c = 0

-4u1,1+u2,1+u1,2=-(Tc+Ta)

j = 1, i = 2
8.6 EDP Elliptical implicit method
13
1
-
u
3.1 4 u 2.1 + u 1.1 + u 2.2 + u 2.0 = 0
u 3.1 - 4 u 2.1 + u 1.1 + u 2.2 + T c = 0

u 1,1 - 4 u 2,1 + u 3,1 + u 2,2 = - T c

j = 1, i = 3

-
4 + + + =
u
4.1 u
3.1 u
2.1 u
3.2 u
3.0 0

T b - 4 u 3.1 + u 2.1 + u 3.2 + T c = 0

u2,1 - 4 u3,1 + u3,2 = - ( Tc + Tb )

j = 2, i = 1

-
u
2.2 4 u 1.2 + u 0.2 + u 1.3 + u 1.1 = 0
8.6 EDP Elliptical implicit method
13
2
u 2.2 - 4 u 1.2 + T a + u 1.3 + u 1.1 = 0 - 4 u 1.2 + u 2.2 + u 1.1 + u 1.3 = - T a j = 2, i = 2

-
u
1,2 4 u 2,2 + u 3,2 + u 2,3 + u 2,1 = 0 j = 2, i = 3

-
u
4.2 4 u 3.2 + u 2.2 + u 3.3 + u 3.1 = 0
T b - 4 u 3.2 + u 2.2 + u 3.3 + u 3.1 = 0 u 2.2 - 4 u 3.2 + u 3.3 + u 3.1 = - T b j = 3, i = 1

-
u
2.3 4 u 1.3 + u 0.3 + u 1.4 + u 1.2 = 0
u2,3-4u1,3+Ta+Td+u1,2=0

- 4 u 1,3 + u 2,3 + u 1,2 = - ( T d + T a ) j = 3, i = 2

-
u
3.3 4 u 2.3 + u 1.3 + u 2.4 + u 2.2 = 0
-
u
3.3 4 u 2.3 + u 1.3 + T d + u 2.2 = 0
-
u
1,3 4 u 2,3 + u 1,3 + u 2,2 = - T d j = 3, i = 3

-
u
4.3 4 u 3.3 + u 2.3 + u 3.4 + u 3.2 = 0
tb−4u3,3+u2,3+Td+u3,2=0

u 2.3 - 4 u 3.3 + u 3.2 = - ( T d + T b )

Python Code - Multiple Trapezoid Rule

# Partial differential equations


# Elliptical. Implicit method import numpy as np

# INCOME
# Initial boundary conditions
Ta = 60
TB = 60
Tc = 50
Td = 70
# plate dimensions
x0 = 0
xn = 2
y0 = 0
yn = 1.5
# discretize, assume dx=dy sectionsx = 4
spansy = 4
dx = (xn-x0)/sectionsx
dy = (yn-y0)/legsy
maxitera = 100
tolerates = 0.0001

A = np.array([
Nonlinear equations 5
8.6 EDP Elliptical implicit method
13
3
1.1 Bisection Method 5
1.2 Newton Raphson method 5
1.3 Secant Method 15
1.4 Modified secant 18
Systems of nonlinear equations 28
2.1 Fixed Point Method 28
Exercise 2.1 ( Exercise No. 6.12 / Page. 168) 28
2.2 Newton Raphson method 33
2.3 Broyden method 38
Exercise 2.5 I ( Exercise No. 6.12 / Page. 168) 38
3.1 General Gauss Method 35
3.2 Gauss Jordan method 40
4.1 Gauss Seidel 47
Jacobbi 49
Interpolation 57
5.1 Finite differences 57
5.2 Differences Divided - (Newton Polynomial) 59
5.3 Interpolation polynomial 63
5.4 Lagrangian interpolation 65
5.5 Cubic Tracers 67
Numerical integration 77
6.1 Newton-Cotes integration formulas 77
6.1.1 trapezoid rule 77
Simpson's Rules 78
Integration of equations 91
Romberg integration 91
Gaussian quadrature 94
6.2.3 Gauss quadrature with legendre polynomials 96
Ordinary differential equations 107
7.1 EDO with Taylor 107
7.2 Runge-Kutta 2nd Order dy/dx 112
7.3 Runge-Kutta 4th Order dy/dx 112
7.4 Runge Kutta d2y/dx2 115
partial differential equations 117
8.1 EDP Parabolic 117
8.6 EDP Elliptical implicit method
13
4
8.2 EDP Parabolic explicit method 118
8.3 EDP Parabolic implicit method 121
8.4 EDP Ellipticals 126
8.5 EDP elliptical iterative method 127
8.6 EDP Elliptical implicit method 129
8.7 Hyperbolic EDPs 136

[ 0, 0, 0, 0, 0, 1, 0, 1,-4],
])
B = np.array([-(Tc+Ta),-Tc,-(Tc+Tb),
-Ta,0,-Tb,
-(Td+Ta),-Td,-(Td+Tb)])

# PROCEDURE
# Solve system equations
Xu = np.linalg.solve(A,B)
[nx,mx] = np.shape(A) xi = np.arange(x0,xn+dx,dx) yj = np.arange(y0,yn+dy,dy) n = len (xi)
m = len (yj) u = np.zeros(shape=(n,m),dtype= float )
u[:,0] = Tc
u[:,m-1] = Td
u[0,:] = Ta
u[n-1,:] = Tb
u[1:1+3,1] = Xu[0:0+3]
u[1:1+3,2] = Xu[3:3+3]
u[1:1+3,3] = Xu[6:6+3]
# EXIT
np.set_printoptions(precision=2) print ( 'xi=' )
print (xi)
print ( 'yj=' )
print (yj)
print ( 'u array' ) print (u)

# Graph
import matplotlib.pyplot as plt from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

X, Y = np.meshgrid(xi, yj)
U = np.transpose(u) # setting row indices is x figure = plt.figure() ax = Axes3D(figure)
ax.plot_surface(X, Y, U, rstride=1, cstride=1, cmap=cm.Reds) plt.title( 'EDP elliptical' ) plt.xlabel( 'x' )
plt.ylabel( 'y' )
plt.show()
8.6 EDP Elliptical implicit method
13
5

Figure 8.9: EDP Elliptical


8.7 Hyperbolic EDPs
13
6
8.7 Hyperbolic EDPs
The hyperbolic Partial Differential Equations similar to the one shown, for a particular example, represent the wave
equation of one dimension u [ x , t ], which represents the vertical displacement of a string.
δu 2δu
2=
δt c δx 2

Figure 8.10: The ends of the rope of unit length are fastened.

At the beginning the rope is stretched at its center point:


(u ( ,0)
x 0) í - 0.5 x .0 < x <= 0.5
u x
0.5( x - 1) ,0.5 < x < 1

The rope is released, with zero velocity from the initial position:
δu ( x ,00 )
δt =
Similar to the procedure for parabolic and elliptical PDEs, it is discretized with finite differences divided you give:
-
u
i,j+1 2ui,j+ui,j-1 2
u
i + 1, j
-
2 u i - 1, j
= c
( ∆t ) 2 (∆x)2
with error of the order O ( ∆ x ) 2 + O ( ∆ t ) 2
is regrouped in the form:
c2 ( ∆t ) 2
-
u
i,j+1 2ui,j+ui,j-1= ( u i + 1, j − 2 u i , j + u i − 1, j )
(∆x)2
the constant term, for ease of use, is simplified with the value of 1
c2 ( ∆t ) 2
λ= =1
(∆x)2
If c = 2 and ∆ x = 0.2, it follows that ∆ t = 0.1
which when substituted into the equation, is simplified by canceling the term u [ i , j ]:

u + u = u + u
i,j+1 i,j−1 i + 1, j i − 1, j
8.7 Hyperbolic EDPs
13
7

Figure 8.11: EDP Rope

Python Code - Multiple Trapezoid Rule

# Partial Differential Equations # Hyperbolic. Explicit method import numpy as np

def startstring(xi):
n = len (xi)
y = np.zeros(n,dtype= float )
for i in range (0,n,1):
if (xi[i]<=0.5):
y[i]=-0.5*xi[i]
else :
y[i]=0.5*(xi[i]-1) return (y)

# INCOME
x0 = 0
xn = 1 # String length
y0 = 0
yn = 0 # Tie points
t0 = 0
c=2
# discretize
sectionsx = 16
transt = 32
dx = (xn-x0)/sectionsx
dt = dx/c

# PROCEDURE
xi = np.arange(x0,xn+dx,dx)
tj = np.arange(0,tramost*dt+dt,dt) n = len (xi)

m = len (tj)

u = np.zeros(shape=(n,m),dtype= float ) u[:,0] = startstring(xi)


u[0,:] = y0
u[n-1,:] = yn
8.7 Hyperbolic EDPs
13
8
# Applying initial condition
j=0
for i in range (1,n-1,1):
u[i,j+1] = (u[i+1,j]+u[i-1,j])/2
# For the other points
for j in range (1,m-1,1):
for i in range (1,n-1,1):
u[i,j+1] = u[i+1,j]-u[i,j-1]+u[i-1,j]

# EXIT
np.set_printoptions(precision=2) print ( 'xi =' )
print (xi)
print ( 'tj =' )
print (tj)
print ( 'array u =' ) print (u)

Figure 8.12: Hyperbolic EDP

http://olivierlemaire.wordpress.com/2010/03/08/tableaux-tikz/? )

Bibliography

[1] W. Gautschi. Numerical Analysis. An Introduction. Birkhäuser, 1997.

[2] Q. Henrici. Essentials of Numerical Analysis. Wiley, New York, 1982.

[3] Steven C. Chapra, Raymond P. Canale Numerical methods for engineers Fifth Edition, Mc-Graw Hill
Publishing. Mexico, 2007.

[4] Rodriguez, L. BASIC NUMERICAL ANALYSIS An algorithmic approach with the support of Python Digital
8.7 Hyperbolic EDPs
13
9
book, Version 4.4 - 2016, Department of Mathematics, Faculty of Sciences Natu rals and Mathematics
(FCNM), ESPOL

[5] Burden, Richard L. Faires, J. Douglas Analisis Numerico (Spanish Edition) 9th Revised edition, Cengage
Learning Editores SA Wiley, New York, 1982.

[6] Alexánder Borbón A., Walter Mora F Editing of scientific texts L A T E X Instituto Tecnológico de Costa
Rica, 2017.

[7] Walter Mora F Introduction to Numerical Methods Technological Institute of Costa Rica, 2016.

[8] Cristian Castro Numerical Methods for Civil Engineering IC-343 Classroom - Semester 2019 - I

[9] ESPOL Numerical Analysis http://blog.espol.edu.ec/matg1013/category/ tema-por-


semana/unidad01-introduccion/

You might also like