Resolution Numerical Methods of The Chapra Book With Python PDF
Resolution Numerical Methods of The Chapra Book With Python PDF
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
4.2 Jacobbi 45
4.3 Issues 47
Bibliography 137
114
Nonlinear equations
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
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:' );
Solution
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
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
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
It is deduced from graphical interpretation or through the use of the Taylor series.
125
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
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----------------------------------------------------------------------------------------------------
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
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
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
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
Study and use graphical and analytical methods to explain any peculiarities in your results.
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
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
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
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
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.500000 2.500000 -0.996635 0.166396 2.356929 6.070241
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
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
N To solve the following exercises we will only change the function in the code.
Solution
1. In graphic form.
Solution
+ ∗
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
1. In graphic form.
Solution
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.
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
Solution
The root converges towards its real solution according to the graph.
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
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
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
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
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 .
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
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
return pfix3
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
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.
( 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()
import numpy as np
import matplotlib.pyplot as plt # Plotting the system.
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
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.
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
( 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.
ak , i
Ak=Ak-Ai
a
i,i
bi i-1 P ni - 1 j =ai +x 1j
ij
xi=
i - 1 ii
for i = n - 1, n - 2
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)
N For the following exercises we will use the same code, but we will change the functions to be analyzed.
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
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:
0
A= -10,0000 5,5000 -91,0000
37,3000 )
0
,
0 -18,6500
Result:
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
Solution
2 1 -1 1
5 2 2 -4
3 1 1 5
A=
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
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
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
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
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
n1bi-X Ai,jXj
xi= j = 0, j 6= i A
ii
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 | ]
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.
# Gauss-Seidel algorithm,
# arrays, iterative methods
# enter iteramax if more iterations are required
import numpy as np
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)
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]])
def Jacobi3x3(A,B):
x = np.asmatrix(np.zeros( len (A),1))
xk = np.asmatrix(np.zeros(( len (x),1))) RE = 1
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 ...")
w = -1 * w
res = np.multiply(wT,x)
res[I] = 0
sum = np. sum (res)
return sum
Jacobi(A, B)
4.3 Problems
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]])
[[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
Solution
# INCOME
A = np . array ([[15, - 3, - 1],
[-3,18,-6],
4.3 Problems
5
3
[-4, -1, 12]])
tolerates = 0.05
# EXIT
response from A. X = B :
[[320,20103859]
[227,19666023]
[321,50006788]]
check A. X :
[[3799,92553032]
[1199,93636106]
[2350.]]
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],
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]]
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]])
tolerates = 0.05
# EXIT
response from A. X = B :
[[0,6289026]
[9,06176403]
[-2,61342771]]
check A. X :
[[27,02598178]
[-61,48414743]
[-21,5]]
- 8 x 1 + x 2 - 2 x 3 = - 20
Solution
# INCOME
A = np . array ([[ - 8,1, - 2],
tolerates = 0.05
# EXIT
response from A. X = B :
[[3,99875792]
[7,99945128]
[-2,00061071]]
check A. X :
[[-19,98939064]
[-37,99858113]
[-34.]]
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.
Solution
# INCOME
A = np . ar ray ([[2.01475, - 0.020875, 0, 0],
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 ] ]
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
‘ 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
∆nf0
hn
is an approximation for f ( n ) in the interval [ x 0 , x n ] Advanced finite difference interpolation polynomial:
# 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
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:
4. 0.4 2. 0. 0. 0.
expanded polynomial:
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 ]
print (vector)
print (array)
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
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)
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.
Interpolation polynomial:
# 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
# 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()
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
# 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)
# 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
Sj(xj)=aj+bj(x-xj)+cj(x-xj)2+dj(x-xj)3
For each j = 0, 1, · · · , n - 1
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
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)
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)
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])
# 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
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)
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
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
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
expanded polynomial:
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
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
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:
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
Lagrange polynomial:
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
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
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
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
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).
N We saw the case of problem 18.4 in the previous problem, for that reason we will only do f (2.25)
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
The result of
f (2.25) = 8.007914847161564
7
7
Numerical integration
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.
Zbf (
x) Z b
f2(x)
I= to dx ∼ to dx
=
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
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).
(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
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
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
-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
x 0 = a = 0, x1 = 1, x2 = 2, x 3 = 3, x4=b=4
3,470592
x 0 = a = 0, x 1 = 1.333333, x 2 = 2.666666, x 3 = b = 4
I = 3.388365
For the following exercises we will use Python codes to solve the problems and we will present it in a box.
import numpy as np
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: " ))
(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" )
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 sum = 0
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
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(%)
n h Yo Ea(%)
4 0.392699 12.424803 67.365947
n h Yo Ea(%)
Table 6.4: Results from a single application of Simpson's 3/8 rule, n= 4 and 5
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(%)
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
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
Integrate the following function both analytically and with Simpson's rule, with n = 4 and 5.
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
(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
n h Yo Ea(%)
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.
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)
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
n h Yo Ea(%)
3 0.333333 518.804319 0.304347
3
Z
1
2µ
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
2µ
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
N The mistakes
25,833333333 - 25,83335064
²t =
25,833333333 ∗ 100%
² t = - 6.69936774194 E 5
Compare ² a and ² t .
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
j hj T 1, j T 2, j T 3, j T 4, j
0 3 90.38491615
with an accuracy of ² s = 0.5%. You should present your results in the form of Figure 22.3.
j hj T 1, j T 2, j T 3, j
0 2 1.34376994
1 1 1.81556261 1.97282684
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
Solution
j hj T 1, j T 2, j T 3, j T 4, j
1 1 0.00000000
The mistake is
² t = 4.78515246332 E - 2
I∼=c0f(x0)+c1f(x1)
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
# 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()
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
Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.00000000 -0.50000000 49.00000000 0.88888889 43.55555556
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
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
N In this example we notice that the Gauss - Legendre method works, since the error is smaller
to 128% for n = 2, 3, 4.
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
Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.86113631 1.86113631 1.38040240 0.34785485 0.48017966
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
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
Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.86113631 2.79170447 0.11371889 0.34785485 0.03955767
Yo zi xi f ( xi ) wi wi∗f(xi)
1 0.23861919 1.85792878 0.22462323 0.46791393 0.10510434
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)])
"""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
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
"""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
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
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.
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
# 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
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)
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
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
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
def rungekutta4(d1y,x0,y0,h,samples):
# 4th Order Runge Kutta
size = samples + 1
estimate = np.zeros(shape=(size,2),dtype= float )
yi = yi + (1/6)*(K1+2*K2+2*K3 +K4) xi = xi + h
δ 2 and δ y
2 = δ x + etc
δ x
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)
import numpy as np
yi = yi + (K1y+K2y)/2
zi = zi + (K1z+K2z)/2
xi = xi + h
estimate[i] = [xi,yi,zi]
return (estimated)
*
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
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 ].
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.
Using the mesh, there would be some ways to pose the solution, depending on the finite difference used: centered,
forward, backward.
∆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.
# PROCEDURE
# iterations in length xi = np.arange(a,b+dx,dx) m = len (xi) ultimox = m-1
R = lamb
# 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
...,
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
∆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
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.
# 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
# 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)
# 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
.. .,
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.
δ2u δu=
2
δx δ and 2
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 )
-
u
i + 1, j 4 u i , j + u i - 1, j + u i , j + 1 + u i , j - 1 = 0
δuδu
δx2δy2
and with the assumption
that: (∆y)2
λ =
( ∆ x ) 2 =1
-
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
# 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()
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
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
-
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
j = 1, i = 3
-
4 + + + =
u
4.1 u
3.1 u
2.1 u
3.2 u
3.0 0
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
-
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
# 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.10: The ends of the rope of unit length are fastened.
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
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)
# EXIT
np.set_printoptions(precision=2) print ( 'xi =' )
print (xi)
print ( 'tj =' )
print (tj)
print ( 'array u =' ) print (u)
http://olivierlemaire.wordpress.com/2010/03/08/tableaux-tikz/? )
Bibliography
[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