1
Programming, Computation, Simulation
Applications in Math & Physics
Euler and Runge-Kutta Methods
Ordinary Differential Equations & Initial Value Problems
Syeilendra Pramuditya
Department of Physics
Institut Teknologi Bandung
Grid-based Computation
 Euler-type Methods
 Differential equations
 Temporal domain (variation in time)
 Initial conditions
 Finite Difference Methods
 Differential equations
 Spatial domain (variation in space)
 Boundary conditions: Von Neumann VS
Dirichlet
3
Differential Equation
2
( ) ( , ) 1
( 0) 0
( ) ..?
dy
y x f x y x
dx
y x
y x
    
 

4
Differential Equation
2
( ) ( , ) 1
( 0) 0
dy
y x f x y x
dx
y x
    
 
2
3
3
Exact Solution
( 1)
1
3
( 0) 0 0
1
( ) ( , )
3
dy x dx
y x x C
y x C
y x f x y x x
 
  
   
  
 
5
Differential Equation
1
( ) ( , )
( 1) 0
( ) ..?
dy
y x f x y
dx x
y x
y x
   
 

6
Differential Equation
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
   
 
Exact Solution
1
ln( )
( 1) 0 0
( ) ( , ) ln( )
dy dx
x
y x C
y x C
y x f x y x

 
   
 
 
7
Differential Equation
2 1
( ) ( , ) 1
( 0) 0
..???
dy
y x f x y x
dx y
y x
y
     
 

Euler Methods
 Linear approx. of Taylor series
 “Simple Euler”
0
x x
0
( )
f x
( )
f x  
0
0 0 0
0
0 0 0
0
0 0 0 0
0 0 0 0
0 0 0
( ) ( )
( ) ( , )
( ) ( ) ( )
( ) ( ) ( , )
( ) ( ) ( , ( ))
( )
y x y x
y x f x y
x x
y x y x x x y x
x x h
y x h y x hf x y
y x h y x hf x y x
y x h y hf

  


  
 
  
  
  
0 0 0
( ) ( , )
y
y x f x y
x

  

9
Ordinary Differential Equation (ODE)
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
( , )
new old old old
y x h y x hf x y
y x h y hf
y y hf x y
  
  
 
 Solve the above ODE numerically (find y(x)) using Simple
Euler method (for 1<x<10), use h = 1.0 and h = 0.5
 How good the numerical solution is?
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
   
 
Simple Euler Code
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
( , )
new old old old
y x h y x hf x y
y x h y hf
y y hf x y
  
  
 
Solve the ODE numerically (find y(x)) using Simple
Euler method (for 1<x<10), use h = 1.0 and h = 0.5
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
   
 
Exact VS Numeric
0
0.5
1
1.5
2
2.5
3
0 2 4 6 8 10
Exact
Euler, h = 1.0
Euler, h = 0.5
Euler Methods
 Linear approx. of Taylor series
 “Modified Euler” Use more correct gradient
0
x x
0
( )
f x
( )
f x
 
0 0
0 0
0
0
0 0
0 0
( ) ( ) ( )
( ) ( ) ( , )
2
( )
2
2
( )
mid
mid mid
mid
mid
mid
mid
y x y x x x y x
y x h y x hf x y
h
x x
h
y y x
h
y y f
y x h y hf

  
  
 
 
 
  
Modified Euler
 
0 0
0 0
0
0
0 0
0 0
( ) ( ) ( )
( ) ( ) ( , )
2
( )
2
2
( )
mid
mid mid
mid
mid
mid
mid
y x y x x x y x
y x h y x hf x y
h
x x
h
y y x
h
y y f
y x h y hf

  
  
 
 
 
  
Euler Methods
 Linear approx. of Taylor series
 “Improved Euler” Use more correct gradient
0
x x
0
( )
f x
( )
f x
0 0 0
0 0
0
0 0
0 0 0 0
0 0
( ) ( , )
( ) ( , )
( ) ( )
( )
2
( , ) ( , )
2
( , ) ( , ( , ))
( ) ( )
avg
avg
avg
y x f x y
y x f x y y hf
y x y x
y x
f x y f x y
f
f x y f x h y hf x y
y x h y x hf
 
   
 

 


  
  
Improved Euler
0 0 0
0 0
0
0 0
0 0 0 0
0 0
( ) ( , )
( ) ( , )
( ) ( )
( )
2
( , ) ( , )
2
( , ) ( , ( , ))
( ) ( )
avg
avg
avg
y x f x y
y x f x y y hf
y x y x
y x
f x y f x y
f
f x y f x h y hf x y
y x h y x hf
 
   
 

 


  
  
2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( , )
( , )
( , )
2 2
( )
dy
y f x y
dx
k hf x y
k
h
k hf x y
y x h y k
  

  
  
4th Order Runge-Kutta (RK4)
 
1 0 0
2 0 0 1
3 0 0 2
4 0 0 3
0 0 1 2 3 4
4th Order Runge-Kutta
( , )
( , )
( , )
2 2
( , )
2 2
( , )
( ) 2 2
6
dy
y f x y
dx
k f x y
h h
k f x y k
h h
k f x y k
k f x h y hk
h
y x h y k k k k
  

  
  
  
     
Exact VS Numeric
 Simple Euler (h = 0.2)
Exact VS Numeric
 Modified Euler (h = 0.2)
Exact VS Numeric
 Improved Euler (h = 0.2)
Exact VS Numeric
 RK4 (h = 0.2)
22
Block-Spring System
( ) ( , ) ( )
( ) ( , ) ( )
F ma
dv F k
a x
dt m m
dv k k
x v t f x t x t
dt m m
dx
v x t f v t v t
dt


  
 

   

   
23
Block-Spring System
   
   
0 0 0
0 0 0 0
0
0 0 0
0 0 0 0
0
2 2
1 1
( ) ( ) ( ) ( ) ( , )
1 1
( ) ( ) ( ) ( ) ( , )
1 1
2 2
t
t
v v at v t v v t t v t v t v f v t
t t
dv k
v t v t v x
dt m
x x vt x t x x t t x t x t x f x t
t t
dx
x t x t x v
dt
E mv kx
  
  


 
        
    
 
        
    
 
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
y x h y hf
  
  
24
Block-Spring System (Simplified)
1
2
0 0
0 0 1 0 0 0 0
0 0 2 0 0 0 0 0 0
2 2
( ) ( , ) ( )
( ) ( , ) ( )
( )
( ) ( , )
( ) ( , ) ( )
1 1
2 2
dv k
v t f t v x t
dt m
dx
x t f t x v t
dt
kx t kx
v t t v t f t v v t v t
m m
x t t x t f t x x tv t x tv
E mv kx

   
   
   
 
           
   
   
           
 
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
  
    
25
Block-Spring System (Simplified)
0 0
0 0 0 0 0
0 0 0 0 0 0 0
2 2
( ) ( )
( ) ( )
( )
( ) ( )
( ) ( ) ( )
1 1
2 2
dv k
v t x t
dt m
dx
x t v t
dt
kx t kx
v t t v t v t v t v t
m m
x t t x t x t x tv t x tv
E mv kx

  
  
   
 

           
   
   

           
 
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
  
    
26
Block-Spring System (Simplified)
0
0 0 0 0 0
0 0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx
dv F
v t t v t v a t v t v t
dt m m
k
v t t v x t
m
dx
x t t x t x v t
dt
E mv kx

             
    
       
 
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
  
    
27
Block-Spring System
1
0 0 0 0 0 0 0 0 0
2
0 0 0
0 0 0 0 0 0
Simple Euler
( , ) ( ) ( )
( ) ( , ) ( , )
( , ) ( ) ( )
( , )
( ) ( , )
new old old
new old
dx
x t x f t v t
dt
x t t x t x t x x tv t v x tv
x x tv
dv k
v t v f t x t
dt m
kx t x kx
v t t v t v t v v t v t
m m
v v t
   

           
  

   
   
 

           
   
   

  
2 2
1 1
2 2
old
kx
m
E mv kx
 
 
 
 
Block-Spring System
 Analytic Solution
( ) cos( )
( ) sin( )
m
m
x t x t
v t v t
 
 
 
  
29
Write a Simple Euler Code
 m = k = 1
 0 < t < 20 sec
 dt = 0.2
 x(t=0) = 1
 v(t=0) = 0
 Calculate x(t) and v(t) using Simple Euler
 Output: time,x(t),x_exact,v(t),v_exact,Em
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
  
    
0
0 0 0 0 0
0 0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx
dv F
v t t v t v a t v t v t
dt m m
k
v t t v x t
m
dx
x t t x t x v t
dt
E mv kx

             
    
       
 
Simple Euler Code
0 0 0
0 0 0
2 2
( )
( )
1 1
2 2
k
v t t v x t
m
x t t x v t
E mv kx
    
    
 
output.txt
Gnuplot Script
# Script to plot 1D dataset
reset
unset label
unset key
set key left top
set xrange [0:10]
set yrange [-3:3]
set title "Plot Image"
set xlabel "X Value"
set ylabel "Y Value"
set terminal wxt size 600,400 font "Verdana,10"
plot 'output.txt' using 2:3 title "Numeric" with linespoints pointtype 6 lw 1 lc 7, 
'output.txt' using 2:4 title "Analytic" with linespoints pointtype 6 lw 1 lc 6
33
Simple Euler: Position
34
Simple Euler: Velocity
Simple Euler: Energy
36
Block-Spring System
1
2
0 0 1 0 0 0
0 0 2 0 0 0
0 0 1
0 0 2
Modified Euler
( , ) ( ) ( )
( , ) ( ) ( )
( ) ( )
2 2 2
( ) ( )
2 2 2
( ) ( )
( ) ( )
mid
mid
mid
mid
dx
x t x f t v t
dt
dv k
v t v f t x t
dt m
t t t
x x t x f t x v
t t t k
v v t v f t v x
m
x t t x t f t
v t t v t f t
   

   
  
       
   
       
     
     
37
Modified Euler: Position
38
Modified Euler: Velocity
Modified Euler: Energy
Mass-Spring System
 2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k

  
 
   
  
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
y x
dy
y f x y
dx
k hf x y
k
h
k hf x y
y x h y k

  

  
  
y(x)
x
q(t)
t
Mass-Spring System
 2nd Order Runge-Kutta (RK2)
1
( ) ?
( , ) ( )
x t
dx
x f t x v t
dt

   
x(t)
t
v(t)
t
2
( ) ?
( , ) ( )
v t
dv k
v f t v x t
dt m


   
Mass-Spring System
 2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k

  
 
   
  
1
1 1
2 1
2
2
( ) ( )
( ) ( )
( ) ( )
2 2
( ) ( ) ( ) ( )
2 2 2
( ) ( )
2
( ) ( ) ( ) ( ) ( )
2
dx
x f t v t
dt
k t f t t v t
t t
k t f t t v t
t t t k
v t v t v v t x t
m
t k
k t v t x t
m
t k
x t t x t k x t t v t x t
m
   
     
 
       
   

     
 
 
    
 
 
 
 
         
 
 
Mass-Spring System
 2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k

  
 
   
  
2
1 2
2 2
2
2
( ) ( )
( ) ( )
( ) ( )
2 2
( ) ( ) ( ) ( )
2 2 2
( ) ( )
2
( ) ( ) ( ) ( ) ( )
2
dv k
v f t x t
dt m
k
k t f t t x t
m
t k t
k t f t t x t
m
t t t
x t x t x x t v t
k t
k t x t v t
m
k t
v t t v t k v t t x t v t
m

   

     
  
       
  

    
 
 
   
 
 
 
 
        
 
 
RK2: Position x(t)
RK2: Velocity v(t)
RK2: Mechanical Energy Em(t)
4th Order Runge-Kutta (RK4)
 
1 0 0
2 0 0 1
3 0 0 2
4 0 0 3
0 0 1 2 3 4
( , )
( , )
( , )
2 2
( , )
2 2
( , )
( ) 2 2
6
dy
y f x y
dx
k f x y
h h
k f x y k
h h
k f x y k
k f x h y hk
h
y x h y k k k k
  

  
  
  
     
1
2
( ) ( )
( ) ( )
( ) ?
( ) ?
dx
x f t v t
dt
dv k
v f t x t
dt m
x t dt
v t dt
   

   
 
 
RK4: Position x(t)
49
Damped Mass-Spring System
F ma kx cv
dv F kx cv
a
dt m m
dv k c
x v
dt m m
dx
v
dt
   
 
  

 

50
Damped Mass-Spring System
     
   
0 0 0
0 0 0 0
0
0 0 0
0 0 0 0
0
2 2
1 1
( ) ( ) ( ) ( ) ( , )
1 1
( ) ( ) ( ) ( ) ( , )
1 1
2 2
t
t
v v at v t v v t t v t v t v f v t
t t
dv
v t v t v x cv
dt
x x vt x t x x t t x t x t x f x t
t t
dx
x t x t x v
dt
E mv kx
  
  


 
        
     
 
        
    
 
51
Damped Oscillation
 m = k = 1
 0 < t < 20 sec
 dt = 0.2
 x(t=0) = 1
 v(t=0) = 0
 c = 0.15
 Calculate x(t) and v(t) using Euler Method
 Output: time,x(t),x_exact,v(t),v_exact,Em
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
  
    
0 0
0 0 0 0 0
0 0
0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx cv
dv F
v t t v t v a t v t v t
dt m m
kx cv
v t t v t
m
dx
x t t x t x v t
dt
E mv kx
 
             
 
    
       
 
52
Damped Oscillation (Mod. Euler)
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
0 5 10 15 20
TIme
Position
Damped Oscillation: Analytical Solution
 Halliday-Resnick, Chapter 15
Type of Damped Oscillation
Type of Damped Oscillation
LC Oscillation
2
2
0
d x k
x
dt m
 
Damped LC Oscillation (RLC Circuit)
 The RLC Circuit
2
2
0
d x c dx k
x
dx m dt m
  
Self-Practice for Interested Students
 Ideal/undamped oscillating systems
 Mass-spring systems
 RLC systems
 Modified / Improved Euler Methods
 RK2 / RK4 Methods
 Damped oscillating systems
 Mass-spring systems
 RLC systems
 Modified / Improved Euler Methods
 RK2 / RK4 Methods
 Underdamped / Overdamped / Critically damped

SPSF04 - Euler and Runge-Kutta Methods

  • 1.
    1 Programming, Computation, Simulation Applicationsin Math & Physics Euler and Runge-Kutta Methods Ordinary Differential Equations & Initial Value Problems Syeilendra Pramuditya Department of Physics Institut Teknologi Bandung
  • 2.
    Grid-based Computation  Euler-typeMethods  Differential equations  Temporal domain (variation in time)  Initial conditions  Finite Difference Methods  Differential equations  Spatial domain (variation in space)  Boundary conditions: Von Neumann VS Dirichlet
  • 3.
    3 Differential Equation 2 ( )( , ) 1 ( 0) 0 ( ) ..? dy y x f x y x dx y x y x        
  • 4.
    4 Differential Equation 2 ( )( , ) 1 ( 0) 0 dy y x f x y x dx y x        2 3 3 Exact Solution ( 1) 1 3 ( 0) 0 0 1 ( ) ( , ) 3 dy x dx y x x C y x C y x f x y x x              
  • 5.
    5 Differential Equation 1 ( )( , ) ( 1) 0 ( ) ..? dy y x f x y dx x y x y x       
  • 6.
    6 Differential Equation 1 ( )( , ) ( 1) 0 dy y x f x y dx x y x       Exact Solution 1 ln( ) ( 1) 0 0 ( ) ( , ) ln( ) dy dx x y x C y x C y x f x y x           
  • 7.
    7 Differential Equation 2 1 () ( , ) 1 ( 0) 0 ..??? dy y x f x y x dx y y x y         
  • 8.
    Euler Methods  Linearapprox. of Taylor series  “Simple Euler” 0 x x 0 ( ) f x ( ) f x   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ( ) ( ) ( ) ( , ) ( ) ( ) ( ) ( ) ( ) ( , ) ( ) ( ) ( , ( )) ( ) y x y x y x f x y x x y x y x x x y x x x h y x h y x hf x y y x h y x hf x y x y x h y hf                     0 0 0 ( ) ( , ) y y x f x y x     
  • 9.
    9 Ordinary Differential Equation(ODE) 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) ( , ) new old old old y x h y x hf x y y x h y hf y y hf x y          Solve the above ODE numerically (find y(x)) using Simple Euler method (for 1<x<10), use h = 1.0 and h = 0.5  How good the numerical solution is? 1 ( ) ( , ) ( 1) 0 dy y x f x y dx x y x      
  • 10.
    Simple Euler Code 00 0 0 0 0 0 ( ) ( ) ( , ) ( ) ( , ) new old old old y x h y x hf x y y x h y hf y y hf x y         Solve the ODE numerically (find y(x)) using Simple Euler method (for 1<x<10), use h = 1.0 and h = 0.5 1 ( ) ( , ) ( 1) 0 dy y x f x y dx x y x      
  • 11.
    Exact VS Numeric 0 0.5 1 1.5 2 2.5 3 02 4 6 8 10 Exact Euler, h = 1.0 Euler, h = 0.5
  • 12.
    Euler Methods  Linearapprox. of Taylor series  “Modified Euler” Use more correct gradient 0 x x 0 ( ) f x ( ) f x   0 0 0 0 0 0 0 0 0 0 ( ) ( ) ( ) ( ) ( ) ( , ) 2 ( ) 2 2 ( ) mid mid mid mid mid mid mid y x y x x x y x y x h y x hf x y h x x h y y x h y y f y x h y hf                
  • 13.
    Modified Euler   00 0 0 0 0 0 0 0 0 ( ) ( ) ( ) ( ) ( ) ( , ) 2 ( ) 2 2 ( ) mid mid mid mid mid mid mid y x y x x x y x y x h y x hf x y h x x h y y x h y y f y x h y hf                
  • 14.
    Euler Methods  Linearapprox. of Taylor series  “Improved Euler” Use more correct gradient 0 x x 0 ( ) f x ( ) f x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ( ) ( , ) ( ) ( , ) ( ) ( ) ( ) 2 ( , ) ( , ) 2 ( , ) ( , ( , )) ( ) ( ) avg avg avg y x f x y y x f x y y hf y x y x y x f x y f x y f f x y f x h y hf x y y x h y x hf                   
  • 15.
    Improved Euler 0 00 0 0 0 0 0 0 0 0 0 0 0 ( ) ( , ) ( ) ( , ) ( ) ( ) ( ) 2 ( , ) ( , ) 2 ( , ) ( , ( , )) ( ) ( ) avg avg avg y x f x y y x f x y y hf y x y x y x f x y f x y f f x y f x h y hf x y y x h y x hf                   
  • 16.
    2nd Order Runge-Kutta(RK2) 1 0 0 1 2 0 0 0 0 2 ( , ) ( , ) ( , ) 2 2 ( ) dy y f x y dx k hf x y k h k hf x y y x h y k          
  • 17.
    4th Order Runge-Kutta(RK4)   1 0 0 2 0 0 1 3 0 0 2 4 0 0 3 0 0 1 2 3 4 4th Order Runge-Kutta ( , ) ( , ) ( , ) 2 2 ( , ) 2 2 ( , ) ( ) 2 2 6 dy y f x y dx k f x y h h k f x y k h h k f x y k k f x h y hk h y x h y k k k k                   
  • 18.
    Exact VS Numeric Simple Euler (h = 0.2)
  • 19.
    Exact VS Numeric Modified Euler (h = 0.2)
  • 20.
    Exact VS Numeric Improved Euler (h = 0.2)
  • 21.
    Exact VS Numeric RK4 (h = 0.2)
  • 22.
    22 Block-Spring System ( )( , ) ( ) ( ) ( , ) ( ) F ma dv F k a x dt m m dv k k x v t f x t x t dt m m dx v x t f v t v t dt                 
  • 23.
    23 Block-Spring System        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 1 ( ) ( ) ( ) ( ) ( , ) 1 1 ( ) ( ) ( ) ( ) ( , ) 1 1 2 2 t t v v at v t v v t t v t v t v f v t t t dv k v t v t v x dt m x x vt x t x x t t x t x t x f x t t t dx x t x t x v dt E mv kx                                           0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y y x h y hf      
  • 24.
    24 Block-Spring System (Simplified) 1 2 00 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 2 2 ( ) ( , ) ( ) ( ) ( , ) ( ) ( ) ( ) ( , ) ( ) ( , ) ( ) 1 1 2 2 dv k v t f t v x t dt m dx x t f t x v t dt kx t kx v t t v t f t v v t v t m m x t t x t f t x x tv t x tv E mv kx                                                  0 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y dy y x h y hf y h dx        
  • 25.
    25 Block-Spring System (Simplified) 00 0 0 0 0 0 0 0 0 0 0 0 0 2 2 ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) 1 1 2 2 dv k v t x t dt m dx x t v t dt kx t kx v t t v t v t v t v t m m x t t x t x t x tv t x tv E mv kx                                                  0 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y dy y x h y hf y h dx        
  • 26.
    26 Block-Spring System (Simplified) 0 00 0 0 0 0 0 0 0 0 0 0 2 2 ( ) ( ) ( ) 1 1 2 2 kx dv F v t t v t v a t v t v t dt m m k v t t v x t m dx x t t x t x v t dt E mv kx                               0 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y dy y x h y hf y h dx        
  • 27.
    27 Block-Spring System 1 0 00 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 Simple Euler ( , ) ( ) ( ) ( ) ( , ) ( , ) ( , ) ( ) ( ) ( , ) ( ) ( , ) new old old new old dx x t x f t v t dt x t t x t x t x x tv t v x tv x x tv dv k v t v f t x t dt m kx t x kx v t t v t v t v v t v t m m v v t                                                         2 2 1 1 2 2 old kx m E mv kx        
  • 28.
    Block-Spring System  AnalyticSolution ( ) cos( ) ( ) sin( ) m m x t x t v t v t         
  • 29.
    29 Write a SimpleEuler Code  m = k = 1  0 < t < 20 sec  dt = 0.2  x(t=0) = 1  v(t=0) = 0  Calculate x(t) and v(t) using Simple Euler  Output: time,x(t),x_exact,v(t),v_exact,Em 0 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y dy y x h y hf y h dx         0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 ( ) ( ) ( ) 1 1 2 2 kx dv F v t t v t v a t v t v t dt m m k v t t v x t m dx x t t x t x v t dt E mv kx                              
  • 30.
    Simple Euler Code 00 0 0 0 0 2 2 ( ) ( ) 1 1 2 2 k v t t v x t m x t t x v t E mv kx            
  • 31.
  • 32.
    Gnuplot Script # Scriptto plot 1D dataset reset unset label unset key set key left top set xrange [0:10] set yrange [-3:3] set title "Plot Image" set xlabel "X Value" set ylabel "Y Value" set terminal wxt size 600,400 font "Verdana,10" plot 'output.txt' using 2:3 title "Numeric" with linespoints pointtype 6 lw 1 lc 7, 'output.txt' using 2:4 title "Analytic" with linespoints pointtype 6 lw 1 lc 6
  • 33.
  • 34.
  • 35.
  • 36.
    36 Block-Spring System 1 2 0 01 0 0 0 0 0 2 0 0 0 0 0 1 0 0 2 Modified Euler ( , ) ( ) ( ) ( , ) ( ) ( ) ( ) ( ) 2 2 2 ( ) ( ) 2 2 2 ( ) ( ) ( ) ( ) mid mid mid mid dx x t x f t v t dt dv k v t v f t x t dt m t t t x x t x f t x v t t t k v v t v f t v x m x t t x t f t v t t v t f t                                            
  • 37.
  • 38.
  • 39.
  • 40.
    Mass-Spring System  2ndOrder Runge-Kutta (RK2) 1 0 0 1 2 0 0 0 0 2 ( ) ? ( , ) ( , ) ( , ) 2 2 ( ) q t dq q f t q dt k dt f t q k dt k dt f t q q t dt q k              1 0 0 1 2 0 0 0 0 2 ( ) ? ( , ) ( , ) ( , ) 2 2 ( ) y x dy y f x y dx k hf x y k h k hf x y y x h y k            y(x) x q(t) t
  • 41.
    Mass-Spring System  2ndOrder Runge-Kutta (RK2) 1 ( ) ? ( , ) ( ) x t dx x f t x v t dt      x(t) t v(t) t 2 ( ) ? ( , ) ( ) v t dv k v f t v x t dt m      
  • 42.
    Mass-Spring System  2ndOrder Runge-Kutta (RK2) 1 0 0 1 2 0 0 0 0 2 ( ) ? ( , ) ( , ) ( , ) 2 2 ( ) q t dq q f t q dt k dt f t q k dt k dt f t q q t dt q k              1 1 1 2 1 2 2 ( ) ( ) ( ) ( ) ( ) ( ) 2 2 ( ) ( ) ( ) ( ) 2 2 2 ( ) ( ) 2 ( ) ( ) ( ) ( ) ( ) 2 dx x f t v t dt k t f t t v t t t k t f t t v t t t t k v t v t v v t x t m t k k t v t x t m t k x t t x t k x t t v t x t m                                                              
  • 43.
    Mass-Spring System  2ndOrder Runge-Kutta (RK2) 1 0 0 1 2 0 0 0 0 2 ( ) ? ( , ) ( , ) ( , ) 2 2 ( ) q t dq q f t q dt k dt f t q k dt k dt f t q q t dt q k              2 1 2 2 2 2 2 ( ) ( ) ( ) ( ) ( ) ( ) 2 2 ( ) ( ) ( ) ( ) 2 2 2 ( ) ( ) 2 ( ) ( ) ( ) ( ) ( ) 2 dv k v f t x t dt m k k t f t t x t m t k t k t f t t x t m t t t x t x t x x t v t k t k t x t v t m k t v t t v t k v t t x t v t m                                                             
  • 44.
  • 45.
  • 46.
  • 47.
    4th Order Runge-Kutta(RK4)   1 0 0 2 0 0 1 3 0 0 2 4 0 0 3 0 0 1 2 3 4 ( , ) ( , ) ( , ) 2 2 ( , ) 2 2 ( , ) ( ) 2 2 6 dy y f x y dx k f x y h h k f x y k h h k f x y k k f x h y hk h y x h y k k k k                    1 2 ( ) ( ) ( ) ( ) ( ) ? ( ) ? dx x f t v t dt dv k v f t x t dt m x t dt v t dt             
  • 48.
  • 49.
    49 Damped Mass-Spring System Fma kx cv dv F kx cv a dt m m dv k c x v dt m m dx v dt             
  • 50.
    50 Damped Mass-Spring System          0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 1 ( ) ( ) ( ) ( ) ( , ) 1 1 ( ) ( ) ( ) ( ) ( , ) 1 1 2 2 t t v v at v t v v t t v t v t v f v t t t dv v t v t v x cv dt x x vt x t x x t t x t x t x f x t t t dx x t x t x v dt E mv kx                                           
  • 51.
    51 Damped Oscillation  m= k = 1  0 < t < 20 sec  dt = 0.2  x(t=0) = 1  v(t=0) = 0  c = 0.15  Calculate x(t) and v(t) using Euler Method  Output: time,x(t),x_exact,v(t),v_exact,Em 0 0 0 0 0 0 0 0 ( ) ( ) ( , ) ( ) y x h y x hf x y dy y x h y hf y h dx         0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 ( ) ( ) ( ) 1 1 2 2 kx cv dv F v t t v t v a t v t v t dt m m kx cv v t t v t m dx x t t x t x v t dt E mv kx                                 
  • 52.
    52 Damped Oscillation (Mod.Euler) -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 1.2 0 5 10 15 20 TIme Position
  • 53.
    Damped Oscillation: AnalyticalSolution  Halliday-Resnick, Chapter 15
  • 54.
    Type of DampedOscillation
  • 55.
    Type of DampedOscillation
  • 56.
    LC Oscillation 2 2 0 d xk x dt m  
  • 57.
    Damped LC Oscillation(RLC Circuit)  The RLC Circuit 2 2 0 d x c dx k x dx m dt m   
  • 58.
    Self-Practice for InterestedStudents  Ideal/undamped oscillating systems  Mass-spring systems  RLC systems  Modified / Improved Euler Methods  RK2 / RK4 Methods  Damped oscillating systems  Mass-spring systems  RLC systems  Modified / Improved Euler Methods  RK2 / RK4 Methods  Underdamped / Overdamped / Critically damped