Docsity

Numerical Methods for Initial Value Differential Equations: Euler, Runge-Kutta, Study notes of Computer-Aided Analysis of Machine Dynamics

Toggle sidebar
bg1
Forward Time Integration
Euler’s Method
given first-order initial-value differential equations of the form

t},y{f}y{
for time step i1i tth
determine the slope at the beginning of the interval

ii t,yfk
hkyy i1i
Second Order Runge-Kutta
given first-order initial-value differential equations of the form

t},y{f}y{
for time step i1i tth
determine the slope at the beginning of the interval

iiA t,yfk
estimate the slope at the end of the interval using Euler’s method

ht,hkyfk iAiB
use the mean of these two slopes
BA
2
1
Mkkk
hkyy Mi1i
ti
y(t)
t
ti+1
kA
kB
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Numerical Methods for Initial Value Differential Equations: Euler, Runge-Kutta and more Study notes Computer-Aided Analysis of Machine Dynamics in PDF only on Docsity!

Forward Time Integration

Euler’s Method

given first-order initial-value differential equations of the form {y }f{^ y},t

for time step h  ti  1 ti

determine the slope at the beginning of the interval k f yi ,ti

y (^) i  1 yik h

Second Order Runge-Kutta

given first-order initial-value differential equations of the form {y }f{ y},t

for time step h  ti  1 ti

determine the slope at the beginning of the interval k (^) A f yi,ti

estimate the slope at the end of the interval using Euler’s method k (^) B f yikAh,tih

use the mean of these two slopes k (^) M  21  kAkB

y (^) i  1 yikM h

t (^) i

y(t)

t t (^) i+

kA

kB

Fourth Order Runge-Kutta (RK4)

determine the slope at the beginning of the interval k 1 f yi,ti

estimate the slope at the midpoint of the interval using Euler’s method k 2 f yik 12 h,ti 2 h

estimate the slope at the midpoint of the interval using the new value k 3 f yik 22 h,ti 2 h

estimate the slope at the end of the interval using the newest value k 4 f yik 3 h,tih

use a weighted mean of slopes k (^) M  61 k 1  2 k 2  2 k 3 k 4 

y (^) i  1 yikM h

t (^) i t (^) i +½h

y(t)

t t (^) i+

k

k

k

k

know coefficients a 0 , a 1 and a 2 for interpolant f() = a 0 + a 1  + a 2 ^2 = y^ ()

yi+1 = ydt

ti 1  (^) ti^ 

  • yi = h fd

1  0 ^ + yi^ = h ( a^0 ^ + a^1 

(^2) /2 + a 2  (^3) /3 ) 1 0 + yi

yi+1 = yi + h ( a 0 + a 1 /2 + a 2 /3 )

PREDICTOR (3-step Adams-Bashforth) y (^) i  1 yih  23 fi 16 fi 1  5 fi 2  / 12

now use predicted value of yi+1 to compute f(yi+1 ) = fi+1*

use third-degree polynomial corrector f = b 0 + b 1  + b 2 ^2 + b 3 ^3 = [ 1  ^2 ^3 ] 

3

2

1

0

b

b

b

b

f

f

f

f

b

b

b

b

b

b

b

b

f

f

f

f

i 2

i 1

i

i* 1

3

2

1

0

3

2

1

0

i 2

i 1

i

i* 1

know coefficients b 0 , b 1 , b 2 and b 3 for new interpolant^ f() = b 0 + b 1 ^ + b 2 ^2 + b 3 ^3 =^ y^ ()

yi+1 = ydt

ti 1  (^) ti^ 

  • yi = h fd

1  0 ^ + yi^ = h ( b^0 ^ + b^1 

(^2) /2 + b 2  (^3) /3 + b 3  (^4) /4 ) 10 + yi

yi+1 = yi + h ( b 0 + b 1 /2 + b 2 /3 + b 3 /4 )

CORRECTOR (4-step Adams-Moulton) y (^) i  1 yih^  9 fi* 1  19 fi 5 fi 1 fi 2 ^ / 24

fi-

fi

fi-



fi+1*

variable time step second-degree polynomial predictor for f f = a 0 + a 1 (t-ti) + a 2 (t-t (^) i ) 2

 

 

  

 i 2

i 1

i 1

2

1

0

2

1

0

2

1

0

i 2 i i 2 i^2

i 1 i i 1 i^2 i 2

i 1

i

f

f

f [A] a

a

a

a

a

a [A] a

a

a

1 (t t ) (t t )

1 (t t) (t t)

f

f

f

yi+1 = ydt

ti 1  (^) ti^ 

  • yi

PREDICTOR

yi+1 = yi + a 0 (t (^) i+1 -t (^) i ) + a 1 (t (^) i+1 -t (^) i ) 2 /2 + a 2 (t (^) i+1 -t (^) i) 3 /

use predicted value of yi+1 to compute f(yi+1 ,t) = fi+1*

use third-degree polynomial corrector for f f = b 0 + b 1 (t-t (^) i ) + b 2 (t-ti ) 2 + b 3 (t-t (^) i )^3

 

  

  

  

i 2

i 1

i

i 1 1

3

2

1

0

3

2

1

0

3

2

1

0

3 i 2 i

2 i 2 i i 2 i

3 i 1 i

2 i 1 i i 1 i

3 i 1 i

2 i 1 i i 1 i

i 2

i 1

i

i 1

f

f

f

f

[B]

b

b

b

b

b

b

b

b

[B]

b

b

b

b

1 (t t) (t t ) (t t )

1 (t t) (t t) (t t)

1 (t t) (t t) (t t)

f

f

f

f

CORRECTOR

yi+1 = yi + b 0 (t (^) i+1 -t (^) i ) + b 1 (t (^) i+1 -t (^) i ) 2 /2 + b 2 (t (^) i+1-t (^) i )^3 /3 + b 3 (t (^) i+1-t (^) i) 4 /

FIXED TIME STEP

  1. Predictor and corrector functions are constant coefficient, weighted sums of f (^) i.
  2. Coefficients are the same for all functions {y }^ f{ y},t.

VARIABLE TIME STEP

  1. Matrices [A] and [B] must be inverted at each time step.
  2. Matrices [A] and [B] are the same for all functions {y } f{ y},t.

%% pred_corr.msimulate exponential- time integration y = exp(-t), using ydotquadratic = -exp(-t) predictor, = -y cubic corrector % HJSIII, 11.01. clear %pred predictor-corrector = [ 23 -16 5 coefficients] / 12; corr = [ 9 19 -5 1 ] / 24; %h time= 0.001; step and duration of simulation nnm1 = 1001;= n - 1; t = h * (0:nm1)'; %y_exact exact solution= exp( -t );

%y_pred_only allocate space = zeros(n,1); for solutions f_pred_onlyy_pred_corr == zeros(n,1);zeros(n,1); f_pred_corr = zeros(n,1); %y_pred_only(1:3) use exact solution = y_exact(1:3);for first three samples to get started f_pred_only(1:3)y_pred_corr(1:3) == -y_pred_only(1:3);y_exact(1:3); f_pred_corr(1:3) = -y_pred_corr(1:3); %for integrate i = 3 : forwardnm1, in time y_pred_only(i+1)+ h * pred * =[ y_pred_only(i)f_pred_only(i) ...f_pred_only(i-1) f_pred_only(i-2) ]'; f_pred_only(i+1) = -y_pred_only(i+1); y_star+ h= *y_pred_corr(i) pred * [ f_pred_corr(i) ... f_pred_corr(i-1) f_pred_corr(i-2) ]'; f_stary_pred_corr(i+1) = -y_star; = y_pred_corr(i) ... f_pred_corr(i+1)+^ h^ *^ corr^ *^ [=^ f_star-y_pred_corr(i+1);^ f_pred_corr(i)^ f_pred_corr(i-1)^ f_pred_corr(i-2)^ ]'; end %figure( plot exact 1 ) solution clfsubplot( 3, 1, 1 ) plot(title( t,y_exact,'r' 'Exact solution ) y = exp(-t)' )

%err_pred_only plot differences = y_pred_only - y_exact; err_pred_corrsubplot( 3, =1, y_pred_corr 2 ) - y_exact; plot(ylabel( t,err_pred_only,'g') 'Error' ) subplot(title(^ 'Predictor3, 1, 3 )^ only^ -^ dt=0.001'^ ) plot(xlabel( t,err_pred_corr,'b' 'Time [sec]' ) ) ylabel(title( 'Predictor'Error' ) and Corrector - dt=0.001' )

% bottom of pred_corr

Forward Time Integration in MATLAB

[T,Y] = solver (odefun,tspan,y0) [T,Y] = solver (odefun,tspan,y0,options)

odefun (^) A function handle that evaluates the right side of the differential equations. All solvers solve systems of equations in the form y ′ = f ( t , y ) or problems that involve a mass matrix, M ( t , y ) y ′ = f ( t , y ). The ode23s solver can solve only equations with constant mass matrices. ode15s and ode23t can solve problems with a mass matrix that is singular, i.e., differential-algebraic equations (DAEs). tspan (^) A vector specifying the interval of integration, [t0,tf]. The solver imposes the initial conditions at tspan(1), and integrates from tspan(1) to tspan(end). To obtain solutions at specific times (all increasing or all decreasing), use tspan = [t0,t1,...,tf].

For tspan vectors with two elements [t0 tf], the solver returns the solution evaluated at every integration step. For tspan vectors with more than two elements, the solver returns solutions evaluated at the given time points. The time values must be in order, either increasing or decreasing. Specifying tspan with more than two elements does not affect the internal time steps that the solver uses to traverse the interval from tspan(1) to tspan(end). All solvers in the ODE suite obtain output values by means of continuous extensions of the basic formulas. Although a solver does not necessarily step precisely to a time point specified in tspan, the solutions produced at the specified time points are of the same order of accuracy as the solutions computed at the internal time points.

Specifying tspan with more than two elements has little effect on the efficiency of computation, but for large systems, affects memory management. y0 (^) A vector of initial conditions.

options Structure of optional parameters that change the default integration properties. This is the fourth input argument.

Solver Problem Type

Order of Accuracy

When to Use

MATLAB Algortihms

ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. It is a one- step solver – in computing y(tn), it needs only the solution at the immediately preceding time point, y(tn-1). In general, ode45 is the best function to apply as a first try for most problems. [3]

ode23 is an implementation of an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient than ode45 at crude tolerances and in the presence of moderate stiffness. Like ode45, ode23 is a one-step solver. [2]

ode113 is a variable order Adams-Bashforth-Moulton PECE solver. It may be more efficient than ode45 at stringent tolerances and when the ODE file function is particularly expensive to evaluate. ode113 is a multistep solver — it normally needs the solutions at several preceding time points to compute the current solution. [7]

The above algorithms are intended to solve nonstiff systems. If they appear to be unduly slow, try using one of the stiff solvers below.

ode15s is a variable order solver based on the numerical differentiation formulas (NDFs). Optionally, it uses the backward differentiation formulas (BDFs, also known as Gear's method) that are usually less efficient. Like ode113, ode15s is a multistep solver. Try ode15s when ode45 fails, or is very inefficient, and you suspect that the problem is stiff, or when solving a differential-algebraic problem. [9], [10]

ode23s is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it may be more efficient than ode15s at crude tolerances. It can solve some kinds of stiff problems for which ode15s is not effective. [9]

ode23t is an implementation of the trapezoidal rule using a "free" interpolant. Use this solver if the problem is only moderately stiff and you need a solution without numerical damping. ode23t can solve DAEs. [10]

ode23tb is an implementation of TR-BDF2, an implicit Runge-Kutta formula with a first stage that is a trapezoidal rule step and a second stage that is a backward differentiation formula of order two. By construction, the same iteration matrix is used in evaluating both stages. Like ode23s, this solver may be more efficient than ode15s at crude tolerances. [8], [1]

References

[1] Bank, R. E., W. C. Coughran, Jr., W. Fichtner, E. Grosse, D. Rose, and R.Smith, "Transient Simulation of Silicon Devices and Circuits," IEEE Trans. CAD , 4 (1985), pp. 436–451.

[2] Bogacki, P. and L. F. Shampine, "A 3(2) pair of Runge-Kutta formulas," Appl. Math. Letters , Vol. 2, 1989, pp. 321–325.

[3] Dormand, J. R. and P. J. Prince, "A family of embedded Runge-Kutta formulae," J. Comp. Appl. Math. , Vol. 6, 1980, pp. 19–26.

[4] Forsythe, G. , M. Malcolm, and C. Moler, Computer Methods for Mathematical Computations , Prentice-Hall, New Jersey, 1977.

[5] Kahaner, D. , C. Moler, and S. Nash, Numerical Methods and Software , Prentice-Hall, New Jersey, 1989.

[6] Shampine, L. F. , Numerical Solution of Ordinary Differential Equations , Chapman & Hall, New York, 1994.

[7] Shampine, L. F. and M. K. Gordon, Computer Solution of Ordinary Differential Equations: the Initial Value Problem , W. H. Freeman, SanFrancisco, 1975.

[8] Shampine, L. F. and M. E. Hosea, "Analysis and Implementation of TR-BDF2," Applied Numerical Mathematics 20 , 1996.

[9] Shampine, L. F. and M. W. Reichelt, "The MATLAB ODE Suite," SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22.

[10] Shampine, L. F., M. W. Reichelt, and J.A. Kierzenka, "Solving Index-1 DAEs in MATLAB and Simulink," SIAM Review , Vol. 41, 1999, pp. 538–552.

Fifth-degree (Quintic) Predictor and Seventh-degree (Heptic) Corrector

with Second Derivatives

given second-order initial-value differential equations of the form {y ^ }f { y},tand {y ^ }g{ y},{y},t

must know { y}i ,{y}i 1 ,{y}i 2 respectively at times ti , t (^) i-1 and t (^) i-

may determine {y  }i ,{y}i 1 ,{y}i 2 and {y }i ,{y}i 1 ,{y}i 2

fixed time step h  = ( t - t (^) i ) / h d = dt / h dt = h d d /dt = 1 / h

use fifth degree polynomial predictor f a 0 a 1 a 2 ^2 a 3 ^3 a 4 ^4 a 5 ^5

gh f gh a 1 2 a 2 3 a 3 2 4 a 4 3 5 a 5 4 dt

fd dt

g df         

t (^) i-2 t (^) i-1 t (^) i t (^) i+

y^ = f(y) fi gi

t

fi- gi-

fi- gi-

fi fi-1 gi gi-

fi- gi-

5

4

3

2

1

0

2 3 4

2 3 4 5

a

a

a

a

a

a

gh

f

5

4

3

2

1

0

i 2

i 1

i

i 2

i 1

i

a

a

a

a

a

a

g h

g h

g h

f

f

f

g h

g h

g h

f

f

f

a

a

a

a

a

a

i 2

i 1

i

i 2

i 1

i

5

4

3

2

1

0

know coefficients ai for interpolant f    a 0 a 1 a 2 ^2 a 3 ^3 a 4 ^4 a 5 ^5 y 

i i^ ^01223344556 ^10

1 i 0

ti 1 y (^) i 1  (^) ti ydty h fdy y ha a / 2 a  / 3 a  / 4 a  / 5 a  / 6

  

y (^) i  1 yih a 0 a 1 / 2 a 2 / 3 a 3 / 4 a 4 / 5 a 5 / 6 

PREDICTOR (3-step Obreshkov) y *i  1 yih  949 fi 608 fi 1  581 fi 2 h  637 gi 1080 gi 1  173 gi 2  / 240

g

g

g

g

f

f

f

f

b

b

b

b

b

b

b

b

i 2

i 1

i

*i 1

i 2

i 1

i

i* 1

7

6

5

4

3

2

1

0

know coefficients bi f   b 0 b 1 b 2 ^2 b 3 ^3 b 4 ^4 b 5 ^5 b 6 ^6 b 7 ^7 y 

i^ ^012233445566778 ^10

i

1 i 0

ti 1 i (^1) ti y hb b / 2 b / 3 b / 4 b / 5 b / 6 b / 7 b / 8

y ydt y h fd y                

 (^)     

  

y (^) i  1 yih  b 0 b 1 / 2 b 2 / 3 b 3 / 4 b 4 / 5 b 5 / 6 b 6 / 7 b 7 / 8 

CORRECTOR (4-step Obreshkov)

 

h 3849 g 22977 g 7263 g 489 g

34465 f 42255 f 12015 f 1985 f y y h *i 1 i i 1 i 2

i i 1 i 2

i 1 i 1 i  

  

   

Third-degree ( Cubic) Predictor and Third-degree (Cubic) Corrector

with Second Derivatives

given second-order initial-value differential equations of the form {y ^ }f { y},tand {y ^ }g{ y},{y},t

must know {y }i and{y}i 1 respectively at times ti and t (^) i-

may determine {y  }i {y }i 1 and {y }i {y}i

fixed time step h  = ( t - t (^) i ) / h d = dt / h dt = h d d /dt = 1 / h

use third degree polynomial predictor f a 0 a 1 a 2 ^2 a 3 ^3

gh f gh a 1 2 a 2 3 a 3 2 dt

fd dt

g df     

3

2

1

0

2

2 3

a

a

a

a

gh

f

t (^) i-1 t (^) i t (^) i+

y^ = f(y) fi gi

t

fi- gi-

-1 0 +1 ^ 

fi fi-1 gi gi-

3

2

1

0

i

i 1

i

i 1

b

b

b

b

g h

g h

f

f

g h

g h

f

f

b

b

b

b

i

i 1

i

i 1

3

2

1

0

know coefficients bi f   b 0 b 1 b 2 ^2 b 3 ^3 y 

i^ ^0122334 ^10

i

1 i 0

ti 1 i (^1) ti y hb b / 2 b / 3 b / 4

y ydt y h fd y        

 (^)     

  

y (^) i  1 yih  b 0 b 1 / 2 b 2 / 3 b 3 / 4 

CORRECTOR (2-step Obreshkov per Sehnalova) y *i  1  yih 6 fi 1  6 fih  gi 1 gi / 12

Least-Squares Quadratic Predictor and Cubic Corrector

with Second Derivatives

given second-order initial-value differential equations of the form {y ^ }f { y},tand {y ^ }g{ y},{y},t

must know { y}i ,{y}i 1 ,{y}i 2 respectively at times ti , t (^) i-1 and t (^) i-

may determine {y  }i ,{y}i 1 ,{y}i 2 and {y }i ,{y}i 1 ,{y}i 2

fixed time step h  = ( t - t (^) i ) / h d = dt / h dt = h d d /dt = 1 / h

use quadratic polynomial predictor f = a 0 + a 1  + a 2 ^2

        

dt g a^1 2 a^2 /h gh a^12 a^2

fd dt

g df

2

1

2 0

a

a

a

0 1 2

gh

f

t (^) i-2 t (^) i-1 t (^) i t (^) i+

y^ = f(y) fi-

fi

fi-

t

fi-

fi

fi-

 (^) 