File: compareEuRK4.py

package info (click to toggle)
pycode-browser 1%3A1.03-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,860 kB
  • sloc: python: 2,919; xml: 152; makefile: 76
file content (24 lines) | stat: -rwxr-xr-x 626 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
Sine function is calculated using its derivative, cosine.
Using Euler and RK4 method, with
'''
import math

def rk4(x, y, yprime, dx = 0.01):   # x, y , derivative, stepsize
	k1 = dx * yprime(x)
	k2 = dx * yprime(x + dx/2.0)
	k3 = dx * yprime(x + dx/2.0)
	k4 = dx * yprime(x + dx)
	return y + ( k1/6 + k2/3 + k3/3 + k4/6 )

h = 0.01    # stepsize
x = 0.0     # initail values
y = 0.0     # for Euler
z = 0.0     # for RK4

while x < math.pi:
     print( x, y - math.sin(x), z - math.sin(x)) # errors
     y = y + h * math.cos(x)       # Euler method
     z = rk4(x,z,math.cos,h)       # Runge-Kutta method
     x = x + h