Beruflich Dokumente
Kultur Dokumente
College of Engineering
Department of Electrical Engineering
2nd Year Electrical
System Dynamics and Control Components
2
I – Differential Equations :
i- Definition :
3
ii- Ordinary Differential Equations :
4
iii- Numerical Ordinary Differential Equations :
5
II – Runge-Kutta Methods :
i- Introduction :
w 1 =hf x n , y n
1 1
w 2 =hf x n h , y n w 1
2 2
1 1
w3=h f x n h , y n w 2
2 2
w 4=hf x n h , y n w 3
Example :
Apply the Runge-Kutta method to the IVP
'
y =1− x4y ; y 0=1
to find y(0.2). Take h = 0.1 and use six digits.
Solution :
n x y w=h(1-x+4y) Δ y
0 0.00 1.00000 0.500000 0.50000
0.05 1.25000 0.595000 1.19000
0.05 1.29750 0.614000 0.22800
0.10 1.30700 0.612800 0.612800
1
y1 =1 2.5228=1.420466
6 2.5228
7
n x y w=h(1-x+4y) Δ y
1 0.10 1.42046 0.65818 0.65818
0.15 1.74955 0.78482 1.56964
0.15 1.81282 0.81014 1.42029
0.20 1.82553 0.81021 0.81021
1
y 2 =1.420466 4.45832=2.44469
6 4.45832
8
iii- Computer-Aided 4th Order Runge-Kutta
Method :
9
H:\Users\Mohammed\Desktop\Runge\RK4.py
49
50
51
52 # t his is th e b egi nni ng of the it era tio n p roc ess , ' for ' l oop sh oul d g o a s
m any ti mes as n is :
53
54 for i in ran ge( n):
55
56 # her e w e s imp ly tak e t he val ues of in iti al con dit ion va ria ble s x 0,y 0
ent ere d b y u ser :
57
58 xA [0] =x0
59 yA [0] =y0
60
61 #c hec k
62 #p rin t x A[0 ],y A[0 ]
63
64 # the n w e a ssi gn the m t o v ari abl es cal led x and y to hel p e val ()
fun cti on eva lua te the en ter ed equ ati on as pyt hon 's val id exp res sio n,
it the n h app ens ea ch and ev ery ti me we' d l ike to us e e val () fun cti on
wit h o the r v alu es :
65
66
67 x= xA[ 0]
68 y= yA[ 0]
69
70 # her e w e u se eva l() fu nti on to eva lua te the en ter ed exp res sio n w ith
x0, y0 val ues to ge t w 1(0 ) a nd the n w e u se w1( 0) to get th e n ext
val ue of y :
71
72 wA [0] = h * e val (eq n)
73 yA [1] = y A[0 ] + (0 .5* wA[ 0])
74
75 #c hec k
76 #p rin t w A[0 ],y A[1 ]
77
78 # her e's ho w t he nex t v alu e o f x is ob tai ned an d h ow we use th e
pre vio us tec hni que ag ain :
79
80 xA [1] = x 0+( 0.5 *h)
81 x= xA[ 1]
82 y= yA[ 1]
83
84 #c hec k
85 #p rin t x A[1 ]
86
87 wA [1] = h * e val (eq n)
88 yA [2] = y A[0 ] + (0 .5* wA[ 1])
89
90 #c hec k
91 #p rin t w A[1 ],y A[2 ]
92
93
94
95
96
97
98
H:\Users\Mohammed\Desktop\Runge\RK4.py
99
100 xA [2] = x 0+( 0.5 *h)
101 x= xA[ 2]
102 y= yA[ 2]
103
104 #c hec k
105 #p rin t x A[2 ]
106
107 wA [2] = h * e val (eq n)
108 yA [3] = y A[0 ] + (0 .5* wA[ 2])
109
110 #c hec k
111 #p rin t w A[2 ],y A[3 ]
112
113 xA [3] = x 0+h
114 x= xA[ 3]
115 y= yA[ 3]
116
117 #c hec k
118 #p rin t x A[3 ]
119
120
121 wA [3] = h * e val (eq n)
122
123 #c hec k
124 #p rin t w A[3 ]
125
126 # fin all y w e c alc ula te the va lue of dy fo r t he ite rat ion :
127
128 dy A[0 ] = wA [0]
129 dy A[1 ] = 2* wA[ 1]
130 dy A[2 ] = 2* wA[ 2]
131 dy A[3 ] = wA [3]
132
133 #c hec k
134 #p rin t d yA[ 0], dyA [1] ,dy A[2 ],d yA[ 3]
135
136 su m = dy A[0 ]+d yA[ 1]+ dyA [2] +dy A[3 ]
137
138 # thi s i s h ow we get yn +1 :
139
140 re sul t = yA [0] + ( sum /6. 0)
141
142 # bef ore th e i ter ati on end s, we' d l ike to us e s ome va lue fr om it for
the ne xt one :
143
144 x0 =xA [3] # x n+1
145 y0 =re sul t # y n+1
146
147 #c hec k
148 #p rin t x 0,y 0
149
150 # f unc tio n r etu rns th e f ina l r esu lt of the lo op whi ch' s t he sol uti on of
t he DE at the po int of in ter est an d t he val ue of thi s p oin t :
151 # u nli ke C a nd som e o the r l ang uag es, py tho n i s c apa ble of re tur nin g
m ult ipl e s tuf f n ot jus t o ne var iab le wit hou t t he exp lic it use of
p oin ter s.
152
H:\Users\Mohammed\Desktop\Runge\RK4.py
153
154
155 ret urn re sul t,s
156
157 # e nd of RK4 () fun cti on
158
159
160
161
162
163
164 # he re we sim ply ca ll the fu nct ion to re cei ve the de sir ed val ues :
165
166 f ina l,s = RK4 ()
167
168 # th en we rou nd the so lut ion to 5 dig its :
169
170 f ina l = ro und (fi nal ,5)
171
172 # th e o utp ut :
173
174 p rin t " \ny (%s ) = %f " %(s ,fi nal )
175
176 # sc rip t t erm ina tio n :
177
178 e sc = r aw_ inp ut( "\n \nP res s a ny key to en d . .." )
179
3- A Sample Runtime :
Important notes :
1- When entering the equation, please make sure your use * for multiplication
and ** for exponentiation ex: x^2 should be written as x**2.
2- Use parentheses () to skip order of precedence.
y(0.2) = 2.444690
14
III – Utilizing Runge-Kutta Method
In Solving Higher Order
Differential Equations :
Any differential equation of order n can be written as a system of
n first-order differential equations. Given an explicit ordinary
differential equation of order n and dimension 1,
' '' n−1 n
F x , y , y , y ,..... , y = y
we define a new family of unknown functions
n−1
y n := y
.
.
.
'
y n−1 = y n
'
y n =F y n ,.... , y 1 , x
15
Suppose we obtained a system from a higher order DE to be as
follows :
' '
x = f t , x , y , y = g t , x , y ; x 0= x 0 , y 0= y0
1
x n1= x n v 12 v 22 v 3 v 4
6
1
y n1 = y n w 1 2w 2 2w 3w 4
6
where :
v1 =hf t n , x n , y n
w 1 =hg t n , x n , y n
1 1 1
v 2 =hf t n h , x n v1 , y n w 1
2 2 2
1 1 1
w 2 =hg t n h , x n v 1 , y n w 1
2 2 2
1 1 1
v 3=hf t n h , x n v 2 , y n w 2
2 2 2
1 1 1
w 3 =hg t n h , x n v 2 , y n w 2
2 2 2
v 4 =hf t n h , x n v 3 , y n w 3
w 3 =hg t n h , x n v 3 , y n w 3
16
Example :
Use Runge-Kutta method with step size h = 0.25 to compute
x(0.5) for the IVP x ' ' −tx ' − x=0 ; x 0=0 , x ' 0=1
Use five digits.
Solution :
n t x y V=hf W=hg Δx Δy
0 0.000 0.00000 1.0000 0.25000 0.00000 0.25000 0.00000
0.125 0.12500 1.0000 0.25000 0.06250 0.50000 0.12500
0.125 0.12500 1.0313 0.25783 0.06347 0.51566 0.12696
0.250 0.25783 1.0635 0.26588 0.13093 0.26588 0.13093
x 1=00.25526=0.25526
y1 =10.063815=1.0638 0.28853 0.063815
n t x y V=hf W=hg Δx Δy
1 0.250 0.25526 1.0638 0.26595 0.13030 0.26595 0.13030
0.375 0.38824 1.1290 0.28225 0.20290 0.56450 0.40580
0.375 0.39639 1.1653 0.29133 0.20848 0.58266 0.41696
0.500 0.54659 1.2723 0.31808 0.29569 0.31808 0.29569
x 0.5≈ x 2 =0.255260.28853=0.54379
' 0.28853 0.20813
x 0.5= y 0.5≈ y 2 =1.06380.20813=1.2719
17
Although we took n = 2 only, the total error made in computing
x(0.5) is 0.00004 which is much better than any of the second
order methods with n = 5.
18