Chapter 1-2
Chapter 1-2
Chapter 1-2
SPRING 2014
INSTRUCTORS: PROF. DR. NECATİ AĞIRALİOĞLU and DOÇ. DR. MEHMET ÖZGER
COURSE OUTLINE
REFERENCES
1
CONTENTS
2. NUMERICAL ANALYSIS
2.1 INTRODUCTION
2.2 NEWTON-RAPHSON METHOD
2.3 LEAST SQUARES METHOD
2.4 CURVE FITTING
2.5 NUMERICAL INTEGRATION
2.5.1 Numerical Integration By Trapezoidal Rule
2.5.2 Numerical Integration By Simpson’s Rule
2.6 SOLVED PROBLEMS
2.6.1 Calculation of the water at depth downstream of a spillway
2.6.2 Example of Regression
2.6.3 Example for Polynomial Fitting
2.6.4 Example for Numerical Integration
2.6.5 Example for Numerical Integration
2.6.6 Example for Numerical Integration by FORTRAN Programming
2.6.7 Example for Numerical Integration By BASIC Programming
REFERENCES
5
CHAPTER 1
INTRODUCTION
Continuous Discrete
curve approximation
6
The main stages in a CH simulation are:
Pre-processing: (a) formulation of the problem (governing equations and boundary
conditions), (b) construction of a computational mesh (a set of control volumes).
Solving: (a) discretisation of the governing equations, (b) solution of the resulting algebraic
equations.
Post-processing: (a) visualization (graphs and plots of the solution); (b) analysis of results
(calculation of derived quantities: forces, flow rates, ... )
In hydraulics or in fluid mechanics this is usually expressed as rate by dividing with the time
interval (and transferring the net amount passing through the boundary to the LHS of the equation):
[RATE OF CHANGE (inside volume)] + [NET FLUX (out of boundary)] = [SOURCE(inside volume)]
(1.1)
The important point is that this is a single, generic equation, irrespective of whether the
physical quantity concerned is mass, momentum, chemical content, etc. Thus, instead of
dealing with too many different equations we can consider the numerical solution of a generic
scalar-transport equation.
The finite-volume method, which is the subject of this course, is based on approximation of
these control-volume equations.
7
V
In regions without shocks, interfaces or other discontinuities, the fluid-flow equations can also
be written in equivalent differential forms. These describe what is going on at a point rather than over
a whole control volume. Mathematically, they can be derived by making the control volumes infinitely
small.
The finite-difference method is based on the direct approximation of a differential form of the
governing equations.
8
development of body-fitted grids. By early 90’s, unstructured grid methods had appeared.
Advantages: basic FV control volume balance does not limit cell shape; mass, momentum, energy
conserved even on coarse grids; efficient, iterative solvers are well developed. Disadvantages: false
diffusion when simple numerics are used.
Finite volume: basic methodology: Integrate the differential equation over the control volume and
apply the divergence theorem. To evaluate derivative terms, values at the control volume faces are
needed and then it is required to make an assumption about how that values vary. The result is a set
of linear algebraic equations as one for each control volume solved iteratively or simultaneously.
Cells and nodes: Using finite volume method, the solution domain is subdivided into a finite number of
small control volumes (cells) by a grid. The grid defines the boundaries of the control volumes while
the computational node lies at the center of the control volume. The advantage of FVM is that the
integral conservation is satisfied exactly over the control volume.
9
One Dimensional Model – Model defined on one space coordinate, i.e., variables are averaged
over the other two directions, e.g., pipe flow models and numerical models of long wave propagation
in a narrow channel.
Physical Model – Model using the physical properties and behavior of modeling materials to
represent the prototype.
Prototype – The full-sized structure, system process, or phenomenon being modeled.
Quasi Steady-State Model – Model in which time-dependent variables are simulated by a
sequence of steady-state models.
Quasi Three-Dimensional Model – A combination of several two-dimensional models used to
simulate variations in three dimensions.
Simulation – Replication of the prototype using a model.
Steady-State Model – Model in which the variables being investigated are independent of
time.
Three-Dimensional Model – Model defined on three spaces coordinates, e.g., coastal models
and numerical models of explosions.
Two-Dimensional Model – Model defined on two space coordinates, i.e., variables are
averaged over the third direction (e.g., wave flume experiments, numerical models of storm surges).
Unsteady-State Model – Model in which the variables being investigated are time dependent.
Validation – Comparison of model results with a set of prototype data not used for verification.
Comparison includes: (1) Using a data set very similar to the verification data to determine the validity
of the model under conditions for which it was designed; (2) using a data set quite different from the
verification data to determine the validity of the model under conditions for which it was not designed
but could possibly be used; and (3) using post-construction prototype data to determine the validity of
the predictions based on model results.
Verification – Check of the behavior of an adjusted model against a set of prototype
conditions.
10
Distortion – Conscious departure from a scaling law often necessitated by a complex set of
prototype and laboratory conditions. The term is most commonly used for geometric distortion in
which the vertical and horizontal scales of a model are different.
Dynamic Similarity – Similarity of forces.
Euler Number = (p-p0)/ (ρV2) Dimensionless number related to the ratio of inertial force to
pressure force.
Froude Number = V/(gL)1/2 – Dimensionless Number related to ratio of inertial force to
gravitation force ; the length is often the hydraulic depth making this the ratio of velocity of flow to the
speed of propagation of a small disturbance. The numerical value of one distinguishes between
subcritical and super critical flow in open channels.
Geometric Similarity – Similarity in form
Kinematic Similarity – Similarity of motion.
Laboratory Effect – Consequences of necessary laboratory simplifications or physical
constraints on the model.
Mach number = V/(K/ρ)1/2 – Dimensionless number related to the ratio of inertial force to
elastic force ; also the ratio of velocity of flow to speed of sound in an infinite medium.
Mobility number (or shields’ parameter) = ρV2*/[(ρs-ρ)gD] – Dimensionless number related to
the ratio of shear force to submerged weight of a sediment particle.
Reynolds Number = Vl/ – Dimensionless ratio related to the ratio of inertial force to viscous
force; the length may include grain size, roughness, depth of flow, and pipe diameter, resulting in
different Reynolds Numbers for different purposes. The critical Reynolds Number describes the onset
of turbulence.
Scale (or scale ratio) – Ratio of variable in model to the corresponding variable in its prototype.
Scale Effect – Consequence of non-similarity between model and prototype, resulting from the
fact that not all pertinent dimensionless numbers are the same in model and prototype. In a “real”
model, economics dictates the use of certain materials, e.g., water as a fluid. This means that fluid
density and viscosity are not correctly scaled down from prototype to model and of necessity; some
dimensionless numbers which are not the same in model and prototype, result in scale effect.
Scaling laws – Conditions that must be satisfied to achieve desired similarity between model
and prototype.
Sediment Simulant – Material used in a model to simulate prototype sediment behavior.
Shields’ parameter – See Mobility Number.
Similarity (or similitude) – Correspondence between the behavior of a model and its prototype.
Similitude – See similarity.
Tilting. – Supplying additional slope to an open channel model, e.g., to achieve a model
Reynolds numbers that would be large enough for turbulent flow or cause adequate sediment motion
in the model.
Weber Number = V2lρ/σ – Dimensionless ratio related to the ratio of inertial force to surface
tension force. The quantity is useful in analyzing thin film flows and formation of droplets and bubbles.
11
Froude Number Model (or Gravitational Model) – Model designed to emphasize similarity of
gravitational and inertial forces (Froude Number), while other forces, such as viscous (Reynolds
Number), may not be reproduced accurately. Open channel and coastal models are of this type.
Gravitational Model – See Froude Number Model
Ground Water Model – Model designed to study the movement of ground water (seepage
model)
Harbor Model – Model to determine wave agitation, tidal flushing, etc., in a harbor.
Ice Model – Model in which formation of ice, ice conditions, or ice forces are simulated.
Movable Bed Model – Model in which the bed or side materials, or both, are erodible and are
transported in a manner similar to the prototype.
Navigation Model – Fixed-bed model to study maneuverability of vessels under currents,
waves, wind, etc. (for design of navigable waterways)
Reynolds Number Model (or Viscosity Model) – Model designed to emphasize similarity of
viscous forces (Reynolds Number), while other forces, such as gravity (Froude Number), may not be as
accurately reproduced. Pipe-flow and wind-tunnel models are of this type.
Salinity Model – Densimetric Froude Number model in which salinity distributions are
simulated.
Semimovable Bed Model (Semigrid Model) – Model with only certain portions of the bed or
side materials erodible.
Ship Model – Froude Number model to determine power requirements and maneuverability of
ships (for ship design)
Structure Model – Model to determine flow patterns, forces, and responses to forces at or
near hydraulic structures.
Tidal Model – Froude number model in which tidal water level and current fluctuations are
reproduced.
Tracer Model – Fixed-bed model in which patterns of erosion and deposition are estimated
using tracers of bed material or lightweight sediment simulants.
Upper Region Model – Froude Number model in which similarity of the main body of water is
emphasized while the boundary layer properties may not be accurately reproduced.
Viscosity Model – See Reynolds Number Model
Wave Model – Froude number model in which gravity waves are reproduced to provide the
driving mechanism for currents, impact forces, wave agitation, sediment transport, etc.
13
Markov Chain – Sequential probabilistic process in which the probability of the state of the
variety under analysis is dependent upon its state at the previous steps in sequence. An Nth-order
Markov Chain allows dependence on up to N previous steps.
Monte Carlo Method – Technique of stochastic sampling or selection of random numbers to
generate synthetic data.
Numerical Diffusivity – Second order term introduced as a result of discretization of the
governing differential equations using finite differences, which are of the same form as diffusion
terms.
Numerical Dispersion – Numerical smearing or smoothing of a solution resulting from
numerical diffusivity.
Roundoff Error – Cumulative error introduced by rounding of the results from individual
arithmetic operations because only a finite number of digits can be retained after each operation.
Schematization – Representation of a continuum by discrete elements, e.g., dividing a real
river into reaches with constant parameters.
Scheme (numerical or computational) – Systematic program of action for solving the governing
equations of a mathematical model.
Stability (numerical or computational) – Ability of a scheme to control the propagation or
growth of small perturbations introduced in the calculations. A scheme is unstable if it allows the
growth of error so that it eventually obliterates the true solution.
Truncation Error – The error introduced by replacing the differentials of a differential equation
by finite differences using truncated Taylor series expansions.
14
CHAPTER 2
NUMERICAL ANALYSIS
2.1 INTRODUCTION
Numerical analysis is the area of mathematics and computer science that creates, analyzes,
and implements algorithms for solving continuous mathematical problems numerically. Such problems
originate generally from real-world applications of algebra, geometry and calculus, and they involve
variables which vary continuously; these problems occur throughout the natural sciences, social
sciences, engineering, medicine, and business. During the past half-century, the growth in power and
availability of digital computers has led to an increasing use of realistic mathematical models in science
and engineering, and numerical analysis of increasing sophistication has been needed to solve these
more detailed mathematical models of the world. The formal academic area of numerical analysis
varies from quite theoretical mathematical studies to computer science issues.
With the tremendous surge of importance of using computers to carry out numerical
procedures in solving mathematical models of the scientific world, an area known as scientific
computing or computational science has taken shape during the 1980s and 1990s.
This area looks at the use of numerical analysis from a computer science perspective. It is
concerned with using the most powerful tools of numerical analysis,
computer graphics, symbolic mathematical computations, and graphical user interfaces to make it
easier for a user to set up, solve, and interpret complicated mathematical models of the real world.
Numerical algorithms are almost as old as human civilization. The Rhind Papyrus (1650 BC) of
ancient Egypt describes a root finding method for solving a simple equation. Archimedes of Syracuse
(287- 212 BC) created much new mathematics, including the “method of exhaustion” for calculating
lengths, areas, and volumes of geometric figures. When used as a method to find approximations, it is
in much the spirit of modern numerical integration; and it was an important precursor to the
development of the calculus by Isaac Newton and Gottfried Leibnitz.
A major impetus for developing numerical procedures was the invention of the calculus by
Newton and Leibnitz, as this led to accurate mathematical models for physical reality, first in the
physical sciences and eventually in the other sciences, engineering, medicine, and business. These
mathematical models cannot usually be solved explicitly, and numerical methods to obtain
approximate solutions are needed.
Another important aspect of the development of numerical methods was the creation of
logarithms by Napier (1614) and others, giving a much simpler manner of carrying out the arithmetic
operations of multiplication, division, and exponentiation.
Newton created a number of numerical methods for solving a variety of problems, and his
name is attached today to generalizations of his original ideas. Of special note is his work on root
finding and polynomial interpolation. Following Newton, many mathematics’ giants of the 18th and
19th centuries made major contributions to the numerical solution of mathematical problems.
Foremost among these are Leonhard uler (1707-1783), Joseph-Louis Lagrange (1736-1813), and Karl
Friedrich Gauss (1777-1855).
15
You may remember from algebra that a root of a function is a zero of the function. This means
that at the "root", the function equals zero. As shown in Figure 2.1, the derivative of the function can
be written as
f(x)
f(xi)
f(xi)-0
0 xi+1 xi x
Figure 2.1
x i 1 x i f x i / f ' x i (2.2)
We can find these roots of a simple function such as: f(x) =x3 – 4x +1 simply by setting the
function to zero:
The Newton-Raphson method uses an iterative process to approach one root of a function.
The specific root that the process locates depends on the initial, arbitrarily chosen x-value.
f ( xn )
xn 1 xn
f '( xn ) (2.4)
Here, xn is the current known x-value, f(xn) represents the value of the function at xn, and f'(xn)
is the derivative (slope) at xn. xn+1 represents the next x-value that you are trying to find. Essentially,
f'(x), the derivative represents f(x)/dx (dx = delta-x). Therefore, the term f(x)/f'(x) represents a value of
dx.
16
f ( x) f ( x)
x
f '( x) f ( x) / x (2.5)
The more iteration that are run, the closer dx will be to zero (0). To see how this works, we will
perform the Newton-Raphson method on the function that we investigated earlier, f(x) =x3 – 4x +1.
Below are listed the values that we need to know in order to complete the process.
f′′(x) = 6x (2.7)
A graphical representation can also be very helpful. Below, you see the same function f(x) =x3 –
4x +1 (shown in blue). The process here is the same as above. In the first iteration, the red line is
tangent to the curve at x0. The slope of the tangent is the derivative at the point of tangency, and for
the first iteration is equal to 71. Dividing the value of the function at the initial x (f(5) = 106) by the
slope of the tangent (71), we find that the delta-x is equal to 1,493. Subtracting this from five (5) we
find that the new x-value is equal to 3.507. Another way of considering this is to find the root of this
tangent line. The new x-value (xn+1) will be equal to the root of the tangent to the function at the
current x-value (xn). As seen from the table, the solution is found as 1,861.
17
Figure 2.2 Newton-Raphson Method: First iteration Initial x=5: New x= 3,507
f(x)
=y
Cloud of points
x
Figure 2.3 Scatter diagram
This is usually done by the least squares method, achieving a minimum of the variance
between the smooth curve and the experimental points.
18
This analysis is useful in finding the “best fit” of a set of experimental observations to a mathematical
relationship. If the best straight line is sought, then it is more accurate to use linear regression analysis.
The independent variable is x and this is assumed to be error free. The dependent variable is y and this
is the result of experimental observation and thus contains errors.
The general straight line is:
y= mx + c (2.7)
where m is the slope of the line and c is the point at which the line crosses the y-axis, or known as the
“y-intercept”.
Suppose we have n values of xi and corresponding observation yi
. We use the principle of “Least-square” to find the best straight line to represent the relationship
between y and x.
The principles state that the sum of the squares of the “errors” or “deviations” should be a minimum,
i.e, ∑(yi=y)2 should be a minimum. Now
(2.8)
The errors may be negative or positive so the square of errors are always positive.
y × × × × × ×
× × × × ×
× × × ×
× × × × × ×
× × × × ×
× × × x
(2.9)
(2.10)
Differentiating the right-hand side of equation partially with respect to m and c we obtained:
(2.11)
19
(2.12)
(2.13)
And
(2.14)
(2.15)
In which the summation are carried out from 1 to n . Solving Equations 2.13 and 2.15
(2.16)
(2.17)
+
(2.18)
(2.19)
(2.20)
y=mx+c
c tan=m
The largest power of x denotes the order of the polynomial. There if n=2 in Equation (2.21), we have a
quadratic relationship between y and x;
20
If it is assumed that the intervals x, at which observed values of y are available, are ”exact” then any
errors are confined to the observations y. If any observed values yi is known, the the corresponding
”residual” ∆i is;
The fitting of a polynomial involves choosing values for the coefficients a0>a1....an which together
minimize the sum of the squares of all the residuals as given by equations such an Eq.(2.23). If there
are p observed values of y and if E is the sum of the squares of residuals then,
= ∆i2 (2.24)
The proceeding is to differentiate Eq.(2.24) with respect to the coefficients a0, a1, ... , an is turn
and equate to zero. The resulting equations are called the “normal“ equations and their solution gives
the “best” values of the coefficient in the polynomial of chosen order n. Suppose N=2, then we have a
quadratic relationship in Eq. (2.22) and,
21
*└ ┘= └ ┘ (2.31)
(2.32)
Several computational schemes for numerical integration exist and can be derived from
Newton’s formula. The function error introduced can be computed from the integration of a
polynomial of degree n.
Suppose we have a function
(2.33)
Which can be represented as in Fig. 2.6 and for which we have a sequence of numerical values of y,
h1,h2,h3... at equal intervals along the x-axis.
The area between the curve and the x-axis is obtained approximately by adding the areas of
the trapezoid formed by drawing chords between the ordinate, i.e.
(2.34)
Thus the function (curve) is replaced by straight lines between each successive pair of
ordinates. This is the “trapezoidal” rule.
22
Figure 2.7 Area of parabolic segment in Simpson’s rule approximation
(2.35)
Substituting for
(2.37)
Hence in the general case Fig. 2.7, with n coordinates (n-1, intervals) the total area is
(2.38)
(2.39)
23
2.6 SOLVED PROBLEMS
2.6.1 Calculation the water depth downstream of a spillway
Problem 1: A spillway has a span width of 20 m and the maximum depth of overflow of 10 m, as
shown in the following figure. The discharge per span is calculated as 470 m 3/s. The difference
between the bottom elevations of upstream and downstream of spillway is 55 m. (a) Find the head of
water over spillway assuming the discharge coefficient of the spillway as C = 2,10. (b) Find the water
depth at the downstream of the spillway.
Using the Bernoulli equation to determine flowrate over a dam assumes that the velocity
upstream of the dam is negligible (V1=0) and that the nappe is exposed to atmospheric pressure above
and below. Experiments have shown that the Bernoulli equation alone does not adequately predict
the flow, so empirical constants have been determined which allow better agreement between
equations and real flows. To obtain better accuracy than the Bernoulli equation alone provides, use
our weir calculations.
The main equation used in describing open channel flow is the Bernoulli equation. This equation can
be derived by integrating the expression resulting from the application of Newton's second law to
open channel flow. Common form of Bernoulli's equation, valid at any arbitrary point along a
streamline where gravity is constant, is:
Problem 2: The following equation is given. Please find the roots of this equation. To approximate the
solution of the following equation using the Newton-Raphson method, start the process by taking h=1
Solution :
Xi+1 =
Xi = 0.51
25
Xi+1 =
Xi = 0.28
Xi+1 =
Xi = 0.22
Xi = 0.215
Therefore,
(b) For
(c) For
26
FORTRAN:
LEAST SQUARE CURVE FITTING
DIMENSION X(100),F(100)
READ(5.5) N,(X(I),F(I),I=1,N)
5 FORMAT (I4/(10F 7.0))
SUM1=0.
SUM2=0.
SUM3=0.
SUM4=0.
DO 7 I=1,N
SUM1=SUM1+X(I)
SUM2=SUM2+F(I)
SUM3=SUM3+X(I)××2
SUM4=SUM4+X(I)*F(I)
7 CONTINUE
A=(SUM 4*N-SUM1*SUM2)/(N*SUM3-SUM1××2)
B=(SUM3*SUM2-SUM1*SUM4)/(N*SUM3-SUM1××2)
WRITE(6.8) A,B
8 FORMAT (2F (0.4))
STOP
END
Problem 2: Find the relationship between volume and water level in the reservoir for the given data.
V = a*hb
y = c + b*x
h 5 10 15 20 25 30 35 Σ
V 2250000 8.05*106 17.75*106 30.5*106 46*106 65.7*106 89.5*106 259.75*106
x=logh 0,699 1 1,176 1,301 1,398 1,477 1,544 8,595
y=logV 6,352 6,906 7,249 7,484 7,663 7,818 7,952 51,424
x*y 4,440 6,906 8,526 9,737 10,712 11,547 12,278 64,146
x^2 0,488 1 1,383 1,693 1,954 2,182 2,384 11,085
2
(∑ 𝑥𝑖 ) = 73.878 , ∑ 𝑥𝑖 ∑ 𝑦𝑖 = 441.997,
∑ 𝑥𝑖2 ∑ 𝑦𝑖 = 3799.102
7 ∗ 64.146 − 441.997
𝑏= = 1.892
7 ∗ 11.085 − 73.878
3799.102 − 64.146 ∗ 8.595
𝑐= = 5.023
7 ∗ 11.085 − 73.878
b = 1.892
𝑽 = 𝟏𝟎𝟓𝟓𝟎𝟔. 𝟒𝟖𝟕𝒉𝟏.𝟖𝟗𝟐
X 0 1 2 3 4 5
Y 5 15 26 27 30 35
y = a0 + a1x + a2x2
p = 6, n = 2;
∑x i = 15, ∑xi2=55, ∑xi3=225, ∑xi4=979,
∑yi=138, ∑xiyi=443, ∑xi2yi=1717
From which
28
a0 = 5.61 6a0 + 15a1 + 55a2 = 138
a1 =10.7 15a0 + 55a1+ 225a2 = 443
a2 = -1.02 55a0+ 225a1 + 979a2 =1717
y=5.61+10.7x-1.02x2
6
3
I=a/3 [sum of end ordinates + 4(sum of even ordinates)+2(sum of other odd ordinates)]
x1=3.0 , x2=3.5 x3=4.0 y1=4.5 x5=5 x6=5.5 x7= 6.0
f1(3,0)=5*3*3+8*3+1=70
f2(3,5)=5*3,5*3,5+8*3,5+1=90,5
f3(4,0)=5*4*4+8*4+1=113
f4(4,5)=5*4,5*4,5+8*4,5+1=138,25
f5(5,0)=5*5*5+8*5+1=166
f6(5,5)=5*5,5*5,5+8*5,5+1=196,25
f7(6,0)=5*6*6+8*6+1=229.
I=426,166.
Analytical solution is 426. The difference between analytical and numerical solution is 0,116. The
numerical solution is obtained by hand. If we increase the number ordinates and use a small computer
program, the difference between analytical and numerical solution should be less than 0,166.
Problem 2: Find the emptying time of a reservoir that is required to lower the water level from h1=50
m to h2=40 m. The reservoir volume-elevation relationship is given as V = 8000 h2,7 , where V is the
volume in m3 and H is the water level elevation in m. The outlet works is located at the elevation of h’=
29
10 m, its diameter is 2,5 m and discharge coefficient, c, is 0,63 (assumed constant during in the
variation of reservoir elevation. The inflow through the reservoir is constant as I = Qi =10 m3/s.
𝑑𝑉
= 𝑂 − 𝐼 = 𝑄𝑜 − 𝑄𝑖
𝑑𝑡
in which, V is volume, t is time, O is outflow, and I is inflow
𝑡2 ℎ2
𝑑𝑉
∫ 𝑑𝑡 = ∫
𝑄𝑜 − 𝑄𝑖
𝑡1 ℎ1
𝑉 = 𝑎ℎ𝑏
𝑑𝑉 = 𝑎 ∗ 𝑏 ∗ ℎ𝑏−1 𝑑ℎ
𝑉 = 8000ℎ2,7
𝑑𝑉 = 21600ℎ0,7 𝑑ℎ
𝐷2
𝑄𝑜 = 𝐶𝑜 ∗ 𝜋 ∗ ∗ √2𝑔(ℎ − ℎ′ )
4
2,52
𝑄𝑜 = 0,63 ∗ 𝜋 ∗ ∗ √2𝑔(ℎ − 10)
4
𝑡 ℎ 𝑑𝑉 ℎ 𝑎∗𝑏∗ℎ 𝑏−1
Find 𝑇 = ∫𝑡 2 𝑑𝑡 = ∫ℎ 2 = ∫ℎ 2 2 0.5
1 𝑄 1 𝑐 − 𝑄𝑔 1 𝐶 𝑜 ∗𝜋∗𝑟 ∗(2𝑔∗ℎ) − 𝑄𝑖
𝑡2 50
21600 ∗ ℎ0.7 𝑑ℎ
𝑇 = ∫ 𝑑𝑡 = ∫
𝑡1 40 13.698 ∗ (ℎ − 10)0.5 − 25
To solve this equation we will use Simpson’s formula. The number of ordinates, n, is taken as 5, so we
have 4 intervals.
ℎ2 − ℎ1 10
𝑎= = = 2.5
𝑛 − 1 4
21600 ∗ ℎ0.7
𝑓(ℎ) =
13.698 ∗ (ℎ − 10)0.5 − 25
i hi f(hi)
1 40 5710,7
2 42,5 5614,5
3 45 5536,3
4 47,5 5472,1
5 50 5418,9
30
2.5
𝑇= ( 5710,7 + 5418,9 + 4(5614,5 + 5472,1) + 2(5536,3))
3
T = 55456,9 seconds = 15,4 hours
Month 1 2 3 4 5 6 7 8 9 10 11 12
Discharge
x106m3/day 8 6 5 5 4 1 1 1 5 6 7 7
NUMERICAL INTEGRATION
DIMENSION F(100)
READ(5,5)N,H,(F(I),I=1,N)
5 FORMAT (I4,F7.0)/(10F7.10))
SUM=0
NN=N-1
DO 10 I=1, NN
SUM=SUM+(F(I)+F(I+1))/2*/t
WRITE(6,15) SUM
15 FORMAT (E14.6)
10 CONTINUE
STOP
END
PROGRAM (BASIC)
10 PRINT “AREA UNDER CURVE BY TRAPEZOIDAL AND SIMPSON’S RULES”
20 PRINT “TYPE IN NUMBER OF ORDINATES”
30 INPUT N
40 IF (N-1)/2=INT(N/2) THEN 60
50 PRINT “N MUST BE ODD;THAT NUMBER WAS EVEN”:GO TO 20
60 DIM H(N)
70 PRINT “TYPE IN INTERNAL”
80 INPUT L
90 PRINT “TYPE IN ORDINATES; EACH FOLLOWED BY ‘RETURN’”
100 FOR I=1 TO N: INPUT H(I)
110 FOR I=2 TO N-1 :H(I) =2*H(I): NEXT
120 FOR I= 1 TO N: A=A+H(I):NEXT
31
130 A=A*L/2
140 PRINT: PRINT “ AREA BY TRAPEZOIDAL RULE=”;A
150 A=0
160 FOR I=2 TO N-1 STEP 2:H(I)=2*H(I): NEXT170 FOR I=1 TO N:A=A+H(I): NEXT
180 A=A*L/3
190 PRINT : PRINT “AREA BY SIMPSON’S RULE=”; A
200 END
Problem: Write a m-file to compute the integral of f(x)=sin2(x)+x2 between the boundries [0 ] by
numerical integration for 100 segments using trapezoidal rule and Simpson’s rules .
𝐻
Hint: 𝐼 = (𝑓(1) + 2 ∗ ∑𝑛𝑖=2 𝑓(𝑖) + 𝑓(𝑛 + 1)) n=number of segments, H=interval width
2
a=0;
b=pi;
n=100;
h=(b-a)/n;
x=linspace(a,b,n+1);
f=@(x)sin(x).^2+x.^2;
fpm=feval(f,x);
Itrap = (fpm(1) + 2*sum(fpm(2:n)) + fpm(n+1)) * (h/2)
Isimp = (fpm(1) + 4*sum(fpm(2:2:n)) + 2*sum(fpm(3:2:n-1)) + fpm(n+1)) * (h/3)
32