본문 바로가기
Python/Python Examples

[Python Example] 모델링을 위한 데이터 분석 방법 Analytical solution, simple Euler integration, improved Euler integration, Runge-Kutta 4th order: #matplotlib, #for_loop, #numpy

by Henry Cho 2023. 9. 21.
728x90

모델링을 위한 데이터 분석 방법 Analytical solution, simple Euler integration, improved Euler integration, Runge-kutta 4th order: #matplotlib, #for_loop, #numpy

포스트 난이도: HOO_Middle


# 계산식에 따른 산출값의 변화

Timeseries의 데이터를 분석하여 모델링을 하기 위해서는 비정형 데이터를 정형화하는 작업이 필요하다. 그중에 가장 기본적으로 데이터를 분석하는 방식이 연속성 데이터를 분석할 수 있는 계산식을 통해서 결괏값을 산출해 내는 것이다. 대표적인 계산식으로는 Analytical solution, Simple Euler integration, Improved Euler integration, Runge-Kutta 4th order (RK4) 등이 있다. 이 외에도 알고리즘 구성을 어떻게 하느냐에 따라서 실제 값과 산출 값의 오차가 달라질 수 있다. 이번 포스트에서는 각 계산식의 오차가 어떻게 발생하고 어떤 차이점을 가지는 지를 비교해 보기 위해 작성되었다. 실제 값과 모델링 된 값의 차이는 항상 발생할 수밖에 없고 모델링 된 값들 사이에서도 차이가 발생할 수 있다는 점을 아래의 예제코드들을 통해 살펴보도록 하자.


# Codes of Analytical solution

아래의 코드는 Analytical solution을 통해서 얻어진 결과를 보여주고 있다. 또한 테이블에 나온 산출값과 그래프를 통해서 다른 계산식의 산출값과도 비교해볼 수 있다.


#analytical solution
import numpy as np
import matplotlib.pyplot as plt

# Constants
N0 = 10
k = 0.10
t_values = np.arange(0, 31, 0.1)

# Analytical solution
N_analytical = (N0 - 0) * np.exp(k * t_values) + 0

# Plotting
for t, N_t in zip(t_values, N_analytical):
  print("time: " ,t, "/ N_t: " ,N_t)

plt.plot(t_values, N_analytical, label="Analytical Solution")
plt.xlabel("Time (t)")
plt.ylabel("Population (N)")
plt.legend()
plt.show()

더보기

time: 0.0 / N_t: 10.0 time: 0.1 / N_t: 10.10050167084168 time: 0.2 / N_t: 10.202013400267557 time: 0.30000000000000004 / N_t: 10.304545339535169 time: 0.4 / N_t: 10.408107741923882 time: 0.5 / N_t: 10.512710963760242 time: 0.6000000000000001 / N_t: 10.618365465453596 time: 0.7000000000000001 / N_t: 10.725081812542165 time: 0.8 / N_t: 10.832870676749586 time: 0.9 / N_t: 10.941742837052104 time: 1.0 / N_t: 11.051709180756477 time: 1.1 / N_t: 11.162780704588712 time: 1.2000000000000002 / N_t: 11.274968515793757 time: 1.3 / N_t: 11.388283833246218 time: 1.4000000000000001 / N_t: 11.502737988572274 time: 1.5 / N_t: 11.61834242728283 time: 1.6 / N_t: 11.735108709918103 time: 1.7000000000000002 / N_t: 11.853048513203657 time: 1.8 / N_t: 11.972173631218102 time: 1.9000000000000001 / N_t: 12.092495976572515 time: 2.0 / N_t: 12.214027581601698 time: 2.1 / N_t: 12.336780599567431 time: 2.2 / N_t: 12.460767305873809 time: 2.3000000000000003 / N_t: 12.586000099294779 time: 2.4000000000000004 / N_t: 12.712491503214046 time: 2.5 / N_t: 12.840254166877415 time: 2.6 / N_t: 12.969300866657719 time: 2.7 / N_t: 13.099644507332473 time: 2.8000000000000003 / N_t: 13.23129812337437 time: 2.9000000000000004 / N_t: 13.36427488025472 time: 3.0 / N_t: 13.498588075760033 time: 3.1 / N_t: 13.634251141321778 time: 3.2 / N_t: 13.771277643359571 time: 3.3000000000000003 / N_t: 13.909681284637802 time: 3.4000000000000004 / N_t: 14.04947590563594 time: 3.5 / N_t: 14.190675485932573 time: 3.6 / N_t: 14.333294145603404 time: 3.7 / N_t: 14.477346146633245 time: 3.8000000000000003 / N_t: 14.622845894342246 time: 3.9000000000000004 / N_t: 14.769807938826427 time: 4.0 / N_t: 14.918246976412703 time: 4.1000000000000005 / N_t: 15.068177851128537 time: 4.2 / N_t: 15.219615556186339 time: 4.3 / N_t: 15.372575235482815 time: 4.4 / N_t: 15.527072185113362 time: 4.5 / N_t: 15.683121854901689 time: 4.6000000000000005 / N_t: 15.840739849944818 time: 4.7 / N_t: 15.999941932173602 time: 4.800000000000001 / N_t: 16.160744021928934 time: 4.9 / N_t: 16.323162199553792 time: 5.0 / N_t: 16.487212707001284 time: 5.1000000000000005 / N_t: 16.652911949458865 time: 5.2 / N_t: 16.820276496988864 time: 5.300000000000001 / N_t: 16.989323086185507 time: 5.4 / N_t: 17.160068621848584 time: 5.5 / N_t: 17.332530178673952 time: 5.6000000000000005 / N_t: 17.506725002961012 time: 5.7 / N_t: 17.682670514337353 time: 5.800000000000001 / N_t: 17.860384307500734 time: 5.9 / N_t: 18.039884153978573 time: 6.0 / N_t: 18.22118800390509 time: 6.1000000000000005 / N_t: 18.404313987816376 time: 6.2 / N_t: 18.589280418463424 time: 6.300000000000001 / N_t: 18.776105792643435 time: 6.4 / N_t: 18.964808793049517 time: 6.5 / N_t: 19.15540829013896 time: 6.6000000000000005 / N_t: 19.347923344020316 time: 6.7 / N_t: 19.542373206359397 time: 6.800000000000001 / N_t: 19.73877732230448 time: 6.9 / N_t: 19.937155332430827 time: 7.0 / N_t: 20.137527074704767 time: 7.1000000000000005 / N_t: 20.339912586467506 time: 7.2 / N_t: 20.54433210643888 time: 7.300000000000001 / N_t: 20.750806076741227 time: 7.4 / N_t: 20.959355144943647 time: 7.5 / N_t: 21.170000166126748 time: 7.6000000000000005 / N_t: 21.382762204968188 time: 7.7 / N_t: 21.597662537849153 time: 7.800000000000001 / N_t: 21.814722654982013 time: 7.9 / N_t: 22.03396426255937 time: 8.0 / N_t: 22.255409284924678 time: 8.1 / N_t: 22.479079866764717 time: 8.200000000000001 / N_t: 22.70499837532406 time: 8.3 / N_t: 22.93318740264183 time: 8.4 / N_t: 23.16366976781092 time: 8.5 / N_t: 23.396468519259912 time: 8.6 / N_t: 23.631606937057946 time: 8.700000000000001 / N_t: 23.86910853524277 time: 8.8 / N_t: 24.1089970641721 time: 8.9 / N_t: 24.35129651289875 time: 9.0 / N_t: 24.5960311115695 time: 9.1 / N_t: 24.843225333848164 time: 9.200000000000001 / N_t: 25.09290389936298 time: 9.3 / N_t: 25.34509177617855 time: 9.4 / N_t: 25.599814183292718 time: 9.5 / N_t: 25.857096593158463 time: 9.600000000000001 / N_t: 26.116964734231182 time: 9.700000000000001 / N_t: 26.37944459354153 time: 9.8 / N_t: 26.644562419294175 time: 9.9 / N_t: 26.912344723492623 time: 10.0 / N_t: 27.18281828459045 time: 10.100000000000001 / N_t: 27.45601015016917 time: 10.200000000000001 / N_t: 27.731947639642986 time: 10.3 / N_t: 28.01065834699079 time: 10.4 / N_t: 28.292170143515598 time: 10.5 / N_t: 28.576511180631638 time: 10.600000000000001 / N_t: 28.86370989267959 time: 10.700000000000001 / N_t: 29.153794999769968 time: 10.8 / N_t: 29.446795510655242 time: 10.9 / N_t: 29.742740725630657 time: 11.0 / N_t: 30.041660239464335 time: 11.100000000000001 / N_t: 30.34358394435676 time: 11.200000000000001 / N_t: 30.648542032930024 time: 11.3 / N_t: 30.956565001247117 time: 11.4 / N_t: 31.26768365186156 time: 11.5 / N_t: 31.58192909689768 time: 11.600000000000001 / N_t: 31.89933276116185 time: 11.700000000000001 / N_t: 32.21992638528501 time: 11.8 / N_t: 32.54374202889671 time: 11.9 / N_t: 32.87081207383119 time: 12.0 / N_t: 33.20116922736548 time: 12.100000000000001 / N_t: 33.534846525490245 time: 12.200000000000001 / N_t: 33.87187733621335 time: 12.3 / N_t: 34.21229536289674 time: 12.4 / N_t: 34.55613464762676 time: 12.5 / N_t: 34.903429574618414 time: 12.600000000000001 / N_t: 35.25421487365383 time: 12.700000000000001 / N_t: 35.60852562355522 time: 12.8 / N_t: 35.966397255692826 time: 12.9 / N_t: 36.32786555752809 time: 13.0 / N_t: 36.69296667619244 time: 13.100000000000001 / N_t: 37.061737122102 time: 13.200000000000001 / N_t: 37.434213772608636 time: 13.3 / N_t: 37.81043387568781 time: 13.4 / N_t: 38.190435053663364 time: 13.5 / N_t: 38.57425530696975 time: 13.600000000000001 / N_t: 38.961933017952155 time: 13.700000000000001 / N_t: 39.353506954704734 time: 13.8 / N_t: 39.749016274947486 time: 13.9 / N_t: 40.14850052994202 time: 14.0 / N_t: 40.551999668446754 time: 14.100000000000001 / N_t: 40.959554040711765 time: 14.200000000000001 / N_t: 41.37120440251393 time: 14.3 / N_t: 41.78699191923247 time: 14.4 / N_t: 42.20695816996553 time: 14.5 / N_t: 42.63114515168819 time: 14.600000000000001 / N_t: 43.05959528345207 time: 14.700000000000001 / N_t: 43.49235141062742 time: 14.8 / N_t: 43.92945680918758 time: 14.9 / N_t: 44.37095519003665 time: 15.0 / N_t: 44.81689070338064 time: 15.100000000000001 / N_t: 45.267307943142534 time: 15.200000000000001 / N_t: 45.722251951421605 time: 15.3 / N_t: 46.18176822299782 time: 15.4 / N_t: 46.645902709881256 time: 15.5 / N_t: 47.11470182590742 time: 15.600000000000001 / N_t: 47.58821245137855 time: 15.700000000000001 / N_t: 48.0664819377518 time: 15.8 / N_t: 48.54955811237434 time: 15.9 / N_t: 49.037489283266225 time: 16.0 / N_t: 49.53032424395115 time: 16.1 / N_t: 50.0281122783359 time: 16.2 / N_t: 50.53090316563868 time: 16.3 / N_t: 51.038747185367264 time: 16.400000000000002 / N_t: 51.55169512234683 time: 16.5 / N_t: 52.0697982717985 time: 16.6 / N_t: 52.593108444468996 time: 16.7 / N_t: 53.12167797181167 time: 16.8 / N_t: 53.65555971121975 time: 16.900000000000002 / N_t: 54.19480705131208 time: 17.0 / N_t: 54.73947391727201 time: 17.1 / N_t: 55.28961477624005 time: 17.2 / N_t: 55.84528464276054 time: 17.3 / N_t: 56.406539084283224 time: 17.400000000000002 / N_t: 56.973434226719924 time: 17.5 / N_t: 57.54602676005731 time: 17.6 / N_t: 58.1243739440259 time: 17.7 / N_t: 58.70853361382601 time: 17.8 / N_t: 59.298564185911474 time: 17.900000000000002 / N_t: 59.89452466383115 time: 18.0 / N_t: 60.496474644129464 time: 18.1 / N_t: 61.10447432230612 time: 18.2 / N_t: 61.71858449883554 time: 18.3 / N_t: 62.338866585247175 time: 18.400000000000002 / N_t: 62.96538261026659 time: 18.5 / N_t: 63.59819522601832 time: 18.6 / N_t: 64.23736771429137 time: 18.7 / N_t: 64.88296399286712 time: 18.8 / N_t: 65.53504862191149 time: 18.900000000000002 / N_t: 66.19368681043079 time: 19.0 / N_t: 66.8589444227927 time: 19.1 / N_t: 67.53088798531287 time: 19.200000000000003 / N_t: 68.20958469290753 time: 19.3 / N_t: 68.89510241581294 time: 19.400000000000002 / N_t: 69.58750970637274 time: 19.5 / N_t: 70.28687580589295 time: 19.6 / N_t: 70.99327065156633 time: 19.700000000000003 / N_t: 71.70676488346616 time: 19.8 / N_t: 72.42742985161014 time: 19.900000000000002 / N_t: 73.15533762309568 time: 20.0 / N_t: 73.89056098930651 time: 20.1 / N_t: 74.63317347319196 time: 20.200000000000003 / N_t: 75.38324933661926 time: 20.3 / N_t: 76.14086358779977 time: 20.400000000000002 / N_t: 76.90609198879001 time: 20.5 / N_t: 77.67901106306775 time: 20.6 / N_t: 78.4596981031845 time: 20.700000000000003 / N_t: 79.2482311784949 time: 20.8 / N_t: 80.04468914296353 time: 20.900000000000002 / N_t: 80.84915164305062 time: 21.0 / N_t: 81.66169912567652 time: 21.1 / N_t: 82.48241284626664 time: 21.200000000000003 / N_t: 83.31137487687697 time: 21.3 / N_t: 84.14866811440133 time: 21.400000000000002 / N_t: 84.99437628886125 time: 21.5 / N_t: 85.84858397177894 time: 21.6 / N_t: 86.71137658463456 time: 21.700000000000003 / N_t: 87.58284040740838 time: 21.8 / N_t: 88.46306258720884 time: 21.900000000000002 / N_t: 89.35213114698753 time: 22.0 / N_t: 90.25013499434122 time: 22.1 / N_t: 91.1571639304031 time: 22.200000000000003 / N_t: 92.07330865882253 time: 22.3 / N_t: 92.99866079483586 time: 22.400000000000002 / N_t: 93.93331287442784 time: 22.5 / N_t: 94.87735836358526 time: 22.6 / N_t: 95.8308916676438 time: 22.700000000000003 / N_t: 96.79400814072847 time: 22.8 / N_t: 97.76680409528907 time: 22.900000000000002 / N_t: 98.74937681173188 time: 23.0 / N_t: 99.74182454814724 time: 23.1 / N_t: 100.74424655013587 time: 23.200000000000003 / N_t: 101.75674306073338 time: 23.3 / N_t: 102.77941533043447 time: 23.400000000000002 / N_t: 103.81236562731848 time: 23.5 / N_t: 104.85569724727576 time: 23.6 / N_t: 105.90951452433784 time: 23.700000000000003 / N_t: 106.97392284111059 time: 23.8 / N_t: 108.04902863931262 time: 23.900000000000002 / N_t: 109.13493943041976 time: 24.0 / N_t: 110.23176380641604 time: 24.1 / N_t: 111.33961145065307 time: 24.200000000000003 / N_t: 112.4585931488185 time: 24.3 / N_t: 113.58882080001457 time: 24.400000000000002 / N_t: 114.73040742794838 time: 24.5 / N_t: 115.88346719223392 time: 24.6 / N_t: 117.0481153998086 time: 24.700000000000003 / N_t: 118.22446851646369 time: 24.8 / N_t: 119.41264417849109 time: 24.900000000000002 / N_t: 120.6127612044472 time: 25.0 / N_t: 121.82493960703474 time: 25.1 / N_t: 123.04930060510415 time: 25.200000000000003 / N_t: 124.2859666357755 time: 25.3 / N_t: 125.53506136668234 time: 25.400000000000002 / N_t: 126.79670970833882 time: 25.5 / N_t: 128.07103782663037 time: 25.6 / N_t: 129.35817315543085 time: 25.700000000000003 / N_t: 130.65824440934563 time: 25.8 / N_t: 131.97138159658357 time: 25.900000000000002 / N_t: 133.29771603195778 time: 26.0 / N_t: 134.6373803500169 time: 26.1 / N_t: 135.9905085183093 time: 26.200000000000003 / N_t: 137.35723585077932 time: 26.3 / N_t: 138.7376990212991 time: 26.400000000000002 / N_t: 140.1320360773362 time: 26.5 / N_t: 141.5403864537581 time: 26.6 / N_t: 142.96289098677602 time: 26.700000000000003 / N_t: 144.39969192802886 time: 26.8 / N_t: 145.85093295880793 time: 26.900000000000002 / N_t: 147.31675920442575 time: 27.0 / N_t: 148.79731724872838 time: 27.1 / N_t: 150.29275514875408 time: 27.200000000000003 / N_t: 151.80322244953908 time: 27.3 / N_t: 153.328870199072 time: 27.400000000000002 / N_t: 154.8698509633994 time: 27.5 / N_t: 156.4263188418817 time: 27.6 / N_t: 157.99842948260402 time: 27.700000000000003 / N_t: 159.58634009794037 time: 27.8 / N_t: 161.1902094802755 time: 27.900000000000002 / N_t: 162.81019801788435 time: 28.0 / N_t: 164.44646771097055 time: 28.1 / N_t: 166.09918218786706 time: 28.200000000000003 / N_t: 167.7685067213988 time: 28.3 / N_t: 169.45460824541018 time: 28.400000000000002 / N_t: 171.15765537145882 time: 28.5 / N_t: 172.8778184056764 time: 28.6 / N_t: 174.61526936579997 time: 28.700000000000003 / N_t: 176.3701819983733 time: 28.8 / N_t: 178.14273179612204 time: 28.900000000000002 / N_t: 179.93309601550325 time: 29.0 / N_t: 181.74145369443067 time: 29.1 / N_t: 183.56798567017927 time: 29.200000000000003 / N_t: 185.41287459746877 time: 29.3 / N_t: 187.27630496672919 time: 29.400000000000002 / N_t: 189.15846312255047 time: 29.5 / N_t: 191.0595372823165 time: 29.6 / N_t: 192.9797175550277 time: 29.700000000000003 / N_t: 194.9191959603113 time: 29.8 / N_t: 196.87816644762407 time: 29.900000000000002 / N_t: 198.85682491564728 time: 30.0 / N_t: 200.8553692318767 time: 30.1 / N_t: 202.87399925240936 time: 30.200000000000003 / N_t: 204.91291684192953 time: 30.3 / N_t: 206.97232589389506 time: 30.400000000000002 / N_t: 209.05243235092766 time: 30.5 / N_t: 211.15344422540616 time: 30.6 / N_t: 213.27557162026915 time: 30.700000000000003 / N_t: 215.41902675002422 time: 30.8 / N_t: 217.58402396197081 time: 30.900000000000002 / N_t: 219.77077975763427


Firgure1. Analytical solution graph


https://github.com/WhoisHOO/HOOAI/blob/main/Python%20Examples/analytical_solution


# Codes of simple Euler integration

아래의 코드는 Simple Euler integration을 통해서 얻어진 결과를 보여주고 있다. 또한 테이블에 나온 산출값과 그래프를 통해서 다른 계산식의 산출값과도 비교해 볼 수 있다.


#the simple Euler integration
import numpy as np
import matplotlib.pyplot as plt

# Constants
N0 = 10
k = 0.10
t_values = np.arange(0, 31, 0.1)
delta_t = 0.1

# Simple Euler Integration
N_simple_euler = [N0]
for t in t_values[1:]:
    N_next = N_simple_euler[-1] + delta_t * k * N_simple_euler[-1]
    N_simple_euler.append(N_next)

#Table values
for t, N_t in zip(t_values, N_simple_euler):
  print("time: " ,t, "/ N_t: " ,N_t)

# Plotting
plt.plot(t_values, N_simple_euler, label="Simple Euler Integration")
plt.xlabel("Time (t)")
plt.ylabel("Population (N)")
plt.legend()
plt.show()

더보기

time: 0.0 / N_t: 10 time: 0.1 / N_t: 10.1 time: 0.2 / N_t: 10.201 time: 0.30000000000000004 / N_t: 10.30301 time: 0.4 / N_t: 10.4060401 time: 0.5 / N_t: 10.510100501 time: 0.6000000000000001 / N_t: 10.615201506010001 time: 0.7000000000000001 / N_t: 10.721353521070101 time: 0.8 / N_t: 10.828567056280802 time: 0.9 / N_t: 10.93685272684361 time: 1.0 / N_t: 11.046221254112046 time: 1.1 / N_t: 11.156683466653167 time: 1.2000000000000002 / N_t: 11.268250301319698 time: 1.3 / N_t: 11.380932804332895 time: 1.4000000000000001 / N_t: 11.494742132376224 time: 1.5 / N_t: 11.609689553699987 time: 1.6 / N_t: 11.725786449236987 time: 1.7000000000000002 / N_t: 11.843044313729356 time: 1.8 / N_t: 11.96147475686665 time: 1.9000000000000001 / N_t: 12.081089504435317 time: 2.0 / N_t: 12.20190039947967 time: 2.1 / N_t: 12.323919403474468 time: 2.2 / N_t: 12.447158597509214 time: 2.3000000000000003 / N_t: 12.571630183484306 time: 2.4000000000000004 / N_t: 12.69734648531915 time: 2.5 / N_t: 12.824319950172342 time: 2.6 / N_t: 12.952563149674065 time: 2.7 / N_t: 13.082088781170805 time: 2.8000000000000003 / N_t: 13.212909668982514 time: 2.9000000000000004 / N_t: 13.345038765672339 time: 3.0 / N_t: 13.478489153329061 time: 3.1 / N_t: 13.613274044862353 time: 3.2 / N_t: 13.749406785310976 time: 3.3000000000000003 / N_t: 13.886900853164086 time: 3.4000000000000004 / N_t: 14.025769861695727 time: 3.5 / N_t: 14.166027560312685 time: 3.6 / N_t: 14.30768783591581 time: 3.7 / N_t: 14.45076471427497 time: 3.8000000000000003 / N_t: 14.595272361417718 time: 3.9000000000000004 / N_t: 14.741225085031896 time: 4.0 / N_t: 14.888637335882215 time: 4.1000000000000005 / N_t: 15.037523709241038 time: 4.2 / N_t: 15.187898946333448 time: 4.3 / N_t: 15.339777935796782 time: 4.4 / N_t: 15.49317571515475 time: 4.5 / N_t: 15.648107472306299 time: 4.6000000000000005 / N_t: 15.804588547029361 time: 4.7 / N_t: 15.962634432499655 time: 4.800000000000001 / N_t: 16.122260776824653 time: 4.9 / N_t: 16.2834833845929 time: 5.0 / N_t: 16.446318218438826 time: 5.1000000000000005 / N_t: 16.610781400623214 time: 5.2 / N_t: 16.776889214629445 time: 5.300000000000001 / N_t: 16.94465810677574 time: 5.4 / N_t: 17.114104687843497 time: 5.5 / N_t: 17.285245734721933 time: 5.6000000000000005 / N_t: 17.45809819206915 time: 5.7 / N_t: 17.632679173989843 time: 5.800000000000001 / N_t: 17.809005965729742 time: 5.9 / N_t: 17.98709602538704 time: 6.0 / N_t: 18.16696698564091 time: 6.1000000000000005 / N_t: 18.34863665549732 time: 6.2 / N_t: 18.532123022052293 time: 6.300000000000001 / N_t: 18.717444252272816 time: 6.4 / N_t: 18.904618694795545 time: 6.5 / N_t: 19.0936648817435 time: 6.6000000000000005 / N_t: 19.284601530560934 time: 6.7 / N_t: 19.477447545866543 time: 6.800000000000001 / N_t: 19.672222021325208 time: 6.9 / N_t: 19.86894424153846 time: 7.0 / N_t: 20.067633683953847 time: 7.1000000000000005 / N_t: 20.268310020793386 time: 7.2 / N_t: 20.47099312100132 time: 7.300000000000001 / N_t: 20.675703052211333 time: 7.4 / N_t: 20.882460082733445 time: 7.5 / N_t: 21.09128468356078 time: 7.6000000000000005 / N_t: 21.302197530396388 time: 7.7 / N_t: 21.51521950570035 time: 7.800000000000001 / N_t: 21.730371700757356 time: 7.9 / N_t: 21.94767541776493 time: 8.0 / N_t: 22.16715217194258 time: 8.1 / N_t: 22.388823693662005 time: 8.200000000000001 / N_t: 22.612711930598625 time: 8.3 / N_t: 22.83883904990461 time: 8.4 / N_t: 23.067227440403656 time: 8.5 / N_t: 23.29789971480769 time: 8.6 / N_t: 23.53087871195577 time: 8.700000000000001 / N_t: 23.766187499075325 time: 8.8 / N_t: 24.00384937406608 time: 8.9 / N_t: 24.243887867806738 time: 9.0 / N_t: 24.486326746484806 time: 9.1 / N_t: 24.731190013949654 time: 9.200000000000001 / N_t: 24.97850191408915 time: 9.3 / N_t: 25.22828693323004 time: 9.4 / N_t: 25.480569802562343 time: 9.5 / N_t: 25.735375500587967 time: 9.600000000000001 / N_t: 25.992729255593847 time: 9.700000000000001 / N_t: 26.252656548149787 time: 9.8 / N_t: 26.515183113631284 time: 9.9 / N_t: 26.780334944767596 time: 10.0 / N_t: 27.04813829421527 time: 10.100000000000001 / N_t: 27.31861967715742 time: 10.200000000000001 / N_t: 27.591805873928998 time: 10.3 / N_t: 27.867723932668287 time: 10.4 / N_t: 28.14640117199497 time: 10.5 / N_t: 28.42786518371492 time: 10.600000000000001 / N_t: 28.71214383555207 time: 10.700000000000001 / N_t: 28.99926527390759 time: 10.8 / N_t: 29.289257926646666 time: 10.9 / N_t: 29.582150505913134 time: 11.0 / N_t: 29.877972010972265 time: 11.100000000000001 / N_t: 30.17675173108199 time: 11.200000000000001 / N_t: 30.47851924839281 time: 11.3 / N_t: 30.783304440876737 time: 11.4 / N_t: 31.091137485285504 time: 11.5 / N_t: 31.402048860138358 time: 11.600000000000001 / N_t: 31.71606934873974 time: 11.700000000000001 / N_t: 32.03323004222714 time: 11.8 / N_t: 32.35356234264941 time: 11.9 / N_t: 32.677097966075905 time: 12.0 / N_t: 33.00386894573666 time: 12.100000000000001 / N_t: 33.33390763519403 time: 12.200000000000001 / N_t: 33.66724671154597 time: 12.3 / N_t: 34.003919178661434 time: 12.4 / N_t: 34.34395837044805 time: 12.5 / N_t: 34.68739795415253 time: 12.600000000000001 / N_t: 35.03427193369406 time: 12.700000000000001 / N_t: 35.384614653031 time: 12.8 / N_t: 35.73846079956131 time: 12.9 / N_t: 36.09584540755692 time: 13.0 / N_t: 36.456803861632494 time: 13.100000000000001 / N_t: 36.82137190024882 time: 13.200000000000001 / N_t: 37.18958561925131 time: 13.3 / N_t: 37.56148147544383 time: 13.4 / N_t: 37.93709629019826 time: 13.5 / N_t: 38.31646725310024 time: 13.600000000000001 / N_t: 38.699631925631245 time: 13.700000000000001 / N_t: 39.08662824488756 time: 13.8 / N_t: 39.47749452733643 time: 13.9 / N_t: 39.872269472609794 time: 14.0 / N_t: 40.27099216733589 time: 14.100000000000001 / N_t: 40.67370208900925 time: 14.200000000000001 / N_t: 41.08043910989934 time: 14.3 / N_t: 41.49124350099833 time: 14.4 / N_t: 41.90615593600831 time: 14.5 / N_t: 42.32521749536839 time: 14.600000000000001 / N_t: 42.74846967032207 time: 14.700000000000001 / N_t: 43.17595436702529 time: 14.8 / N_t: 43.607713910695544 time: 14.9 / N_t: 44.0437910498025 time: 15.0 / N_t: 44.48422896030053 time: 15.100000000000001 / N_t: 44.92907124990353 time: 15.200000000000001 / N_t: 45.378361962402565 time: 15.3 / N_t: 45.83214558202659 time: 15.4 / N_t: 46.29046703784686 time: 15.5 / N_t: 46.75337170822533 time: 15.600000000000001 / N_t: 47.220905425307585 time: 15.700000000000001 / N_t: 47.69311447956066 time: 15.8 / N_t: 48.17004562435627 time: 15.9 / N_t: 48.651746080599835 time: 16.0 / N_t: 49.138263541405834 time: 16.1 / N_t: 49.62964617681989 time: 16.2 / N_t: 50.125942638588086 time: 16.3 / N_t: 50.627202064973964 time: 16.400000000000002 / N_t: 51.1334740856237 time: 16.5 / N_t: 51.64480882647994 time: 16.6 / N_t: 52.16125691474474 time: 16.7 / N_t: 52.68286948389219 time: 16.8 / N_t: 53.20969817873111 time: 16.900000000000002 / N_t: 53.74179516051842 time: 17.0 / N_t: 54.27921311212361 time: 17.1 / N_t: 54.822005243244845 time: 17.2 / N_t: 55.370225295677294 time: 17.3 / N_t: 55.92392754863407 time: 17.400000000000002 / N_t: 56.48316682412041 time: 17.5 / N_t: 57.047998492361614 time: 17.6 / N_t: 57.61847847728523 time: 17.7 / N_t: 58.19466326205808 time: 17.8 / N_t: 58.77660989467866 time: 17.900000000000002 / N_t: 59.364375993625444 time: 18.0 / N_t: 59.9580197535617 time: 18.1 / N_t: 60.557599951097316 time: 18.2 / N_t: 61.16317595060829 time: 18.3 / N_t: 61.77480771011437 time: 18.400000000000002 / N_t: 62.392555787215514 time: 18.5 / N_t: 63.01648134508767 time: 18.6 / N_t: 63.646646158538545 time: 18.7 / N_t: 64.28311262012393 time: 18.8 / N_t: 64.92594374632517 time: 18.900000000000002 / N_t: 65.57520318378843 time: 19.0 / N_t: 66.23095521562631 time: 19.1 / N_t: 66.89326476778257 time: 19.200000000000003 / N_t: 67.56219741546039 time: 19.3 / N_t: 68.237819389615 time: 19.400000000000002 / N_t: 68.92019758351115 time: 19.5 / N_t: 69.60939955934627 time: 19.6 / N_t: 70.30549355493973 time: 19.700000000000003 / N_t: 71.00854849048913 time: 19.8 / N_t: 71.71863397539403 time: 19.900000000000002 / N_t: 72.43582031514796 time: 20.0 / N_t: 73.16017851829945 time: 20.1 / N_t: 73.89178030348243 time: 20.200000000000003 / N_t: 74.63069810651726 time: 20.3 / N_t: 75.37700508758243 time: 20.400000000000002 / N_t: 76.13077513845826 time: 20.5 / N_t: 76.89208288984284 time: 20.6 / N_t: 77.66100371874127 time: 20.700000000000003 / N_t: 78.43761375592868 time: 20.8 / N_t: 79.22198989348797 time: 20.900000000000002 / N_t: 80.01420979242285 time: 21.0 / N_t: 80.81435189034707 time: 21.1 / N_t: 81.62249540925055 time: 21.200000000000003 / N_t: 82.43872036334305 time: 21.3 / N_t: 83.26310756697649 time: 21.400000000000002 / N_t: 84.09573864264625 time: 21.5 / N_t: 84.93669602907272 time: 21.6 / N_t: 85.78606298936344 time: 21.700000000000003 / N_t: 86.64392361925708 time: 21.8 / N_t: 87.51036285544966 time: 21.900000000000002 / N_t: 88.38546648400416 time: 22.0 / N_t: 89.2693211488442 time: 22.1 / N_t: 90.16201436033263 time: 22.200000000000003 / N_t: 91.06363450393596 time: 22.3 / N_t: 91.97427084897532 time: 22.400000000000002 / N_t: 92.89401355746507 time: 22.5 / N_t: 93.82295369303972 time: 22.6 / N_t: 94.76118322997011 time: 22.700000000000003 / N_t: 95.70879506226981 time: 22.8 / N_t: 96.66588301289251 time: 22.900000000000002 / N_t: 97.63254184302143 time: 23.0 / N_t: 98.60886726145165 time: 23.1 / N_t: 99.59495593406616 time: 23.200000000000003 / N_t: 100.59090549340682 time: 23.3 / N_t: 101.59681454834089 time: 23.400000000000002 / N_t: 102.6127826938243 time: 23.5 / N_t: 103.63891052076255 time: 23.6 / N_t: 104.67529962597018 time: 23.700000000000003 / N_t: 105.72205262222988 time: 23.8 / N_t: 106.77927314845218 time: 23.900000000000002 / N_t: 107.8470658799367 time: 24.0 / N_t: 108.92553653873605 time: 24.1 / N_t: 110.01479190412341 time: 24.200000000000003 / N_t: 111.11493982316465 time: 24.3 / N_t: 112.2260892213963 time: 24.400000000000002 / N_t: 113.34835011361025 time: 24.5 / N_t: 114.48183361474635 time: 24.6 / N_t: 115.62665195089382 time: 24.700000000000003 / N_t: 116.78291847040275 time: 24.8 / N_t: 117.95074765510678 time: 24.900000000000002 / N_t: 119.13025513165785 time: 25.0 / N_t: 120.32155768297443 time: 25.1 / N_t: 121.52477325980418 time: 25.200000000000003 / N_t: 122.74002099240222 time: 25.3 / N_t: 123.96742120232625 time: 25.400000000000002 / N_t: 125.20709541434951 time: 25.5 / N_t: 126.459166368493 time: 25.6 / N_t: 127.72375803217793 time: 25.700000000000003 / N_t: 129.0009956124997 time: 25.8 / N_t: 130.2910055686247 time: 25.900000000000002 / N_t: 131.59391562431097 time: 26.0 / N_t: 132.90985478055407 time: 26.1 / N_t: 134.2389533283596 time: 26.200000000000003 / N_t: 135.5813428616432 time: 26.3 / N_t: 136.93715629025965 time: 26.400000000000002 / N_t: 138.30652785316224 time: 26.5 / N_t: 139.68959313169387 time: 26.6 / N_t: 141.08648906301082 time: 26.700000000000003 / N_t: 142.49735395364092 time: 26.8 / N_t: 143.92232749317733 time: 26.900000000000002 / N_t: 145.3615507681091 time: 27.0 / N_t: 146.8151662757902 time: 27.1 / N_t: 148.2833179385481 time: 27.200000000000003 / N_t: 149.7661511179336 time: 27.3 / N_t: 151.26381262911292 time: 27.400000000000002 / N_t: 152.77645075540406 time: 27.5 / N_t: 154.3042152629581 time: 27.6 / N_t: 155.84725741558768 time: 27.700000000000003 / N_t: 157.40572998974355 time: 27.8 / N_t: 158.979787289641 time: 27.900000000000002 / N_t: 160.5695851625374 time: 28.0 / N_t: 162.17528101416278 time: 28.1 / N_t: 163.7970338243044 time: 28.200000000000003 / N_t: 165.43500416254744 time: 28.3 / N_t: 167.0893542041729 time: 28.400000000000002 / N_t: 168.76024774621465 time: 28.5 / N_t: 170.4478502236768 time: 28.6 / N_t: 172.15232872591358 time: 28.700000000000003 / N_t: 173.87385201317272 time: 28.8 / N_t: 175.61259053330446 time: 28.900000000000002 / N_t: 177.3687164386375 time: 29.0 / N_t: 179.1424036030239 time: 29.1 / N_t: 180.93382763905413 time: 29.200000000000003 / N_t: 182.74316591544468 time: 29.3 / N_t: 184.57059757459913 time: 29.400000000000002 / N_t: 186.41630355034513 time: 29.5 / N_t: 188.2804665858486 time: 29.6 / N_t: 190.16327125170707 time: 29.700000000000003 / N_t: 192.06490396422416 time: 29.8 / N_t: 193.98555300386639 time: 29.900000000000002 / N_t: 195.92540853390506 time: 30.0 / N_t: 197.8846626192441 time: 30.1 / N_t: 199.86350924543655 time: 30.200000000000003 / N_t: 201.86214433789092 time: 30.3 / N_t: 203.88076578126984 time: 30.400000000000002 / N_t: 205.91957343908254 time: 30.5 / N_t: 207.97876917347335 time: 30.6 / N_t: 210.0585568652081 time: 30.700000000000003 / N_t: 212.15914243386018 time: 30.8 / N_t: 214.28073385819877 time: 30.900000000000002 / N_t: 216.42354119678075


Figure2. Simple Euler integration graph


https://github.com/WhoisHOO/HOOAI/blob/main/Python%20Examples/simple_euler_integration


# Codes of Improved Euler Integration

아래의 코드는 Improved euler integration을 통해서 얻어진 결과를 보여주고 있다. 또한 테이블에 나온 산출값과 그래프를 통해서 다른 계산식의 산출값과도 비교해볼 수 있다.


import numpy as np
import matplotlib.pyplot as plt

# Constants
N0 = 10
k = 0.10
t_values = np.arange(0, 31, 0.1)
delta_t = 0.1

# Improved Euler Integration (Heun's Method)
N_improved_euler = [N0]
for t in t_values[1:]:
    N_pred = N_improved_euler[-1] + delta_t * k * N_improved_euler[-1]
    N_next = N_improved_euler[-1] + 0.5 * delta_t * (k * N_improved_euler[-1] + k * N_pred)
    N_improved_euler.append(N_next)

#Table values
for t, N_t in zip(t_values, N_improved_euler):
  print("time: " ,t, "/ N_t: " ,N_t)

# Plotting
plt.plot(t_values, N_improved_euler, label="Improved Euler Integration")
plt.xlabel("Time (t)")
plt.ylabel("Population (N)")
plt.legend()
plt.show()

더보기

time: 0.0 / N_t: 10 time: 0.1 / N_t: 10.1005 time: 0.2 / N_t: 10.202010025 time: 0.30000000000000004 / N_t: 10.30454022575125 time: 0.4 / N_t: 10.40810085502005 time: 0.5 / N_t: 10.512702268613001 time: 0.6000000000000001 / N_t: 10.618354926412561 time: 0.7000000000000001 / N_t: 10.725069393423007 time: 0.8 / N_t: 10.83285634082691 time: 0.9 / N_t: 10.94172654705222 time: 1.0 / N_t: 11.051690898850094 time: 1.1 / N_t: 11.162760392383538 time: 1.2000000000000002 / N_t: 11.274946134326992 time: 1.3 / N_t: 11.388259342976978 time: 1.4000000000000001 / N_t: 11.502711349373897 time: 1.5 / N_t: 11.618313598435105 time: 1.6 / N_t: 11.735077650099377 time: 1.7000000000000002 / N_t: 11.853015180482876 time: 1.8 / N_t: 11.972137983046728 time: 1.9000000000000001 / N_t: 12.092457969776348 time: 2.0 / N_t: 12.2139871723726 time: 2.1 / N_t: 12.336737743454943 time: 2.2 / N_t: 12.460721957776665 time: 2.3000000000000003 / N_t: 12.58595221345232 time: 2.4000000000000004 / N_t: 12.712441033197516 time: 2.5 / N_t: 12.840201065581152 time: 2.6 / N_t: 12.969245086290242 time: 2.7 / N_t: 13.09958599940746 time: 2.8000000000000003 / N_t: 13.231236838701504 time: 2.9000000000000004 / N_t: 13.364210768930453 time: 3.0 / N_t: 13.498521087158204 time: 3.1 / N_t: 13.634181224084145 time: 3.2 / N_t: 13.771204745386191 time: 3.3000000000000003 / N_t: 13.909605353077323 time: 3.4000000000000004 / N_t: 14.04939688687575 time: 3.5 / N_t: 14.19059332558885 time: 3.6 / N_t: 14.333208788511017 time: 3.7 / N_t: 14.477257536835554 time: 3.8000000000000003 / N_t: 14.622753975080752 time: 3.9000000000000004 / N_t: 14.769712652530313 time: 4.0 / N_t: 14.918148264688242 time: 4.1000000000000005 / N_t: 15.068075654748359 time: 4.2 / N_t: 15.21950981507858 time: 4.3 / N_t: 15.37246588872012 time: 4.4 / N_t: 15.526959170901756 time: 4.5 / N_t: 15.683005110569319 time: 4.6000000000000005 / N_t: 15.84061931193054 time: 4.7 / N_t: 15.999817536015442 time: 4.800000000000001 / N_t: 16.160615702252397 time: 4.9 / N_t: 16.323029890060035 time: 5.0 / N_t: 16.48707634045514 time: 5.1000000000000005 / N_t: 16.652771457676714 time: 5.2 / N_t: 16.820131810826364 time: 5.300000000000001 / N_t: 16.989174135525168 time: 5.4 / N_t: 17.159915335587197 time: 5.5 / N_t: 17.332372484709847 time: 5.6000000000000005 / N_t: 17.50656282818118 time: 5.7 / N_t: 17.682503784604403 time: 5.800000000000001 / N_t: 17.860212947639678 time: 5.9 / N_t: 18.039708087763458 time: 6.0 / N_t: 18.22100715404548 time: 6.1000000000000005 / N_t: 18.404128275943638 time: 6.2 / N_t: 18.58908976511687 time: 6.300000000000001 / N_t: 18.775910117256295 time: 6.4 / N_t: 18.964608013934722 time: 6.5 / N_t: 19.155202324474764 time: 6.6000000000000005 / N_t: 19.347712107835736 time: 6.7 / N_t: 19.542156614519484 time: 6.800000000000001 / N_t: 19.738555288495405 time: 6.9 / N_t: 19.936927769144784 time: 7.0 / N_t: 20.13729389322469 time: 7.1000000000000005 / N_t: 20.339673696851598 time: 7.2 / N_t: 20.544087417504958 time: 7.300000000000001 / N_t: 20.750555496050882 time: 7.4 / N_t: 20.959098578786193 time: 7.5 / N_t: 21.169737519502995 time: 7.6000000000000005 / N_t: 21.382493381574 time: 7.7 / N_t: 21.59738744005882 time: 7.800000000000001 / N_t: 21.814441183831413 time: 7.9 / N_t: 22.03367631772892 time: 8.0 / N_t: 22.255114764722094 time: 8.1 / N_t: 22.47877866810755 time: 8.200000000000001 / N_t: 22.704690393722032 time: 8.3 / N_t: 22.932872532178937 time: 8.4 / N_t: 23.163347901127334 time: 8.5 / N_t: 23.396139547533664 time: 8.6 / N_t: 23.631270749986378 time: 8.700000000000001 / N_t: 23.86876502102374 time: 8.8 / N_t: 24.10864610948503 time: 8.9 / N_t: 24.350938002885353 time: 9.0 / N_t: 24.59566492981435 time: 9.1 / N_t: 24.842851362358985 time: 9.200000000000001 / N_t: 25.092522018550692 time: 9.3 / N_t: 25.344701864837127 time: 9.4 / N_t: 25.59941611857874 time: 9.5 / N_t: 25.85669025057046 time: 9.600000000000001 / N_t: 26.11654998758869 time: 9.700000000000001 / N_t: 26.379021314963957 time: 9.8 / N_t: 26.644130479179346 time: 9.9 / N_t: 26.911903990495098 time: 10.0 / N_t: 27.182368625599572 time: 10.100000000000001 / N_t: 27.45555143028685 time: 10.200000000000001 / N_t: 27.731479722161232 time: 10.3 / N_t: 28.010181093368953 time: 10.4 / N_t: 28.291683413357312 time: 10.5 / N_t: 28.576014831661553 time: 10.600000000000001 / N_t: 28.86320378071975 time: 10.700000000000001 / N_t: 29.153278978715985 time: 10.8 / N_t: 29.44626943245208 time: 10.9 / N_t: 29.742204440248223 time: 11.0 / N_t: 30.04111359487272 time: 11.100000000000001 / N_t: 30.34302678650119 time: 11.200000000000001 / N_t: 30.647974205705527 time: 11.3 / N_t: 30.955986346472866 time: 11.4 / N_t: 31.26709400925492 time: 11.5 / N_t: 31.58132830404793 time: 11.600000000000001 / N_t: 31.89872065350361 time: 11.700000000000001 / N_t: 32.21930279607132 time: 11.8 / N_t: 32.54310678917184 time: 11.9 / N_t: 32.870165012403014 time: 12.0 / N_t: 33.20051017077766 time: 12.100000000000001 / N_t: 33.534175297993976 time: 12.200000000000001 / N_t: 33.871193759738816 time: 12.3 / N_t: 34.21159925702419 time: 12.4 / N_t: 34.55542582955729 time: 12.5 / N_t: 34.902707859144336 time: 12.600000000000001 / N_t: 35.253480073128735 time: 12.700000000000001 / N_t: 35.60777754786368 time: 12.8 / N_t: 35.96563571221971 time: 12.9 / N_t: 36.32709035112752 time: 13.0 / N_t: 36.692177609156346 time: 13.100000000000001 / N_t: 37.06093399412837 time: 13.200000000000001 / N_t: 37.43339638076936 time: 13.3 / N_t: 37.80960201439609 time: 13.4 / N_t: 38.189588514640775 time: 13.5 / N_t: 38.573393879212915 time: 13.600000000000001 / N_t: 38.961056487699004 time: 13.700000000000001 / N_t: 39.35261510540038 time: 13.8 / N_t: 39.74810888720965 time: 13.9 / N_t: 40.14757738152611 time: 14.0 / N_t: 40.55106053421045 time: 14.100000000000001 / N_t: 40.958598692579265 time: 14.200000000000001 / N_t: 41.37023260943969 time: 14.3 / N_t: 41.78600344716456 time: 14.4 / N_t: 42.20595278180856 time: 14.5 / N_t: 42.63012260726574 time: 14.600000000000001 / N_t: 43.05855533946876 time: 14.700000000000001 / N_t: 43.491293820630425 time: 14.8 / N_t: 43.92838132352776 time: 14.9 / N_t: 44.36986155582921 time: 15.0 / N_t: 44.81577866446529 time: 15.100000000000001 / N_t: 45.26617724004317 time: 15.200000000000001 / N_t: 45.7211023213056 time: 15.3 / N_t: 46.18059939963472 time: 15.4 / N_t: 46.64471442360105 time: 15.5 / N_t: 47.11349380355824 time: 15.600000000000001 / N_t: 47.586984416284004 time: 15.700000000000001 / N_t: 48.06523360966766 time: 15.8 / N_t: 48.54828920744482 time: 15.9 / N_t: 49.03619951397964 time: 16.0 / N_t: 49.529013319095135 time: 16.1 / N_t: 50.02677990295204 time: 16.2 / N_t: 50.52954904097671 time: 16.3 / N_t: 51.03737100883853 time: 16.400000000000002 / N_t: 51.550296587477355 time: 16.5 / N_t: 52.068377068181505 time: 16.6 / N_t: 52.59166425771673 time: 16.7 / N_t: 53.12021048350678 time: 16.8 / N_t: 53.654068598866026 time: 16.900000000000002 / N_t: 54.19329198828463 time: 17.0 / N_t: 54.73793457276689 time: 17.1 / N_t: 55.288050815223194 time: 17.2 / N_t: 55.843695725916184 time: 17.3 / N_t: 56.40492486796164 time: 17.400000000000002 / N_t: 56.97179436288465 time: 17.5 / N_t: 57.544360896231645 time: 17.6 / N_t: 58.12268172323877 time: 17.7 / N_t: 58.70681467455732 time: 17.8 / N_t: 59.29681816203662 time: 17.900000000000002 / N_t: 59.89275118456509 time: 18.0 / N_t: 60.49467333396997 time: 18.1 / N_t: 61.10264480097637 time: 18.2 / N_t: 61.71672638122618 time: 18.3 / N_t: 62.3369794813575 time: 18.400000000000002 / N_t: 62.963466125145146 time: 18.5 / N_t: 63.596248959702855 time: 18.6 / N_t: 64.23539126174786 time: 18.7 / N_t: 64.88095694392842 time: 18.8 / N_t: 65.53301056121491 time: 18.900000000000002 / N_t: 66.19161731735512 time: 19.0 / N_t: 66.85684307139455 time: 19.1 / N_t: 67.52875434426207 time: 19.200000000000003 / N_t: 68.2074183254219 time: 19.3 / N_t: 68.89290287959238 time: 19.400000000000002 / N_t: 69.58527655353228 time: 19.5 / N_t: 70.28460858289529 time: 19.6 / N_t: 70.99096889915339 time: 19.700000000000003 / N_t: 71.70442813658988 time: 19.8 / N_t: 72.4250576393626 time: 19.900000000000002 / N_t: 73.1529294686382 time: 20.0 / N_t: 73.88811640979802 time: 20.1 / N_t: 74.63069197971649 time: 20.200000000000003 / N_t: 75.38073043411264 time: 20.3 / N_t: 76.13830677497546 time: 20.400000000000002 / N_t: 76.90349675806397 time: 20.5 / N_t: 77.67637690048251 time: 20.6 / N_t: 78.45702448833237 time: 20.700000000000003 / N_t: 79.24551758444011 time: 20.8 / N_t: 80.04193503616374 time: 20.900000000000002 / N_t: 80.84635648327718 time: 21.0 / N_t: 81.65886236593411 time: 21.1 / N_t: 82.47953393271175 time: 21.200000000000003 / N_t: 83.3084532487355 time: 21.3 / N_t: 84.14570320388529 time: 21.400000000000002 / N_t: 84.99136752108434 time: 21.5 / N_t: 85.84553076467124 time: 21.6 / N_t: 86.70827834885618 time: 21.700000000000003 / N_t: 87.57969654626218 time: 21.8 / N_t: 88.45987249655212 time: 21.900000000000002 / N_t: 89.34889421514247 time: 22.0 / N_t: 90.24685060200464 time: 22.1 / N_t: 91.15383145055479 time: 22.200000000000003 / N_t: 92.06992745663287 time: 22.3 / N_t: 92.99523022757202 time: 22.400000000000002 / N_t: 93.92983229135912 time: 22.5 / N_t: 94.87382710588727 time: 22.6 / N_t: 95.82730906830145 time: 22.700000000000003 / N_t: 96.79037352443788 time: 22.8 / N_t: 97.76311677835848 time: 22.900000000000002 / N_t: 98.74563610198098 time: 23.0 / N_t: 99.73802974480589 time: 23.1 / N_t: 100.74039694374119 time: 23.200000000000003 / N_t: 101.75283793302579 time: 23.3 / N_t: 102.7754539542527 time: 23.400000000000002 / N_t: 103.80834726649293 time: 23.5 / N_t: 104.85162115652119 time: 23.6 / N_t: 105.90537994914423 time: 23.700000000000003 / N_t: 106.96972901763313 time: 23.8 / N_t: 108.04477479426033 time: 23.900000000000002 / N_t: 109.13062478094265 time: 24.0 / N_t: 110.22738755999113 time: 24.1 / N_t: 111.33517280496903 time: 24.200000000000003 / N_t: 112.45409129165897 time: 24.3 / N_t: 113.58425490914014 time: 24.400000000000002 / N_t: 114.725776670977 time: 24.5 / N_t: 115.87877072652032 time: 24.6 / N_t: 117.04335237232185 time: 24.700000000000003 / N_t: 118.21963806366368 time: 24.8 / N_t: 119.4077454262035 time: 24.900000000000002 / N_t: 120.60779326773685 time: 25.0 / N_t: 121.8199015900776 time: 25.1 / N_t: 123.04419160105789 time: 25.200000000000003 / N_t: 124.28078572664852 time: 25.3 / N_t: 125.52980762320134 time: 25.400000000000002 / N_t: 126.79138218981451 time: 25.5 / N_t: 128.06563558082215 time: 25.6 / N_t: 129.35269521840942 time: 25.700000000000003 / N_t: 130.65268980535444 time: 25.8 / N_t: 131.96574933789825 time: 25.900000000000002 / N_t: 133.29200511874413 time: 26.0 / N_t: 134.6315897701875 time: 26.1 / N_t: 135.9846372473779 time: 26.200000000000003 / N_t: 137.35128285171405 time: 26.3 / N_t: 138.73166324437378 time: 26.400000000000002 / N_t: 140.12591645997972 time: 26.5 / N_t: 141.53418192040252 time: 26.6 / N_t: 142.95660044870257 time: 26.700000000000003 / N_t: 144.39331428321202 time: 26.8 / N_t: 145.8444670917583 time: 26.900000000000002 / N_t: 147.31020398603047 time: 27.0 / N_t: 148.7906715360901 time: 27.1 / N_t: 150.28601778502778 time: 27.200000000000003 / N_t: 151.7963922637673 time: 27.3 / N_t: 153.32194600601815 time: 27.400000000000002 / N_t: 154.86283156337862 time: 27.5 / N_t: 156.41920302059057 time: 27.6 / N_t: 157.9912160109475 time: 27.700000000000003 / N_t: 159.57902773185754 time: 27.8 / N_t: 161.1827969605627 time: 27.900000000000002 / N_t: 162.80268407001637 time: 28.0 / N_t: 164.43885104492003 time: 28.1 / N_t: 166.09146149792147 time: 28.200000000000003 / N_t: 167.7606806859756 time: 28.3 / N_t: 169.44667552686965 time: 28.400000000000002 / N_t: 171.1496146159147 time: 28.5 / N_t: 172.86966824280464 time: 28.6 / N_t: 174.60700840864482 time: 28.700000000000003 / N_t: 176.3618088431517 time: 28.8 / N_t: 178.13424502202537 time: 28.900000000000002 / N_t: 179.92449418449672 time: 29.0 / N_t: 181.7327353510509 time: 29.1 / N_t: 183.55914934132898 time: 29.200000000000003 / N_t: 185.40391879220934 time: 29.3 / N_t: 187.26722817607103 time: 29.400000000000002 / N_t: 189.14926381924053 time: 29.5 / N_t: 191.0502139206239 time: 29.6 / N_t: 192.97026857052617 time: 29.700000000000003 / N_t: 194.90961976965997 time: 29.8 / N_t: 196.86846144834504 time: 29.900000000000002 / N_t: 198.8469894859009 time: 30.0 / N_t: 200.84540173023422 time: 30.1 / N_t: 202.86389801762306 time: 30.200000000000003 / N_t: 204.90268019270016 time: 30.3 / N_t: 206.9619521286368 time: 30.400000000000002 / N_t: 209.0419197475296 time: 30.5 / N_t: 211.14279104099225 time: 30.6 / N_t: 213.26477609095423 time: 30.700000000000003 / N_t: 215.40808709066832 time: 30.8 / N_t: 217.57293836592953 time: 30.900000000000002 / N_t: 219.75954639650712


Figure3. Improved Euler integration graph


https://github.com/WhoisHOO/HOOAI/blob/main/Python%20Examples/improved_euler_integration


# Codes of RK4

아래의 코드는 RK4 을 통해서 얻어진 결과를 보여주고 있다. 또한 테이블에 나온 산출값과 그래프를 통해서 다른 계산식의 산출값과도 비교해 볼 수 있다.


#4th-order runge
import numpy as np
import matplotlib.pyplot as plt

# Constants
N0 = 10
k = 0.10
t_values = np.arange(0, 31, 0.1)
delta_t = 0.1

# 4th-Order Runge-Kutta Method
N_rk4 = [N0]
for t in t_values[1:]:
    k1 = k * N_rk4[-1]
    k2 = k * (N_rk4[-1] + 0.5 * delta_t * k1)
    k3 = k * (N_rk4[-1] + 0.5 * delta_t * k2)
    k4 = k * (N_rk4[-1] + delta_t * k3)
    N_next = N_rk4[-1] + (delta_t / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
    N_rk4.append(N_next)

#Table values
for t, N_t in zip(t_values, N_rk4):
  print("time: " ,t, "/ N_t: " ,N_t)

# Plotting
plt.plot(t_values, N_rk4, label="4th-Order Runge-Kutta")
plt.xlabel("Time (t)")
plt.ylabel("Population (N)")
plt.legend()
plt.show()

더보기

time: 0.0 / N_t: 10 time: 0.1 / N_t: 10.100501670833333 time: 0.2 / N_t: 10.202013400250696 time: 0.30000000000000004 / N_t: 10.304545339509621 time: 0.4 / N_t: 10.408107741889477 time: 0.5 / N_t: 10.5127109637168 time: 0.6000000000000001 / N_t: 10.618365465400945 time: 0.7000000000000001 / N_t: 10.725081812480122 time: 0.8 / N_t: 10.832870676677967 time: 0.9 / N_t: 10.941742836970722 time: 1.0 / N_t: 11.051709180665144 time: 1.1 / N_t: 11.162780704487238 time: 1.2000000000000002 / N_t: 11.274968515681945 time: 1.3 / N_t: 11.388283833123872 time: 1.4000000000000001 / N_t: 11.50273798843919 time: 1.5 / N_t: 11.61834242713881 time: 1.6 / N_t: 11.735108709762935 time: 1.7000000000000002 / N_t: 11.853048513037132 time: 1.8 / N_t: 11.972173631040011 time: 1.9000000000000001 / N_t: 12.09249597638264 time: 2.0 / N_t: 12.214027581399822 time: 2.1 / N_t: 12.336780599353332 time: 2.2 / N_t: 12.46076730564726 time: 2.3000000000000003 / N_t: 12.586000099055552 time: 2.4000000000000004 / N_t: 12.71249150296191 time: 2.5 / N_t: 12.840254166612134 time: 2.6 / N_t: 12.969300866379053 time: 2.7 / N_t: 13.099644507040182 time: 2.8000000000000003 / N_t: 13.231298123068205 time: 2.9000000000000004 / N_t: 13.364274879934435 time: 3.0 / N_t: 13.49858807542537 time: 3.1 / N_t: 13.634251140972486 time: 3.2 / N_t: 13.771277642995388 time: 3.3000000000000003 / N_t: 13.909681284258465 time: 3.4000000000000004 / N_t: 14.049475905241177 time: 3.5 / N_t: 14.190675485522117 time: 3.6 / N_t: 14.333294145176977 time: 3.7 / N_t: 14.477346146190568 time: 3.8000000000000003 / N_t: 14.622845893883035 time: 3.9000000000000004 / N_t: 14.769807938350395 time: 4.0 / N_t: 14.91824697591956 time: 4.1000000000000005 / N_t: 15.068177850617984 time: 4.2 / N_t: 15.219615555658077 time: 4.3 / N_t: 15.372575234936539 time: 4.4 / N_t: 15.527072184548764 time: 4.5 / N_t: 15.683121854318458 time: 4.6000000000000005 / N_t: 15.840739849342635 time: 4.7 / N_t: 15.999941931552145 time: 4.800000000000001 / N_t: 16.160744021287876 time: 4.9 / N_t: 16.3231621988928 time: 5.0 / N_t: 16.487212706320022 time: 5.1000000000000005 / N_t: 16.652911948756994 time: 5.2 / N_t: 16.82027649626604 time: 5.300000000000001 / N_t: 16.989323085441377 time: 5.4 / N_t: 17.160068621082797 time: 5.5 / N_t: 17.332530177886145 time: 5.6000000000000005 / N_t: 17.50672500215082 time: 5.7 / N_t: 17.682670513504405 time: 5.800000000000001 / N_t: 17.860384306644654 time: 5.9 / N_t: 18.039884153098978 time: 6.0 / N_t: 18.2211880030016 time: 6.1000000000000005 / N_t: 18.404313986888596 time: 6.2 / N_t: 18.589280417510956 time: 6.300000000000001 / N_t: 18.776105791665877 time: 6.4 / N_t: 18.964808792046462 time: 6.5 / N_t: 19.15540828911 time: 6.6000000000000005 / N_t: 19.347923342965025 time: 6.7 / N_t: 19.54237320527735 time: 6.800000000000001 / N_t: 19.738777321195244 time: 6.9 / N_t: 19.937155331293965 time: 7.0 / N_t: 20.13752707353984 time: 7.1000000000000005 / N_t: 20.339912585274064 time: 7.2 / N_t: 20.544332105216462 time: 7.300000000000001 / N_t: 20.750806075489376 time: 7.4 / N_t: 20.95935514366189 time: 7.5 / N_t: 21.170000164814617 time: 7.6000000000000005 / N_t: 21.382762203625198 time: 7.7 / N_t: 21.597662536474814 time: 7.800000000000001 / N_t: 21.814722653575835 time: 7.9 / N_t: 22.03396426112085 time: 8.0 / N_t: 22.25540928345331 time: 8.1 / N_t: 22.479079865259983 time: 8.200000000000001 / N_t: 22.70499837378544 time: 8.3 / N_t: 22.933187401068796 time: 8.4 / N_t: 23.163669766202933 time: 8.5 / N_t: 23.39646851761643 time: 8.6 / N_t: 23.631606935378425 time: 8.700000000000001 / N_t: 23.86910853352664 time: 8.8 / N_t: 24.108997062418798 time: 8.9 / N_t: 24.351296511107698 time: 9.0 / N_t: 24.59603110974012 time: 9.1 / N_t: 24.843225331979873 time: 9.200000000000001 / N_t: 25.09290389745517 time: 9.3 / N_t: 25.34509177423062 time: 9.4 / N_t: 25.599814181304055 time: 9.5 / N_t: 25.857096591128446 time: 9.600000000000001 / N_t: 26.116964732159175 time: 9.700000000000001 / N_t: 26.3794445914269 time: 9.8 / N_t: 26.644562417136274 time: 9.9 / N_t: 26.912344721290797 time: 10.0 / N_t: 27.182818282344034 time: 10.100000000000001 / N_t: 27.45601014787748 time: 10.200000000000001 / N_t: 27.731947637305343 time: 10.3 / N_t: 28.01065834460651 time: 10.4 / N_t: 28.29217014108397 time: 10.5 / N_t: 28.57651117815196 time: 10.600000000000001 / N_t: 28.86370989015113 time: 10.700000000000001 / N_t: 29.153794997192012 time: 10.8 / N_t: 29.44679550802704 time: 10.9 / N_t: 29.742740722951464 time: 11.0 / N_t: 30.04166023673339 time: 11.100000000000001 / N_t: 30.34358394157329 time: 11.200000000000001 / N_t: 30.64854203009325 time: 11.3 / N_t: 30.95656499835625 time: 11.4 / N_t: 31.2676836489158 time: 11.5 / N_t: 31.581929093896214 time: 11.600000000000001 / N_t: 31.899332758103856 time: 11.700000000000001 / N_t: 32.219926382169646 time: 11.8 / N_t: 32.54374202572315 time: 11.9 / N_t: 32.87081207059856 time: 12.0 / N_t: 33.20116922407293 time: 12.100000000000001 / N_t: 33.53484652213689 time: 12.200000000000001 / N_t: 33.871877332798306 time: 12.3 / N_t: 34.2122953594191 time: 12.4 / N_t: 34.55613464408561 time: 12.5 / N_t: 34.903429571012836 time: 12.600000000000001 / N_t: 35.254214869982874 time: 12.700000000000001 / N_t: 35.60852561981794 time: 12.8 / N_t: 35.96639725188827 time: 12.9 / N_t: 36.327865553655286 time: 13.0 / N_t: 36.69296667225039 time: 13.100000000000001 / N_t: 37.06173711808969 time: 13.200000000000001 / N_t: 37.43421376852507 time: 13.3 / N_t: 37.81043387153196 time: 13.4 / N_t: 38.190435049434186 time: 13.5 / N_t: 38.57425530266619 time: 13.600000000000001 / N_t: 38.96193301357314 time: 13.700000000000001 / N_t: 39.35350695024919 time: 13.8 / N_t: 39.749016270414316 time: 13.9 / N_t: 40.148500525330114 time: 14.0 / N_t: 40.55199966375498 time: 14.100000000000001 / N_t: 40.95955403593899 time: 14.200000000000001 / N_t: 41.371204397659 time: 14.3 / N_t: 41.786991914294205 time: 14.4 / N_t: 42.20695816494276 time: 14.5 / N_t: 42.6311451465797 time: 14.600000000000001 / N_t: 43.05959527825666 time: 14.700000000000001 / N_t: 43.49235140534385 time: 14.8 / N_t: 43.9294568038146 time: 14.9 / N_t: 44.37095518457301 time: 15.0 / N_t: 44.816890697825066 time: 15.100000000000001 / N_t: 45.2673079374937 time: 15.200000000000001 / N_t: 45.72225194567821 time: 15.3 / N_t: 46.181768217158535 time: 15.4 / N_t: 46.64590270394475 time: 15.5 / N_t: 47.1147018198723 time: 15.600000000000001 / N_t: 47.58821244524345 time: 15.700000000000001 / N_t: 48.06648193151531 time: 15.8 / N_t: 48.549558106035064 time: 15.9 / N_t: 49.03748927682272 time: 16.0 / N_t: 49.530324237401956 time: 16.1 / N_t: 50.02811227167952 time: 16.2 / N_t: 50.530903158873656 time: 16.3 / N_t: 51.03874717849207 time: 16.400000000000002 / N_t: 51.55169511535993 time: 16.5 / N_t: 52.069798264698356 time: 16.6 / N_t: 52.59310843725403 time: 16.7 / N_t: 53.121677964480305 time: 16.8 / N_t: 53.65555970377036 time: 16.900000000000002 / N_t: 54.19480704374302 time: 17.0 / N_t: 54.73947390958165 time: 17.1 / N_t: 55.28961476842671 time: 17.2 / N_t: 55.845284634822534 time: 17.3 / N_t: 56.406539076218806 time: 17.400000000000002 / N_t: 56.97343421852737 time: 17.5 / N_t: 57.54602675173487 time: 17.6 / N_t: 58.124373935571775 time: 17.7 / N_t: 58.708533605238415 time: 17.8 / N_t: 59.29856417718855 time: 17.900000000000002 / N_t: 59.894524654971065 time: 18.0 / N_t: 60.496474635130355 time: 18.1 / N_t: 61.10447431316605 time: 18.2 / N_t: 61.71858448955262 time: 18.3 / N_t: 62.338866575819445 time: 18.400000000000002 / N_t: 62.96538260069205 time: 18.5 / N_t: 63.59819521629502 time: 18.6 / N_t: 64.23736770441724 time: 18.7 / N_t: 64.88296398284015 time: 18.8 / N_t: 65.5350486117296 time: 18.900000000000002 / N_t: 66.19368680009185 time: 19.0 / N_t: 66.85894441229462 time: 19.1 / N_t: 67.53088797465348 time: 19.200000000000003 / N_t: 68.20958468208461 time: 19.3 / N_t: 68.89510240482433 time: 19.400000000000002 / N_t: 69.58750969521617 time: 19.5 / N_t: 70.28687579456617 time: 19.6 / N_t: 70.99327064006705 time: 19.700000000000003 / N_t: 71.70676487179203 time: 19.8 / N_t: 72.42742983975884 time: 19.900000000000002 / N_t: 73.15533761106482 time: 20.0 / N_t: 73.89056097709368 time: 20.1 / N_t: 74.63317346079471 time: 20.200000000000003 / N_t: 75.38324932403509 time: 20.3 / N_t: 76.14086357502622 time: 20.400000000000002 / N_t: 76.90609197582452 time: 20.5 / N_t: 77.67901104990776 time: 20.6 / N_t: 78.45969808982743 time: 20.700000000000003 / N_t: 79.24823116493808 time: 20.8 / N_t: 80.04468912920433 time: 20.900000000000002 / N_t: 80.84915162908631 time: 21.0 / N_t: 81.66169911150438 time: 21.1 / N_t: 82.48241283188389 time: 21.200000000000003 / N_t: 83.3113748622808 time: 21.3 / N_t: 84.14866809958895 time: 21.400000000000002 / N_t: 84.99437627382977 time: 21.5 / N_t: 85.84858395652546 time: 21.6 / N_t: 86.71137656915612 time: 21.700000000000003 / N_t: 87.58284039170196 time: 21.8 / N_t: 88.46306257127148 time: 21.900000000000002 / N_t: 89.35213113081613 time: 22.0 / N_t: 90.25013497793275 time: 22.1 / N_t: 91.15716391375436 time: 22.200000000000003 / N_t: 92.0733086419304 time: 22.3 / N_t: 92.99866077769711 time: 22.400000000000002 / N_t: 93.93331285703921 time: 22.5 / N_t: 94.87735834594348 time: 22.6 / N_t: 95.8308916497455 time: 22.700000000000003 / N_t: 96.79400812257025 time: 22.8 / N_t: 97.7668040768676 time: 22.900000000000002 / N_t: 98.74937679304364 time: 23.0 / N_t: 99.74182452918876 time: 23.1 / N_t: 100.74424653090362 time: 23.200000000000003 / N_t: 101.75674304122373 time: 23.3 / N_t: 102.77941531064384 time: 23.400000000000002 / N_t: 103.81236560724312 time: 23.5 / N_t: 104.855697226912 time: 23.6 / N_t: 105.90951450368188 time: 23.700000000000003 / N_t: 106.97392282015859 time: 23.8 / N_t: 108.04902861806079 time: 23.900000000000002 / N_t: 109.13493940886417 time: 24.0 / N_t: 110.23176378455271 time: 24.1 / N_t: 111.339611428478 time: 24.200000000000003 / N_t: 112.45859312632761 time: 24.3 / N_t: 113.58882077720381 time: 24.400000000000002 / N_t: 114.7304074048135 time: 24.5 / N_t: 115.88346716877078 time: 24.6 / N_t: 117.0481153760129 time: 24.700000000000003 / N_t: 118.22446849233111 time: 24.8 / N_t: 119.41264415401731 time: 24.900000000000002 / N_t: 120.61276117962781 time: 25.0 / N_t: 121.82493958186525 time: 25.1 / N_t: 123.04930057957999 time: 25.200000000000003 / N_t: 124.28596660989207 time: 25.3 / N_t: 125.53506134043508 time: 25.400000000000002 / N_t: 126.79670968172294 time: 25.5 / N_t: 128.07103779964118 time: 25.6 / N_t: 129.35817312806347 time: 25.700000000000003 / N_t: 130.65824438159527 time: 25.8 / N_t: 131.9713815684453 time: 25.900000000000002 / N_t: 133.29771600342653 time: 26.0 / N_t: 134.63738032108768 time: 26.1 / N_t: 135.9905084889769 time: 26.200000000000003 / N_t: 137.35723582103859 time: 26.3 / N_t: 138.73769899114484 time: 26.400000000000002 / N_t: 140.13203604676306 time: 26.5 / N_t: 141.54038642276072 time: 26.6 / N_t: 142.96289095534902 time: 26.700000000000003 / N_t: 144.39969189616664 time: 26.8 / N_t: 145.85093292650498 time: 26.900000000000002 / N_t: 147.3167591716764 time: 27.0 / N_t: 148.79731721552693 time: 27.1 / N_t: 150.29275511509474 time: 27.200000000000003 / N_t: 151.80322241541595 time: 27.3 / N_t: 153.3288701644793 time: 27.400000000000002 / N_t: 154.86985092833103 time: 27.5 / N_t: 156.4263188063317 time: 27.6 / N_t: 157.9984294465661 time: 27.700000000000003 / N_t: 159.58634006140832 time: 27.8 / N_t: 161.19020944324313 time: 27.900000000000002 / N_t: 162.8101979803452 time: 28.0 / N_t: 164.44646767291826 time: 28.1 / N_t: 166.09918214929507 time: 28.200000000000003 / N_t: 167.7685066823005 time: 28.3 / N_t: 169.45460820577895 time: 28.400000000000002 / N_t: 171.15765533128783 time: 28.5 / N_t: 172.87781836495884 time: 28.6 / N_t: 174.61526932452884 time: 28.700000000000003 / N_t: 176.37018195654161 time: 28.8 / N_t: 178.14273175372276 time: 28.900000000000002 / N_t: 179.9330959725291 time: 29.0 / N_t: 181.74145365087446 time: 29.1 / N_t: 183.56798562603362 time: 29.200000000000003 / N_t: 185.41287455272618 time: 29.3 / N_t: 187.2763049213822 time: 29.400000000000002 / N_t: 189.15846307659137 time: 29.5 / N_t: 191.05953723573765 time: 29.6 / N_t: 192.97971750782116 time: 29.700000000000003 / N_t: 194.91919591246923 time: 29.8 / N_t: 196.87816639913854 time: 29.900000000000002 / N_t: 198.85682486651018 time: 30.0 / N_t: 200.85536918207976 time: 30.1 / N_t: 202.87399920194426 time: 30.200000000000003 / N_t: 204.91291679078782 time: 30.3 / N_t: 206.97232584206841 time: 30.400000000000002 / N_t: 209.05243229840733 time: 30.5 / N_t: 211.15344417218355 time: 30.6 / N_t: 213.2755715663353 time: 30.700000000000003 / N_t: 215.41902669537038 time: 30.8 / N_t: 217.5840239065879 time: 30.900000000000002 / N_t: 219.7707797015131


Figure4. RK4 graph


https://github.com/WhoisHOO/HOOAI/blob/main/Python%20Examples/runge-kutta_4th_order


 

728x90

댓글