11 July 2017
Metode Numerik II
1
Initial Value Problems of Ordinary Differential Equations
Program 9-1 Second-order Runge-kutta Method
d2
d
M 2 y (t ) B y(t ) ky(t ) 0 ,
dt
dt
y(0)=1, y’(0)=0
Changing 2nd order ODE to 1st order ODE,
d
y (t ) f ( y, z , t ) z (t )
dt
M
(1)
y(0)=1
d
z (t ) Bz (t ) ky(t ) 0
dt
d
B
k
z (t ) g ( y, z , t )
z (t )
y (t )
dt
M
M
11 July 2017
(2) z(0)=0
Metode Numerik II
2
Second-Order Runge-Kutta Method
By 2nd order RK Method
k1 hf ( yn , zn , t ) h zn
l1 hg ( yn , z n , t ) h(
B
k
zn
yn )
M
M
k2 hf ( yn k1 , zn l1 , t ) h( zn l1 )
k
B
l2 hg ( yn k1 , z n l1 , t ) h ( z n l1 ) ( yn k1 )
M
M
1
k1 k2
2
1
z n 1 z n l1 l2
2
yn 1 yn
11 July 2017
Metode Numerik II
3
Program 9-1
/* CSL/c9-1.c Second Order Runge-Kutta Scheme
(Solving the problem II of Example 9.6) */
#include <stdio.h> #include <stdlib.h>
#include <math.h>
/* time : t
y,z: y,y'
kount: number of steps between two lines of printing
k, m, b: k, M(mass), B(damping coefficient) in Example 9.6
int main() {
int kount, n, kstep=0;
float bm, k1, k2, km, l1, l2;
static float time, k = 100.0, m = 0.5, b = 10.0, z = 0.0;
static float y = 1.0, h = 0.001;
printf( "CSL/C9-1 Second Order Runge-Kutta Scheme \n" );
printf( " t y z\n" );
printf( " %12.6f %12.5e %12.5e \n", time, y, z );
km = k/m; bm = b/m;
for( n = 1; n <= 20; n++ ){
for( kount = 1; kount <= 50; kount++ ){
kstep=kstep+1;
time = h*kstep ;
11 July 2017
k1 = h*z;
2nd order RK
l1 = -h*(bm*z + km*y);
k2 = h*(z + l1);
l2 = -h*(bm*(z + l1) + km*(y + k1));
y = y + (k1 + k2)/2;
z = z + (l1 + l2)/2;
}
printf( " %12.6f %12.5e %12.5e \n", time, y, z );
}
exit(0);
result
}
Metode Numerik II
CSL/C9-1 Second Order Runge-Kutta Scheme
t
y
z
0.000000 1.00000e+00 0.00000e+00
0.100000 5.08312e-01 -6.19085e+00
0.200000 6.67480e-02 -2.46111e+00
0.300000 -4.22529e-02 -1.40603e-01
0.400000 -2.58300e-02 2.77157e-01
0.500000 -4.55050e-03 1.29208e-01
0.600000 1.68646e-03 1.38587e-02
0.700000 1.28624e-03 -1.19758e-02
0.800000 2.83107e-04 -6.63630e-03
0.900000 -6.15151e-05 -1.01755e-03
1.000000 -6.27664e-05 4.93549e-04
4
Various Numerical Methods
C1 1, C2
h=0.1
1.0
Exact solution
Modified Euler
Second order RK
Fourth orde Rk
0.8
0.6
2
y e t C1 cos 2 t C2 sin 2 t
Y(m)
Exact Solution:
0.4
0.2
0.0
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
time
h=0.01
1.0
Exact solution
Euler
Modified Euler
Second order RK
Fourth order Rk
0.8
0.8
0.6
Y(m)
Y(m)
0.6
0.4
0.2
Exact solution
Euler
Modified
Second order RK
Fourth order RK
h=0.001
1.0
0.4
0.2
0.0
0.0
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
-0.2
0.0
Time
11 July 2017
0.2
0.4
0.6
0.8
1.0
Time
Metode Numerik II
5
Error Estimation
Error estimation
Fourth order Runge-Kutta
Exact solution
h=0.001
h=0.005
h=0.01
h=0.05
h=0.1
1.0
0.8
0.0025
0.0020
Error
Y(m)
0.6
0.4
0.2
Euler
Modified Euler
Second order RK
Fourth order RK
2
reference(h )
3
reference(h )
5
reference(h )
0.0030
0.0015
0.0010
0.0005
0.0
0.0000
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
time
11 July 2017
0.00
0.01
0.02
0.03
0.04
0.05
h
Metode Numerik II
6
Program 9-2 Fourth-order Runge-Kutta Scheme
A first order Ordinary differential equation
y ' (t )
y (t )
t 2 y 2 (t )
y(0)=1
Fourth-order RK Method
k1 hf ( yn , tn )
k
h
k 2 hf yn 1 , t n
2
2
yn 1 yn
1
k1 2k2 2k3 k4
6
k
h
k3 hf yn 2 , t n
2
2
k4 hf yn k3 , tn h
11 July 2017
Metode Numerik II
7
Program 9-2 Fourth-order Runge-Kutta Scheme
Main algorithm for 4th order RK method at program 9-2
do{ for( j = 1; j <= nstep_pr; j++ ){
t_old = t_new;
t_new = t_new + h;
yn = y;
t_mid = t_old + hh;
yn = y;
k1 = h*fun( yn, t_old );
ya = yn + k1/2; k2 = h*fun( ya, t_mid );
ya = yn + k2/2; k3 = h*fun( ya, t_mid );
ya = yn + k3 ; k4 = h*fun( ya, t_new );
y = yn + (k1 + k2*2 + k3*2 + k4)/6;
}
double fun(float y, float t) {
float fun_v;
fun_v = t*y +1; /* Definition of f(y,t) */
return( fun_v );
}
11 July 2017
4th order RK
CSL/C9-2 Fourth-Order Runge-Kutta Scheme
Interval of t for printing ?
1
Number of steps in one printing interval?
10
Maximum t?
5
h=0.1
-------------------------------------t
y
-------------------------------------0.00000
0.000000e+00
1.00000
1.410686e+00
2.00000
8.839368e+00
3.00000
1.125059e+02
4.00000
3.734231e+03
5.00000
3.357971e+05
6.00000
8.194354e+07
-------------------------------------Maximum t limit exceeded
Interval of t for printing ?
1
-------------------------------------Number of steps in one printing interval? t
y
100
-------------------------------------Maximum t?
0.00000
0.000000e+00
5
1.00000
1.410686e+00
h=0.01
2.00000
8.839429e+00
3.00000
1.125148e+02
4.00000
3.735819e+03
5.00002
3.363115e+05
--------------------------------------
Metode Numerik II
8
Program 9-3 4th order RK Method for a Set of ODEs
A second order Ordinary differential equation
y1 ' ' (t ) y1 (t ) 0
d
y1 (t ) y2 (t )
dt
y1(0)=1, y1’(0)=0
d
y2 (t ) y1 (t )
dt
* The 4th order RK method for the set of two equations(Nakamura’s book p332)
do{
void f(float k[], float y[], float *t, float *h)
for( n = 1; n <= ns; n++ ){
{
t_old = t_new; /* Old time */
k[1] = y[2]**h;
t_new = t_new + h; /* New time */
k[2] = -y[1]**h;
t_mid = t_old + hh; /* Midpoint time */
/* More equations come here if the number of equations
for( i = 1; i <= No_of_eqs; i++ ) ya[i] = y[i];
are greater.*/
f( k1, ya, &t_old, &h );
return;
for( i = 1; i <= No_of_eqs; i++ ) ya[i] = y[i] + k1[i]/2;
}
f( k2, ya, &t_mid, &h );
for( i = 1; i <= No_of_eqs; i++ ) ya[i] = y[i] + k2[i]/2;
f( k3, ya, &t_mid, &h );
for( i = 1; i <= No_of_eqs; i++ ) ya[i] = y[i] + k3[i];
f( k4, ya, &t_new, &h );
for(11
i =July
1; i <=
No_of_eqs; i++ ) y[i] = y[i] + (k1[i]
+ k2[i]*2
+ k3[i]*2
+ k4[i])/6;
2017
Metode
Numerik
II
9
}
Results of Program 9-3
CSL/C9-3
Fourth-Order Runge-Kutta Scheme
for a Set of Equations
Interval of t for printing ? 1
Interval of t for printing ? 1
Number of steps in one print interval ? 100
Number of steps in one print interval ? 10
Maximum t to stop calculations ? 5.0
Maximum t to stop calculations ? 5.0
h= 0.01
h= 0.1
t
y(1),
y(2), .....
t
y(1),
y(2), .....
0.0000
1.00000e+00
0.00000e+00
0.0000 1.00000e+00 0.00000e+00
1.0000 5.40302e-01 -8.41471e-01
1.0000 5.40303e-01 -8.41470e-01
2.0000 -4.16147e-01 -9.09298e-01
2.0000 -4.16145e-01 -9.09298e-01
3.0000 -9.89992e-01 -1.41120e-01
3.0000 -9.89992e-01 -1.41123e-01
4.0000 -6.53644e-01 7.56802e-01
4.0000 -6.53646e-01 7.56800e-01
5.0000 2.83662e-01 9.58924e-01
5.0000 2.83658e-01 9.58925e-01
Type 1 to continue, or 0 to stop.
6.0000 9.60168e-01 2.79420e-01
1
Type 1 to continue, or 0 to stop.
1
11 July 2017
Metode Numerik II
Interval of t for printing ? 1
Number of steps in one print interval ? 1
Maximum t to stop calculations ? 5.0
h= 1
t
y(1),
y(2), .....
0.0000 1.00000e+00 0.00000e+00
1.0000 5.41667e-01 -8.33333e-01
2.0000 -4.01042e-01 -9.02778e-01
3.0000 -9.69546e-01 -1.54803e-01
4.0000 -6.54173e-01 7.24103e-01
5.0000 2.49075e-01 9.37367e-01
Type 1 to continue, or 0 to stop.
10
Shooting Method for Boundary Value Problem ODEs
Definition: a time stepping algorithm along with a root finding method for choosing
the appropriate initial conditions which solve the boundary value problem.
Second-order Boundary-Value Problem
y1 ' ' f ( x, y, y' ), y(a)=A and y(b)=B
* Three types of boundary conditions
dy (a )
dt
1) 0 0 : Dirichlet type boundary condition
1) 0 0 : Neumann type boundary condition
1) 0 0 : Mixed type boundary condition
y (a)
11 July 2017
Metode Numerik II
11
Computational Algorithm of Shooting Method
Computational Algorithm
1. Solve the differential equation using a time-stepping scheme with the initial
conditions y(a)=A and y’(a)=A’.
2. Evaluate the solution y(b) at x=b and compare this value with the target value
of y(b)=B.
3. Adjust the value of A’ (either bigger or smaller) until a desired level of tolerance
and accuracy is achieved. A bisection or secant method for determining values of
A’, for instance, may be appropriate.
4. Once the specified accuracy has been achieved, the numerical solution is complete
and is accurate to the level of the tolerance chosen and the discretization scheme
used in the time-stepping.
11 July 2017
Metode Numerik II
12
Shooting Method for Boundary Value Problem ODEs
Rewrite the second-order ODE as two first-order ODEs:
y ' (t ) z ,
y (a) A
z ' (t ) f ( x, y, z ),
z (a) y ' (a) ?
We should assume a initial value of z(a).
z(a) is determined for which y(b)=B by secant method.
B (B)
B
( n)
( A' )( n1) ( A' )( n )
( A' )
11 July 2017
( n 1)
( B)( n1)
Slope
( A' )( n ) ( A' )( n1)
( A' )
(n)
B (B)
(n)
(n)
Slope
Metode Numerik II
13
Example of Shooting Method
Ex)
T ' ' 2T 0,
T(0)=0 and T(1.0)=100
Sol) Rewrite the second-order ODE as two first-order ODEs:
T ' S,
T(0)=0.0
S ' 2T ,
S(0)=T’(0)
By modified Euler method
P
Ti 1
Ti tSi
1
Ti C1 Ti t Si S iP1
2
SiP1 Si t 2Ti
1
SiC1 Si t 2 Ti Ti P1
2
Let x=0.25, S(0.0)(1)=50, S(0.0)(2)=100
11 July 2017
Metode Numerik II
14
Solution by the Shooting Method
T (1.0)( 2) T (1.0)(1) 170.507813 85.253906
Slope
1.705078
( 2)
(1)
S (0.0) S (0.0)
100.0 50.0
S (0.0)
11 July 2017
( 3)
S (0.0)
100.0 T (1.0) 58.648339
( 2)
( 2)
Slope
Metode Numerik II
15
Errors in the Solution by the Shooting Method
11 July 2017
Metode Numerik II
16
Equilibrium Method
y1 ' ' P( x) y'Q( x) y F ( x),
y(a)=A and y(b)=B
x
x
2
1
P
y
2
x
Q
y
Pi yi 1 x 2 Fi
1
i i 1
i
i
2
2
Ex) T ' ' 2T 2Ta ,
T(0)=0 and T(1.0)=100
Ti 1 (2 2 x 2 )Ti Ti 1 2 x 2Ta
Let x=0.25, Ta=0, =2.0
Ti 1 2.25Ti Ti 1 0
x=0.25 : T0 2.25T1 T2 0, T0 0
x=0.50 : T1 2.25T2 T3 0
x=0.75 : T2 2.25T3 T4 0, T4 100.0
11 July 2017
Metode Numerik II
-2.25 1.0 0
1.0 -2.25 1.0
0
1.0 -2.25
0
T1
0
T2 =
-100
T3
17
Executing Programs for 490D class
A user ID will be made as team name at pam10 computer.
A password is the same as a user ID
You can execute your programs to connect your PC to pam10 computer
by x-manager or telnet.
A C program is compiled by a gcc compiler at the linux PC.
gcc –o a a.c –lm
./a
where, a is a changeable execution sentence.
TA’s E-mail: [email protected] (고한석), [email protected] (김성진)
11 July 2017
Metode Numerik II
18
© Copyright 2026 Paperzz