Ch08a - Faculty

advertisement
MATLAB 입문
CHAPTER 8
Numerical calculus and differential equations
ACSL, POSTECH
1
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Review of integration and differentiation

Engineering applications
f(x)

A
Acceleration and velocity:
b
v(b)   a(t )dt  v(0)
0

Velocity and distance:
b
x(b)   v(t )  x(a)
a
a
b
x

Capacitor voltage and current:
v(b) 
b
A   f ( x)dx
a

1 b
i (t )dt  Q (a )

C  a
Work expended:
d
W   f ( x)dx
0
d
W   kxdx
0
2
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Integrals

Properties of integrals
b
b
a
a
 [cf ( x)  dg ( x)]dx  c

b
a

a
c
b
a
c
f ( x)dx   f ( x)dx   f ( x)dx
Definite integrals

b
x n dx 
a

b
a

x n 1 x b b n 1 a n 1


n  1 x a n  1 n  1
n  1
x b
1
dx  ln x
 ln b  ln a
x a
x
2

b
f ( x)dx  d  g ( x)dx
sin xdx   cos x
x  2
 [cos 2  cos  ]  2
x 
This week's content
handles definite
integrals only
the answers are
always numeric
Indefinite integrals
 cos xdx  sin x
see chapter 9
3
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Improper integrals and singularities

Improper integrals
1
 x  1dx  ln | x  1 |

1-
lim
h
h
0
1
dx  lim ln x  1 h0
x 1
h 1 lim(ln | h  1 |  ln | 1 |)
h
1-
 lim ln( 1  h)  
h

1-
singularity
4
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Derivatives

Integration
g ( x)   f ( x)dx
differentiation
f ( x) 
dg
dx
h( x )  f ( x ) / g ( x )
dh
dg
df
 f ( x)
 g ( x)
dx
dx
dx
df
dg
g ( x)  f ( x)
dh
dx
dx

2
dx
g
z  f ( y ) y  g (x)
dz dz dy

dx dy dx
h( x )  f ( x ) g ( x )
:
product rule
:
quotient rule
:
chain rule
example
dx n
 nx n 1
1)
dx
d ln x 1

dx
x
d sin x
 cos x
dx
d cos x
  sin x
dx
2)
d ( x 2 sin x)
 x 2 cos x  2 x sin x
dx
3)
d (sin 2 x)
dy
 2y
 2 sin x cos x
dx
dx
5
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Numerical integration
Rectangular integration
trapezoidal integration
y
y


0

y=f(x)
···
0
y=f(x)
sin dx   cos x 0  cos 0  cos   2 (exact solution)
···
···
a

sin dx
x
b
a
···
x
(use of the trapz function)
b
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.
6
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Rectangular, Trapezoidal, Simpson Rules

Rectangular rule

N 1
I  h f i
f(x2)
f(x1)
f(x)
Simplest, intuitive
i 0
 wi = 1
 Error=O(h)
Mostly not enough!

x0 =a x1 x2
Trapezoidal rule

Two point formula, h=b-a
 Linear interpolating polynomial
I  h( f 0  f1 ) / 2  O(h 3 )

f0
f(x)
xN=b
f1
Simpson’s Rule

Three point formula, h=(b-a)/2
 Quadratic interpolating polynomial
 Lucky cancellation due to right-left symmetry
x0 =a

1
4
1
I  h( f 0  f1  f 2 )  O(h 5 )
3
3
3
Significantly superior to Trapezoidal rule

Problem for large intervals -> Use the extended formula
x1=b
7
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Matlab's Numerical Integration Functions

trapz(x,y)




trapezoidal integration method
not as accurate as Simpson's method
very simple to use
quad('fun',a,b,tol)




When you have a vector of data
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)



When you have a function
Alternative to quad
adaptive Lobatto integration
integral of ’fun’ from a to b
tol is the desired error tolerance and is optional
8
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Comparing the 3 integration methods

test case:

0
sin dx   cos x 0  cos 0  cos   2
(exact solution)
Trapezoid Method:
x=linspace(0,pi, 10);
y=sin(x);
trapz(x,y);
Simpson's Method:
quad('sin',0,pi);
(
Lobatto's Method
quadl('sin',0,pi);
The answer is
A1=A2=0.6667,
A3=0.6665
9
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Integration near a Singularity
Trapezoid:
x=[0:0.01:1];
y=sqrt(x);
A1=trapz(x,y);
y x
dy / dx  0.5 / x
Simpson:
A2=quad('sqrt',0,1);
(The slope has a singularity at x=0)
Lobatto:
A3=quadl('sqrt',0,1);
10
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Using quad( ) on homemade functions
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.
11
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Problem 1 and 5 -- Integrate


Hint: position is the integral of velocity*dt
Hint: Work is the integral of force*dx
12
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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;
sine(k)=quad('cos',0,x(k));
end
plot(x, sine)
% x vector will go from 0 to pi
% this calculates the integral from 0 to x
% this shows the first half of a sine wave
% calculated by integrating the cosine
14
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Numerical differentiation
dy
y
 lim
dx Δx 0 x
True slope
y3
y=f(x)
y2
B
A
y1
Δx
x1
Δx
x2
y 2  y1 y 2  y1

x 2  x1
x
y3  y2 y3  y2
mB 

x3  x 2
x
mA 
C
x3
mC 
: backward difference
: forward difference
mA  mB 1  y 2  y1 y 3  y 2  y 3  y1
 


: central difference
2
2  x
x 
2x
16
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
The diff function


The diff Function
d= diff (x)
example
x=[5, 7, 12, -20];
d= diff(x)
d=2
5
-32
Backward difference & central
difference method
example
17
Acsl, Postech
MATLAB 입문 : 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
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Problem 7: Derivative problem

Hint: velocity is the derivative of height
19
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Problem 17
20
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Polynomial derivatives -- trivia
f ( x)  a1x n  a 2 x n1  a3 x n2    an  1x  an
Example p1= 5x +2
df
 na1 x n 1  (n  1)a 2 x n  2    an  1
dx
p2=10x2+4x-3
 b1 x n 1  b 2 x n 2    bn  1



b = polyder (p)
p = [a1,a2,…,an]
b = [b1,b2,…,bn-1]
b = polyder (p1,p2)
[num, den] = polyder(p2,p1)
Result:
der2 = [10, 4, -3]
prod = [150, 80, -7]
num = [50, 40, 23]
den = [25, 20, 4]
Numerical differentiation functions
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.
21
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Analytical solutions to differential equations(1/6)

Solution by Direct Integration

Ordinary differential equation (ODE)
example
dy 2
t
dt
t
dy
t3 t t3
2
dt

t
dt


0 dt 0
3 0 3
t
y (t )  y (0) 
t3
3
dy
dt
d2y
y(t )  2
dt
y (t ) 

Partial differential equation (PDE) -- not covered
22
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Analytical solutions to differential equations(2/6)

Oscillatory forcing function
dy
 f (t )
dt
Forcing function
example
f (t )  sin wt
t
dy
cos wt t 1  cos wt
dt   sin wtdt  

0 dt
0
w 0
w
t
1  cos wt
y (t )  y (t )  y (0) 
0
w
1  cos wt
y (t )  y (0) 
w


A second-order equation
t
d2y 3
t
dt 2
t
d2y
dy
t4
3
0 dt 2 dt  dt  y (0)  0 t dt  4
t
dy t 4
  y (0)
dt 4
4
t t

dy
t5

dt

y
(
t
)

y
(
0
)


y
(
0
)
dt

 ty (0)

0 dt
0  4
20

t
y(t )  t 5 / 20  ty (0)  y(0)
23
Acsl, Postech
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Analytical solutions to differential equations(3/6)

Substitution method for first-order
equations

dy
 y  f (t ) ( y(t )  Aest , dy / dt  sAe st )
dt
f (t )  0
dy

 y  sAe st  Ae st  0
dt
s  1  0
s  1/ 
y(0)  Ae0  A
y(t )  y(0)e t /
Characteristic equation
Characteristic root
to find A
suppose
f (t )  0
t 0
for
M at
t=0
Such a function is the step function
y (t )  Ae st  B
B  y (0)  A
y(t )  Aest  y(0)  A
( s  1  0 ,
A  y (0)  M )
y(t )  y(0)e t /  M (1  e t / )
Forced response
Solution, Free response
∵The solution y(t) decays with time if τ>0
τ is called the time constant.
Natural (by Internal Energy) v.s. Forced Response (by External Energy)
Transient (Dynamics-dependent) v.s. Steady-state Response (External Energy-dependent)
24
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Problem 18 – solve on paper then plot
(next week we solve with Matlab)

Use the method in previous slide
25
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Analytical solutions to differential equations(4/6)
Nonlinear equations

example
yy  5 y  y  0
y  sin y  0
y  y  0
Substitution method for second order equations(1/3)

1)
y  c 2 y  0
(
y(t )  Aest
( s 2  c 2 ) Ae st  0
s  c
y(t )  A1ect  A2e ct
suppose that
)
general solution
c  2, y (0)  6, y (0)  4
y(0)  6  A1  A2
y (0)  2 A1  2 A2  4
solution
A1  4, A2  2
27
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Analytical solutions to differential equations(5/6)
Substitution method for second order equations(2/3)

2)
y   2 y  0
(
y(t )  Aest )
y(t )  A1eit  A2e it
Euler’s identities
general solution
e  it  cos t  i sin t
y(t )  B1 sin t  B2 cos t
B1  y (0) / , B2  y(0)
y (t ) 
period
3)
y (0)

P
sin t  y (0) cos t
2

frequency
my  cy  ky  f (t )
(
f  1/ P
f (t )  0, y(t )  Aest
)
(ms2  cs  k ) Aest  0
1. Real and distinct: s1 and s2.
y(t )  A1e s1t  A2e s2t
2. Real and equal: s1.
y(t )  ( A1  tA2 )e s1t
3. Complex conjugates:
s  a  i
y(t )  B1e sin t  B2e at cos t
at
28
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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.
y(t )  A1e 3t  A2e 5t
2.
complex roots: m=1,c=10,k=601
characteristic roots
s  5 24i
y(t )  B1e5t sin 24t  B2e5t cos 24t
P  2 / 24   / 12
3.
unstable case, complex roots: m=1,c=-4,k=20
s  2  4i
y(t )  B1e 2t sin 4t  B2e 2t cos 4t
P  2 / 4   / 2
characteristic roots
4.
unstable case, real roots: m=1,c=3,k=-10
characteristic roots s=2,-5.
y(t )  A1e 2t  A2e 5t
29
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
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
30
your solution
MATLAB 입문 : Chapter 8. Numerical calculus and differential equations
Acsl, Postech
Next Week:

we learn how to solve ODE's
with Matlab
31
Download