acsl, postech1 matlab 입문 chapter 8 numerical calculus and differential equations
TRANSCRIPT
ACSL, POSTECH 1
MATLAB 입문
CHAPTER 8 Numerical calculus and differential equations
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
2
Review of integration and differentiation
Engineering applications
Acceleration and velocity:
Velocity and distance:
Capacitor voltage and current:
Work expended:
b
adxxfA )(
A
a bx
f(x)
b
aaxtvbx )()()(
b
vdttabv0
)0()()(
b
aaQdtti
Cbv )()(
1)(
d
d
kxdxW
dxxfW
0
0)(
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
3
Integrals
Properties of integrals
Definite integrals
Indefinite integrals
b
a
b
a
b
adxxgddxxfcdxxdgxcf )()()]()([
b
a
c
a
b
cdxxfdxxfdxxf )()()(
111
111
n
a
n
b
ax
bx
n
xdxx
nnnb
a
n
abax
bxxdx
x
b
alnlnln
1
2]cos2[cos2
cossin2
x
xxxdx
xxdx sincos
1n
This week's content handles definite
integrals only
the answers are always numeric
see chapter 9
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
4
Improper integrals and singularities
Improper integrals
singularity
|1|ln1
1
xdxx
001lnlim
1
1lim h
hxdx
x
|)1|ln|1|lim(ln h
)1ln(lim h
h 1- h 1-
h 1-
h 1-
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
5
Derivatives
dxxfxg )()(dx
dgxf )(
dx
dfxg
dx
dgxf
dx
dh)()( )()()( xgxfxh
)(/)()( xgxfxh 2
)()(
gdxdg
xfdxdf
xg
dx
dh
)(yfz )(xgy dx
dy
dy
dz
dx
dz
xdx
xd
xdx
xdxdx
xd
nxdx
dx nn
sincos
cossin
1ln
1
xxxxdx
xxdsin2cos
)sin( 22
xxdx
dyy
dx
xdcossin22
)(sin2
Integration differentiation
example
: product rule
: quotient rule
: chain rule
1) 2)
3)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
6
Numerical integrationRectangular integration trapezoidal integration
Numerical integration functions
Command Description
quad (‘function’,a,b,tol) Uses an adaptive Simpson’s rule to compute the integral of the function ‘function’ with a as the lower integration limit and b as the upper limit. The parameter tol is optional. Tol indicates the specified error tolerance.
quadl (‘function’, a,b,tol) Uses Lobatto quadrature to compute the integral of the function ‘function’. The rest of the syntax is identical to quad.
trapz (x,y) Uses trapezoidal integration to compute the integral of y with respect to x, where the array y contains the function values at the points contained in the array x.
y
xa b
y=f(x) y=f(x)
y
xa b
··· ···
··· ···
0sin dx
2cos0coscossin00
xdx (exact
solution)
(use of the trapz function)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
7
Rectangular, Trapezoidal, Simpson Rules
Rectangular rule Simplest, intuitive wi = 1 Error=O(h) Mostly not enough!
Trapezoidal rule Two point formula, h=b-a Linear interpolating polynomial
Simpson’s Rule Three point formula, h=(b-a)/2 Quadratic interpolating polynomial Lucky cancellation due to right-left symmetry
Significantly superior to Trapezoidal rule Problem for large intervals -> Use the extended formula
)(2/)( 310 hOffhI
x0 =a xN=b
f(x)
x1 x2
f(x1)f(x2)
1
0
N
iifhI
x0 =a x1=b
f(x) f0 f1
)()3
1
3
4
3
1( 5
210 hOfffhI
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Matlab's Numerical Integration Functions trapz(x,y) When you have a vector of data
trapezoidal integration method not as accurate as Simpson's method very simple to use
quad('fun',a,b,tol) When you have a function adaptive Simpson’s rule integral of ’fun’ from a to b tol is the desired error tolerance and is optional
quad1('fun',a,b,tol) Alternative to quad adaptive Lobatto integration integral of ’fun’ from a to b tol is the desired error tolerance and is optional 8
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
9
Comparing the 3 integration methods
The answer is A1=A2=0.6667, A3=0.6665
test case:
Trapezoid Method:
x=linspace(0,pi, 10);
y=sin(x);
trapz(x,y);
2cos0coscossin00
xdx
(exact solution)
(
Simpson's Method:
quad('sin',0,pi);
Lobatto's Method
quadl('sin',0,pi);
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Integration near a Singularity
Trapezoid:
x=[0:0.01:1];
y=sqrt(x);
A1=trapz(x,y);
Simpson:
A2=quad('sqrt',0,1);
Lobatto:
A3=quadl('sqrt',0,1);
10
xdxdy
xy
/5.0/
(The slope has a singularity at x=0)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Using quad( ) on homemade functions
11
1) Create a function file:
function c2 = cossq(x)
% cosine squared function.
c2 = cos(x.^2);
Note that we must use array exponentiation.
2) The quad function is called as follows:
quad('cossq',0,sqrt(2*pi))
3) The result is 0.6119.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 1 and 5 -- Integrate
Hint: position is the integral of velocity*dt Hint: Work is the integral of force*dx
12
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 11
First find V(h=4) which is the Volume of the full cup Hint: Flow = dV/dt, so the integral of flow (dV/dt) from 0 to t = V(t) Set up the integral using trapezoid (for part a) and Simpson (for b) Hint: see next slide for how to make a vector of integrals with changing b values So you can calculate vector V(t), i.e. volume over time for a given flow rate
V = quad(....., 0, t) where t is a vector of times and then plot and determine graphically when V crosses full cup 13
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
To obtain a vector of integration results:
For example, sin(x) is the integral of the cosine(z) from z=0 to z=x
In matlab we can prove this by calculating the quad integral in a loop:for k=1:101
x(k)= (k-1)* pi/100; % x vector will go from 0 to pi
sine(k)=quad('cos',0,x(k)); % this calculates the integral from 0 to x
end
plot(x, sine) % this shows the first half of a sine wave
% calculated by integrating the cosine
14
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 14
Hint: The quad function can take a vector of values for the endpoint Set up a function file for current Set up a vector for the time range from 0 to .3 seconds Then find v = quad('current',0,t) will give a vector result and plot v
15
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
16
Numerical differentiation
x
yy
xx
yymB
23
23
23
x1 x2 x3
y1
y2
y3
Δx Δx
y=f(x)
True slope
A
B
C
x
yy
x
yy
x
yymmm
BA
C
22
1
2
132312
x
yy
xx
yymA
12
12
12
x
y
dx
dy
limΔx 0
: backward difference
: central difference
: forward difference
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
17
The diff function
The diff Function
d= diff (x)
Backward difference & central difference method
example
example x=[5, 7, 12, -20]; d= diff(x)
d = 2 5 -32
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Try that: Compare Backward vs Central
Construct function with noise
x=[0:pi/50:pi];
n=length(x);
% y=sin(x) +/- .025 random error
y=sin(x) + .05*(rand(1,51)-0.5);
td=cos(x); % true derivative
Backward difference using diff:
d1= diff(y)./diff(x);
subplot(2,1,1)
plot(x(2:n),td(2:n),x(2:n),d1,'o');
xlabel('x'); ylabel('Derivative');
axis([0 pi -2 2])
title('Backward Difference Estimate')
Central Difference
d2=(y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2));
subplot(2,1,2)
plot(x(2:n-1),td(2:n-1),x(2:n-1),d2,'o');
xlabel('x'); ylabel('Derivative');
axis([0 pi -2 2])
title('Central Difference Estimate');
18
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 7: Derivative problem
Hint: velocity is the derivative of height
19
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 17
20
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
21
Polynomial derivatives -- trivia
b = polyder (p)
p = [a1,a2,…,an]
b = [b1,b2,…,bn-1] b = polyder (p1,p2) [num, den] = polyder(p2,p1)
nnnnn axaxaxaxaxf
12
31
21)(
12
21
1 )1( nnn axanxna
dx
df
12
21
1 nnn bxbxb
Example p1= 5x +2
p2=10x2+4x-3
Result: der2 = [10, 4, -3]
prod = [150, 80, -7]
num = [50, 40, 23]
den = [25, 20, 4]
Command Description
d=diff(x) Returns a vector d containing the differences between adjacent elements in the vector x.
b=polyder(p) Returns a vector b containing the coefficients of the derivative of the polynomial represented by the vector p.
b=polyder(p1,p2) Returns a vector b containing the coefficients of the polynomial that is the derivative of the product of the polynomials represented by p1 and p2. cf. equivalent comment: b=polyder(conv(p1,p2))
[num, den]= polyder(p2,p1)
Returns the vector num and den containing the coefficients of the numerator and denominator polynomials of the derivative of the quotient p2/p1, where p1 and p2 are polynomials.
Numerical differentiation functions
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
22
Analytical solutions to differential equations(1/6)
Solution by Direct Integration Ordinary differential equation (ODE)
example
Partial differential equation (PDE) -- not covered
2tdt
dy
303
33
0 0
2 tttdttdt
dt
dyt t
3)0()(
3tyty
2
2
)(
)(
dt
ydty
dt
dyty
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
23
Analytical solutions to differential equations(2/6)
Oscillatory forcing function
A second-order equation
)(tfdt
dy Forcing function example wttf sin)(
w
wtt
w
wtwtdtdt
dt
dy tt cos1
0
cossin
00
w
wtyty
tty
cos1)0()(
0)(
w
wtyty
cos1)0()(
32
2
tdt
yd
4)0(
4
0
3
0 2
2 tdtty
dt
dydt
dt
yd tt
)0(4
4
yt
dt
dy
)0(20
)0(4
)0()(5
0
4
0yt
tdty
tytydt
dt
dy tt
)0()0(20/)( 5 yyttty
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
24
Analytical solutions to differential equations(3/6)
Substitution method for first-order equations
)(tfydt
dy stAety )( stsAedtdy / 0)( tf
0 stst AesAeydt
dy
Characteristic equation
/1s
AAey 0)0(
/)0()( teyty
Characteristic root
( ),
01s
to find A
Solution, Free response
∵The solution y(t) decays with time if τ>0
τ is called the time constant.
BAety st )(
AyB )0(
AyAety st )0()(
01s MyA )0(
)1()0()( // tt eMeyty
0)( tf 0tsuppose
for
M at t=0 Such a function is the step
function
( ),
Forced response
Natural (by Internal Energy) v.s. Forced Response (by External Energy)
Transient (Dynamics-dependent) v.s. Steady-state Response (External Energy-dependent)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 18 – solve on paper then plot(next week we solve with Matlab)
Use the method in previous slide
25
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 21 – solve on paper then plot(next week we solve with Matlab)
Re-arrange terms:
mv' + cv = f , OR
(m/c) v' + v = f/c
now it's in the same form as 2 slides back
26
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
27
Analytical solutions to differential equations(4/6)
Nonlinear equations
Substitution method for second order equations(1/3)
example
05 yyyy
0sin yy
0 yy
02 ycy stAety )(
0)( 22 stAecs
( )
cs ctct eAeAty 21)( general solution
suppose that 4)0(,6)0(,2 yyc
216)0( AAy
422)0( 21 AAy2,4 21 AA solution
1)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
28
Analytical solutions to differential equations(5/6)
Substitution method for second order equations(2/3)
02 yy stAety )(
tite ti sincos
titi eAeAty 21)(
tBtBty cossin)( 21
)0(,/)0( 21 yByB
tyty
ty
cos)0(sin)0(
)(
2
P Pf /1
)(tfkyycym stAetytf )(,0)(
tsts eAeAty 2121)(
tsetAAty 1)()( 21
teBteBty atat cossin)( 21
( )
general solution
Euler’s identities
period frequency
2)
3) ( )
0)( 2 stAekcsms
1. Real and distinct: s1 and s2.
2. Real and equal: s1.
3. Complex conjugates: ias
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
29
Analytical solutions to differential equations(6/6)
Substitution method for second order equations(3/3)
1. real, distinct roots: m=1,c=8, k=15.characteristic roots s=-3,-5.
tt eAeAty 52
31)(
2. complex roots: m=1,c=10,k=601
characteristic roots is 245
teBteBty tt 24cos24sin)( 52
51
12/24/2 P
3. unstable case, complex roots: m=1,c=-4,k=20 is 42 characteristic roots
teBteBty tt 4cos4sin)( 22
21
2/4/2 P
4. unstable case, real roots: m=1,c=3,k=-10characteristic roots s=2,-5.
tt eAeAty 52
21)(
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Problem 22
Just find the roots to the characteristic equation and determine which
form the free response will take (either 1, 2, 3 or 4 in previous slide)
This alone can be a helpful check on matlab's solutions—if you don't get the right form, you can suspect there is an error somewhere in your solution
30
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Next Week:
we learn how to solve ODE's with Matlab
31