CE206 - Numerical Differentiation - Integration

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CE 206: Engineering

Computation
p Sessional

Numerical Differentiation and


I t
Integration
ti
diff and gradient command
diff(x)
Returns the difference between adjacent elements
i x. T
in Typically
i ll usedd ffor unequally
ll spaced
d intervals
i t l

fx = gradient(f, h)
Determines the derivative of the data in f at each of
the points. h is the spacing between points; if omitted
h=1.
Uses forward difference for the first point, backward
difference for the last point, and centered difference
for the interior points.
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed
diff and gradient Example
function y=fx(x)
y=0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;

x =[0 0.12 0.22 0.32 0.36 0.4 0.44 0.54 0.64 0.7 0.8];
y=fx(x);

Derivative at unequally spaced intervals:


diff(x) ./diff(y)

Derivative at equally spaced intervals from 0 to 0.8


x =0:0.1:0.8;
0:0.1:0.8;
y=fx(x);
gradient(y,0.1)

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Example: Newton’s law of cooling
The rate of cooling is proportional to the differences in temperature
of the body and the surrounding medium. This can be expressed as:
dT
= −k (T − Ta )
dt
Where, T = temp. of the body, Ta = temp. of the surrounding
medium, k = a p
proportionality
p y constant (p
(per min))

If a metal ball heated to 80°C is dropped into water having Ta =


20°C,, the tempp of the ball changes
g as in

Time (min) 0 5 10 15 20 25
T ((°C)
C) 80 44.5 30 19.1 21.7 20.7

(a) Determine dT/dt using numerical differentiation


(b) Using linear regression, determine k.
k

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Derivatives and Errors
A shortcoming
h t i off numerical
i l diff
differentiation
ti ti iis th
thatt it ttends
d tto
amplify errors in data

How to minimize this effect: fit a smooth


smooth, differentiable
function to the data and take the derivative of the function.
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed
Assignment prob: Thermocline in a lake
0
General knowledge:
5
What is a thermocline? Why is it
important in a lake environment?
10 thermocline
Quest o :
Question:
Depth (m)

How to locate the thermocline?


15

The thermocline is located at the


20
inflexion point of the temperature
depth curve (where d2T/dx2 = 0). It
25
is also the point where dT/dx is
maximum.
30
10 15 20 25
Temperature (deg.C)

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Trapezoidal Rule
Assuming n+1 data points
are evenly spaced, there will
b n intervals
be i t l over which
hi h tto
integrate.

h⎡ n −1

I = ⎢ f ( x0 ) + 2∑ f ( xi ) + f ( xn )⎥
2⎣ i =1 ⎦

For unequal segments


f ( x0 ) + f ( x1 ) f ( x1 ) + f ( x2 ) f ( xn −1 ) + f ( xn )
I = ( x1 − x0 ) + ( x2 − x1 ) + L + ( xn − xn −1 )
2 2 2
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed
Simpson’s 1/3 Rule

Can be used for an odd


number
b off points
i

I=
h
[ f (x0 ) + 4 f (x1 ) + f (x2 )] + h [ f (x2 ) + 4 f (x3 ) + f (x4 )] + L + h [ f (xn−2 ) + 4 f (xn−1 ) + f (xn )]
3 3 3
⎡ n −1 n−2

h⎢
I= f ( x0 ) + 4 ∑ f ( xi ) + 2 ∑ f ( xi ) + f ( xn )⎥
3⎢ i =1 j =2

⎢⎣ i , odd j , even ⎥⎦

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


MATLAB trapz function

Built-in
B il i functions
f i to evaluate
l i
integrals
l bbased
d on trapezoidal
id l
rule
z = trapz(y)
p (y)
z = trapz(x, y)

produces
d the
th integral
i t l off y with
ith respectt to
t x. If x is
i omitted,
itt d th
the
program assumes h=1.

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Ex: Modulus of toughness from σ-ε curve
A rod
d subjected
bj t d tto an axial
i l load
l d yielded
i ld d the
th following
f ll i data
d t
upto rupture
ε σ (kip/in2) 60

0.02 40.0 50
0.05 37.5 40
Rod

ss
Stres
R

0.10 43.0 30
0.15 52.0
20
0.20 60.0
10
0.25 55.0
0
0 0.05 0.1 0.15 0.2 0.25
Strain

The area under the stress-strain >> trapz(e,s)


g
diagram upto
p rupture
p is called the
ans =
modulus of rupture
11.2250
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed
Exercise: Work done by a force
Work done = force × distance
F
F
θ θ

x0 xn

X (ft) F(x), lb Θ (rad)


Calculate the work done 0 0 0.50
from the following discrete 5 9 1 40
1.40
measurements of F(x) and 10 13 0.75
θ(x) 15 14 0.90
20 10.5 1.30
W = ∫ F ( x)dx
25 12 1.48
30 5 1.50

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Exercise: Solving problems in hydrostatics
C l l t the
Calculate th force
f exerted
t d on the
th ddam and
d the
th line
li off action
ti

P = ∫ ρgw( z )(D − z )dz


D
ρ = 1000 kg/m3 0

g = 9.81 m/sec
/ 2 Ans: P = 2.54×10
2 54×109 N
ρgzw( z )(D − z )dz
D

D = 60 m d=
∫ 0
d = 21.97 m

ρgw( z )(D − z )dz


D
w = width ∫ 0

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed


Exercise: Integrals of functions
The total
Th t t l force
f exerted
t d on the
th mastt off a sailboat
ilb t can b
be
expressed as the integral of the following continuous
function:
30 ⎛ z ⎞ − 2 z / 30
F =∫ 200⎜ ⎟e dz
0
⎝5+ z ⎠
Nonlinear integral, difficult to evaluate analytically

Evaluate the integral using trapezoidal rule using step sizes


15, 10, 6, 3, 1, 0.5, 0.25, 0.1 ft

[Ans: 1001.7 lb, 1222.3 lb, 1372.3 lb 1450.8 lb, 1477.1 lb 1479.7 lb, 1480.3 lb
1480 5 lb 1480.6
1480.5 1480 6 lb]

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

You might also like