Skip to content

gs18004/RK4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Python을 이용한 RK4 구현

Ch 2.4 예제 4에 초기값 조건을 추가한 미분방정식

equation

직접 해를 구하면,

equation

Python 코드

다음은 RK4를 이용하여 구한 값과 실제 해에 대입한 값을 구하는 Python 코드이다. (h = 0.05, 0부터 20까지 계산)

import decimal
import math


def f(X, Y):
    return decimal.Decimal((-X * Y) / (X * X - 9))


def Y(X):
    return decimal.Decimal(4 / (math.sqrt(X * X - 9)))


h = decimal.Decimal('0.05')
x = []
y = []
x.append(decimal.Decimal('5.00'))
y.append(decimal.Decimal('1.00'))


for i in range(0, 21):

    if i >= 1:
        k1 = f(x[i - 1], y[i - 1])
        k2 = f(x[i - 1] + h / 2, y[i - 1] + h * k1 / 2)
        k3 = f(x[i - 1] + h / 2, y[i - 1] + h * k2 / 2)
        k4 = f(x[i - 1] + h, y[i - 1] + h * k3)

        x.append(x[i - 1] + h)
        y.append(y[i - 1] + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6)

    print("x{} =".format(i), x[i], "  RK4 y{} =".format(i), "%.10f" % y[i], end="")
    print("   참값 y{} =".format(i), "%.10f" % Y(x[i]), end="")
    print("   절대오차 {} =".format(i), "%.10f" % abs(y[i] - Y(x[i])), end="")
    print("   상대오차 {} =".format(i), "%.10f" % (abs(y[i] - Y(x[i])) * 100 / abs(Y(x[i]))), end="")
    print("%")

코드 실행결과

x0 = 5.00   RK4 y0 = 1.0000000000   참값 y0 = 1.0000000000   절대오차 0 = 0.0000000000   상대오차 0 = 0.0000000000%
x1 = 5.05   RK4 y1 = 0.9846573354   참값 y1 = 0.9846573354   절대오차 1 = 0.0000000000   상대오차 1 = 0.0000000031%
x2 = 5.10   RK4 y2 = 0.9698572899   참값 y2 = 0.9698572899   절대오차 2 = 0.0000000001   상대오차 2 = 0.0000000059%
x3 = 5.15   RK4 y3 = 0.9555687905   참값 y3 = 0.9555687906   절대오차 3 = 0.0000000001   상대오차 3 = 0.0000000083%
x4 = 5.20   RK4 y4 = 0.9417632186   참값 y4 = 0.9417632187   절대오차 4 = 0.0000000001   상대오차 4 = 0.0000000104%
x5 = 5.25   RK4 y5 = 0.9284141650   참값 y5 = 0.9284141651   절대오차 5 = 0.0000000001   상대오차 5 = 0.0000000123%
x6 = 5.30   RK4 y6 = 0.9154972141   참값 y6 = 0.9154972142   절대오차 6 = 0.0000000001   상대오차 6 = 0.0000000140%
x7 = 5.35   RK4 y7 = 0.9029897537   참값 y7 = 0.9029897539   절대오차 7 = 0.0000000001   상대오차 7 = 0.0000000155%
x8 = 5.40   RK4 y8 = 0.8908708062   참값 y8 = 0.8908708064   절대오차 8 = 0.0000000002   상대오차 8 = 0.0000000169%
x9 = 5.45   RK4 y9 = 0.8791208790   참값 y9 = 0.8791208791   절대오차 9 = 0.0000000002   상대오차 9 = 0.0000000181%
x10 = 5.50   RK4 y10 = 0.8677218311   참값 y10 = 0.8677218313   절대오차 10 = 0.0000000002   상대오차 10 = 0.0000000192%
x11 = 5.55   RK4 y11 = 0.8566567548   참값 y11 = 0.8566567549   절대오차 11 = 0.0000000002   상대오차 11 = 0.0000000202%
x12 = 5.60   RK4 y12 = 0.8459098687   참값 y12 = 0.8459098689   절대오차 12 = 0.0000000002   상대오차 12 = 0.0000000211%
x13 = 5.65   RK4 y13 = 0.8354664231   참값 y13 = 0.8354664233   절대오차 13 = 0.0000000002   상대오차 13 = 0.0000000219%
x14 = 5.70   RK4 y14 = 0.8253126139   참값 y14 = 0.8253126141   절대오차 14 = 0.0000000002   상대오차 14 = 0.0000000226%
x15 = 5.75   RK4 y15 = 0.8154355061   참값 y15 = 0.8154355063   절대오차 15 = 0.0000000002   상대오차 15 = 0.0000000233%
x16 = 5.80   RK4 y16 = 0.8058229638   참값 y16 = 0.8058229640   절대오차 16 = 0.0000000002   상대오차 16 = 0.0000000239%
x17 = 5.85   RK4 y17 = 0.7964635879   참값 y17 = 0.7964635881   절대오차 17 = 0.0000000002   상대오차 17 = 0.0000000244%
x18 = 5.90   RK4 y18 = 0.7873466590   참값 y18 = 0.7873466592   절대오차 18 = 0.0000000002   상대오차 18 = 0.0000000249%
x19 = 5.95   RK4 y19 = 0.7784620858   참값 y19 = 0.7784620860   절대오차 19 = 0.0000000002   상대오차 19 = 0.0000000254%
x20 = 6.00   RK4 y20 = 0.7698003587   참값 y20 = 0.7698003589   절대오차 20 = 0.0000000002   상대오차 20 = 0.0000000258%

결과표

x y 참값 절대오차 상대오차(%)
5.00 1.0000000000 1.0000000000 0.0000000000 0.0000000000
5.05 0.9846573354 0.9846573354 0.0000000000 0.0000000031
5.10 0.9698572899 0.9698572899 0.0000000001 0.0000000059
5.15 0.9555687905 0.9555687906 0.0000000001 0.0000000083
5.20 0.9417632186 0.9417632187 0.0000000001 0.0000000104
5.25 0.9284141650 0.9284141651 0.0000000001 0.0000000123
5.30 0.9154972141 0.9154972142 0.0000000001 0.0000000140
5.35 0.9029897537 0.9029897539 0.0000000001 0.0000000155
5.40 0.8908708062 0.8908708064 0.0000000002 0.0000000169
5.45 0.8791208790 0.8791208791 0.0000000002 0.0000000181
5.50 0.8677218311 0.8677218313 0.0000000002 0.0000000192
5.55 0.8566567548 0.8566567549 0.0000000002 0.0000000202
5.60 0.8459098687 0.8459098689 0.0000000002 0.0000000211
5.65 0.8354664231 0.8354664233 0.0000000002 0.0000000219
5.70 0.8253126139 0.8253126141 0.0000000002 0.0000000226
5.75 0.8154355061 0.8154355063 0.0000000002 0.0000000233
5.80 0.8058229638 0.8058229640 0.0000000002 0.0000000239
5.85 0.7964635879 0.7964635881 0.0000000002 0.0000000244
5.90 0.7873466590 0.7873466592 0.0000000002 0.0000000249
5.95 0.7784620858 0.7784620860 0.0000000002 0.0000000254
6.00 0.7698003587 0.7698003589 0.0000000002 0.0000000258

About

Runge-Kutta 4th Order Method with Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages