You are on page 1of 14

Uka, Johannie 5 Feb 2019

MExer03
1. Evaluate the following integral:
𝟒
∫ (𝟏 − 𝒆−𝒙 )𝒅𝒙
𝟎

(a) analytically, (b) single application of the trapezoidal rule, (c) composite
trapezoidal rule with n = 2 and 4, (d) single application of Simpson’s 1∕3 rule,
(e) composite Simpson’s 1∕3 rule with n = 4, (f) Simpson’s 3∕8 rule, and (g)
composite Simpson’s rule, with n = 5. For each of the numerical estimates (b)
through (g), determine the true percent relative error based on (a).

Given:
a = 0; b = 4

Solution:

Let:
I = answer for the integral
E = relative error of I for each methods with respect to the analytical
value

(a) analytically

𝟒
I = ∫𝟎 (𝟏 − 𝒆−𝒙 )𝒅𝒙
= (x + 𝑒 −𝑥 )|0 to 4
= (4 + e-4) – (0 + e0)
= 3.018315639 ans

(b) single application of the trapezoidal rule

n = 1 (1 segment) with 2 points required


h = (b-a)/n = 4

To acquire the points, use this formula:


x = xi + h

I xi f(xi)
0 0 0
1 4 0.981684361

𝑓(𝑥0 )+𝑓(𝑥1 )
I = (𝑏 − 𝑎) 2
0+0.981684361
= (4 − 0) 2
= 1.963368722 ans

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
1.963368722 −3.018315639
= | 3.018315639
| × 100%
= 34.95 % ans

(c) composite trapezoidal rule with n = 2 and 4

@n=2

n = 2; 3 points
h = (b-a)/n = 2

To acquire the points, use this formula:


x = xi + h

I xi f(xi)
0 0 0
1 2 0.8646647168
2 4 0.981684361

𝑓(𝑥0 )+2 ∑𝑛−1


𝑖=1 𝑓(𝑥𝑖 )+𝑓(𝑥𝑛 )
I = (𝑏 − 𝑎)
2𝑛
0 +2(0.8646647168)+0.981684361
= (4 − 0)
2(2)

= 2.711013795 ans

𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒


E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
2.711013795 −3.018315639
= | 3.018315639
| × 100%
= 10.18 % ans

@n=4

n = 4; 5 points
h = (b-a)/n = 1

To acquire the points, use this formula:


x = xi + h

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
i xi f(xi)
0 0 0
1 1 0.6321205588
2 2 0.8646647168
3 3 0.9502129316
4 4 0.981684361
𝑓(𝑥0 )+2 ∑𝑛−1
𝑖=1 𝑓(𝑥𝑖 )+𝑓(𝑥𝑛 )
I = (𝑏 − 𝑎) 2𝑛
𝑓(0)+2{𝑓(1)+𝑓(2)+𝑓(3)}+𝑓(4)
= (𝑏 − 𝑎) 2𝑛
0+2(0.6321205588+0.8646647168+0.9502129316)+0.981684361
= (4 − 0)
2(4)

= 2.937840388 ans

𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒


E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
2.937840388 −3.018315639
= | 3.018315639
| × 100%
= 2.67 % ans

(d) single application of Simpson’s 1∕3 rule

3 points required
n=2
h = (b-a)/n = 2

To acquire the points, use this formula:


x = xi + h

I xi f(xi)
0 0 0
1 2 0.8646647168
2 4 0.981684361

𝑓(𝑥0 )+4𝑓(𝑥1 )+𝑓(𝑥2 )


I = (𝑏 − 𝑎) 6
0+4(0.8646647168)+0.981684361
= (4 − 0) 6

= 2.960228819 ans

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
2.960228819 −3.018315639
= | 3.018315639
| × 100%
= 1.92 % ans

(e) composite Simpson’s 1∕3 rule with n = 4

4 points required
n = 4 (4 segment); h = (b-a)/n = 1

To acquire the points, use this formula:


x = xi + h

I xi f(xi)
0 0 0
1 1 0.6321205588
2 2 0.8646647168
3 3 0.9502129316
4 4 0.981684361

𝑓(𝑥0 )+4 ∑𝑛−1 𝑛−2


𝑖=1,3,5 𝑓(𝑥𝑖 )+2 ∑𝑗=2,4,6 𝑓(𝑥𝑗 )+𝑓(𝑥𝑛 )
I = (𝑏 − 𝑎) [ 3𝑛
]
𝑓(0)+4[𝑓(1)+𝑓(3)]+2𝑓(2)+𝑓(4)
= (𝑏 − 𝑎) [ ]
3𝑛
0+4[0.6321205588+0.9502129316]+2(0.8646647168)+0.981684361
= (4 − 0) [ 3(4)
]
= 3.013449252 ans

𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒


E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
3.013449252 −3.018315639
= | 3.018315639
| × 100%
= 0.16 % ans

(f) Simpson’s 3∕8 rule

n = 3 (3 segments); 4 points required


h = (b-a)/n = 4/3

To acquire the points, use this formula:


x = xi + h

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
i xi f(xi)
0 0 0
1 4/3 0.7364028619
2 8/3 0.9305165488
3 4 0.981684361

𝑓(𝑥0 )+3𝑓(𝑥1 )+3𝑓(𝑥2 )+𝑓(𝑥3 )


I = (𝑏 − 𝑎) 8
0+3(0.7364028619)+3(0.9305165488)+0.981684361
= (4 − 0) 8
= 2.991221297 ans

𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒


E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
2.991221297 −3.018315639
= | 3.018315639
| × 100%
= 0.9 % ans

(g) composite Simpson’s rule, with n = 5

n = 5 (5 segment); 6 points required


h = (b-a)/n = 4/5

To acquire the points, use this formula:


x = xi + h

I xi f(xi)
0 0 0
1 4/5 0.5506710359
2 8/5 0.798103482
3 12/5 0.9092820467
4 16/5 0.959237796
5 4 0.981684361

ℎ 3ℎ
I = [𝑓(𝑥0 ) + 4𝑓(𝑥1 ) + 𝑓(𝑥2 )] + [𝑓(𝑥2 ) + 3(𝑓(𝑥3 ) + 𝑓(𝑥4 )) + 𝑓(𝑥5 )]
3 8
4 3(4)
=3(5) [0 + 4(0.5506710359) + 0.798103482] + 8(5)
[0.798103482 +
3(0.9092820467 + 0.959237796) + 0.981684361]
= 3.015814245 ans

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
|× 100%
3.015814245 −3.018315639
= | 3.018315639
| × 100%
= 0.08 % ans

Summary Discussion:
In this item, the integral was calculated using seven methods.
First is the analytical method (the one that we have learned during
our Integral Calculus class) which gives the result 3.018315639 and is to
be considered as the analytical or real value of the given integral.
Second is single application of trapezoidal rule which has 1 segment
and requires 2 points and the points would be acquired using x = x i + h
and this give the result of 1.963368722 which has an error of 34.95%
relative to the analytical value.
Third is the composite trapezoidal rule given the segment, n of 2
and 4. For n = 2, it requires 3 points and the points would be acquired
using x = xi + h. It gives the result of 2.711013795 with an error of 10.18%
relative to the analytical value. For n = 4, this requires 5 points and the
points would be acquired using x = xi + h. It gives the result of
2.937840388 with an error of 2.67% relative to the analytical value.
Fourth is the single application of Simpson’s 1/3 rule. It requires 3
points and it has 2 segments. To acquire the points, use x = xi + h. It gives
the result of 2.960228819 with an error of 1.92% relative to the analytical
value.
Fifth is the composite Simpson’s 1/3 rule with n = 4. It requires 5
points and the points would be acquired using x = xi + h. It gives the result
of 3.013449252 with an error of 0.16% relative to the analytical value.
Sixth is the Simpson’s 3/8 rule. It has n = 3 and 4 points required,
and the points would be acquired using x = xi + h. It gives the result of
2.991221297 with an error of 0.9% relative to the analytical value.
Seventh is the composite Simpson’s rule with n = 5. It requires 6
points and the points would be acquired using x = xi + h. It gives the result
of 3.015814245 with an error of 0.08% relative to the analytical value.
In conclusion, out of all the methods used, the result from the
composite Simpson’s rule with n=5 presented the value nearest to the
analytical value with only very little relative error.

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
2. The function f(x) = e−x can be used to generate the following table of unequally
spaced data:

I xi f(xi)
0 0 1
1 0.1 0.9048
2 0.3 0.7408
3 0.5 0.6065
4 0.7 0.4966
5 0.95 0.3867
6 1.2 0.3012

Evaluate the integral from a = 0 to b = 1.2 using (a) analytical means, (b) the
trapezoidal rule, and (c) a combination of the trapezoidal and Simpson’s rules
wherever possible to attain the highest accuracy. For (b) and (c), compute the
true percent relative error.

In this case, all the given points could be used in solving the integral.
a = 0, b = 1.2
Let
I = the answer for the integral
E = relative error of I for each methods with respect to the
analytical value

Solution:

(a) analytical means

𝟏.𝟐
I = ∫𝟎 (𝒆−𝒙 )𝒅𝒙
= ( -𝑒 −𝑥 )|0 to 1.2
= (-e-1.2) – (-e0)
= 0.6988057881 ans

(b) the trapezoidal rule

n = 6; h = (1.2-0)/n = 0.2

All the given points from the table will be used to solve the integral.


I = 2 [𝑓(𝑥0 ) + 𝑓(𝑥6 ) + 2(𝑓(𝑥1 ) + 𝑓(𝑥2 ) + 𝑓(𝑥3 ) + 𝑓(𝑥4 ) + 𝑓(𝑥5 ))]
0.2
= [1 + 0.3012 + 2(0.9048 + 0.7408 + 0.6065 + 0.4966 + 0.3867)]
2
= 0.7011335893 ans

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
| × 100%
0.7011335893−0.6988057881
= | 0.6988057881
| × 100%

= 0.33 % ans

(c) a combination of the trapezoidal and Simpson’s rules wherever possible to


attain the highest accuracy

Using simpson’s 1/3 rule:


n = 6; a = 0, b = 1.2
h = (1.2-0)/6 = 0.2

All the given points from the table will be used to solve the integral.


I = 3 [𝑓(𝑥0 ) + 𝑓(𝑥6 ) + 4(𝑓(𝑥1 ) + 𝑓(𝑥3 ) + 𝑓(𝑥5 )) + 2(𝑓(𝑥2 ) + 𝑓(𝑥4 ))]
0.2
= [1 + 0.3012 + 4(0.9048 + 0.6065 + 0.3867) + 2(0.7408 + 0.4966)]
3
= 0.6988119702 ans

𝐴𝑐𝑞𝑢𝑖𝑟𝑒𝑑 𝑣𝑎𝑙𝑢𝑒 −𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒


E=| 𝐴𝑛𝑎𝑙𝑦𝑡𝑖𝑐𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
| × 100%
0.6988119702−0.6988057881
= | 0.6988057881
| × 100%

= 0.0008847 % ans

Therefore, as we can see, Simpson’s method gives the higher accuracy than
trapezoidal result.

3. Water exerts pressure on the upstream face of a dam as shown in Fig. P19.9.
The pressure can be characterized by
p(z) = ρg(D − z)

where p(z) = pressure in pascals (or N/m2) exerted at an elevation z meters


above the reservoir bottom; ρ = density of water, which for this problem is
assumed to be a constant 103 kg/m3; g = acceleration due to gravity (9.81
m/s2); and D = elevation (in m) of the water surface above the reservoir
bottom. According to Eq. (P19.9), pressure increases linearly with depth, as
depicted in Fig. P19.9a. Omitting atmospheric pressure (because it works
against both sides of the dam face and essentially cancels out), the total force ft
can be determined by multiplying pressure times the area of the dam face (as
shown in Fig. P19.9b). Because both pressure and area vary with elevation, the
total force is obtained by evaluating
𝑫
𝒇𝒕 = ∫ 𝝆𝒈𝒘(𝒛)(𝑫 − 𝒛) 𝒅𝒛
𝟎

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
where 𝑤(z) = width of the dam face (m) at elevation z (Fig. P19.9b). The line of
action can also be obtained by evaluating
𝑫
∫𝟎 𝝆𝒈𝒛𝒘(𝒛)(𝑫 − 𝒛) 𝒅𝒛
𝒅= 𝑫
∫𝟎 𝝆𝒈𝒘(𝒛)(𝑫 − 𝒛) 𝒅𝒛

Use Simpson’s rule to compute ft and d.

Solution:

From the given figure, there were 4 points to be considered as z which were
0,25,50, and 75. The table below presents the values considering the given
formulas above.

Z 𝒘(𝒛) D-z 𝒘(𝒛)(𝑫 z(𝒘(𝒛)(𝑫 −


− 𝒛) 𝒛))
0 0 122 75 9150 0
1 25 135 50 6750 168750
2 50 175 25 4325 218750
3 75 200 0 0 0

h = (b-a)/n = (75-0)/25 = 3

General formula:


I = 3 [𝑓(𝑥0 ) + 𝑓(𝑥4 ) + 4(𝑓(𝑥1 ) + 𝑓(𝑥3 ) + ⋯ ) + 2(𝑓(𝑥2 ) + 𝑓(𝑥4 ) + ⋯ )]

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
For this problem:
𝐷
∫0 𝜌𝑔𝑧𝑤(𝑧)(𝐷 − 𝑧) 𝑑𝑧

= 𝜌𝑔 3 [𝑓(𝑥0 ) + 4𝑓(𝑥1 ) + 2(𝑓(𝑥2 ))]
3
= (1000)(9.81)∗ 3 [0 + 4(168750) + 2(218750)]
= 10913625000 N-m
= 10913625 kN-m

To solve for the total force:

𝐷
𝑓𝑡 = ∫ 𝜌𝑔𝑤(𝑧)(𝐷 − 𝑧) 𝑑𝑧
0

= 𝜌𝑔 3 [𝑓(𝑥0 ) + 4𝑓(𝑥1 ) + 2(𝑓(𝑥2 ))]
3
= (1000)(9.81) ∗ 3 [9150 + 4(6750) + 2(4325)]
= 439488000 N
= 439488 kN ans

To solve the line of action:

𝐷
∫0 𝜌𝑔𝑧𝑤(𝑧)(𝐷 − 𝑧) 𝑑𝑧
d= 𝐷
∫0 𝜌𝑔𝑤(𝑧)(𝐷 − 𝑧) 𝑑𝑧
10913625 𝑘𝑁−𝑚
=
439488 𝑘𝑁
= 24.83 m ans

4. The force on a sailboat mast can be represented by the following function:


𝒛 𝟐𝒛
𝒇(𝒛) = 𝟐𝟎𝟎 ( ) 𝒆− 𝑯
𝟓+𝒛

where z = the elevation above the deck and H = the height of the mast. The total
force F exerted on the mast can be determined by integrating this function over
the height of the mast:
𝑯
𝑭 = ∫ 𝒇(𝒛)𝒅𝒛
𝟎

The line of action can also be determined by integration:


𝐻
∫0 𝑧𝑓(𝑧)
𝑑= 𝐻
∫0 𝑓(𝑧)

(a) Use the composite trapezoidal rule to compute F and d for the case where H
= 30 (n = 6). (b) Repeat (a), but use the composite Simpson’s 1∕3 rule.

Solutions:

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03

(a) Use the composite trapezoidal rule to compute F and d for the case where H
= 30 (n = 6).

With the use of the software MatLab, the following is the code used for
trapezoidal:
function traprule = trapezoidalrule(y,n,a,b)

format long
% y = f(x)
% a = min; b = max

if n == 1;
hh = (b-a);
%diff of x when n = 1

mm = y(a);
%when x = a

lpp = y(b);
%when x = b

traprule = (hh./2).*(mm+lpp);

else
hh = (b-a)./n;
%change of x

h = zeros(n,1);
%vectors of 0 (for loop storing)

z = a:hh:b;
% the intervals

mm = y(z(1));
%initial

for i = 2:n
h(i,:)=2.*y(z(i));
end

lpp=y(z(n+1));%f(xn)
traprule=(hh./2).*(mm+sum(h)+lpp);

end
end

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
To acquire the answer for this problem, the following are the commands that
are used:

>> y = @(x)200.*(x./(5+x)).*exp((-2.*x)./30)

y = function_handle with value:

@(x)200.*(x./(5+x)).*exp((-2.*x)./30)

>> trapezoidalrule(y,6,0,30)

ans = 1.402728197383921e+03

>> F = ans

F = 1.402728197383921e+03

>> y = @(x)(200.*(x./(5+x)).*exp(-2.*x./30)).*x

y = function_handle with value:

@(x)(200.*(x./(5+x)).*exp(-2.*x./30)).*x

>> trapezoidalrule(y,6,0,30)

ans = 1.924524045564966e+04

>> ans./F

ans = 13.719864255628359

>> d = ans

d = 13.719864255628359

In conclusion to this problem, the matlab program gives the answers


F = 1.402728197383921e+03 N, and d = 13.719864255628359 m.

(b) Repeat (a), but use the composite Simpson’s 1∕3 rule.

With the use of the matlab software, the following is the code used to solve for
the answer using simpson’s rule:
function simpsr = SimpsonsRule(y,n,a,b)
% y = f(x)

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
% a = min; b = max

format long

hh = (b-a)./n;
% change of x

h = zeros(n,1);
% vectors of 0, loop storing

q = zeros(n,1);
% vectors of 0, storing values

zzz = a:hh:b;
%intervals

mmmm = y(a);
%initial

for i = 2:2:n %odd(f(x1), f(x3), ...)


h(i,:)=4.*y(zzz(i));
end

for k = 3:2:n %even (f(x2), f(x4), ...)


q(k,:)=2.*y(zzz(k));
end

lp=y(b);
%f(xn)

simpsr=(hh./3).*(mmmm+sum(h)+sum(q)+lp);

end

To acquire the answer for this problem, the following are the commands used:
>> y = @(x) 200.*(x./(5+x)).*exp((-2.*x)./30)

y = function_handle with value: @(x)200.*(x./(5+x)).*exp((-2.*x)./30)

>> SimpsonsRule(y,6,0,30)

ans = 1.462866512831577e+03

>> F = ans

CpE 531 : Database Management System


Uka, Johannie 5 Feb 2019
MExer03
F = 1.462866512831577e+03

>> y = @(x) (200.*(x./(5+x)).*exp(-2.*x./30)).*x

y = function_handle with value: @(x)(200.*(x./(5+x)).*exp(-


2.*x./30)).*x

>> SimpsonsRule(y,6,0,30)

ans = 1.940674722514138e+04

>> ans./F

ans = 13.266246137234338

>> d = ans

d = 13.266246137234338

In conclusion to this problem, Simpson’s rule gives the answers


F = 1.462866512831577e+03 N, and d = 13.266246137234338 m.

CpE 531 : Database Management System

You might also like