Sie sind auf Seite 1von 13

CPE 310

Chapter 5 Examples part II

Taylor Series Method


Consider

dy
= x+ y
dx
The initial conditions is :

y (0 ) = 1
The analytical solution

y ( x ) = 2e x x 1

Taylor Series Method


The higher order derivatives can be found with the
initial condition, y(0) = 1, and the equation.

y( x ) = x + y ( x ) y(0 ) = x + y (0) = 0 + 1 = 1
y(x ) = 1 + y( x )
y(0 ) = 1 + y(0) = 1 + 1 = 2
y( x ) = y( x )
y(0 ) = y(0 ) = 2
y IV ( x ) = y( x )

y IV (0 ) = y(0 ) = 2

CPE 310

Chapter 5 Examples part II

Taylor Series Method


From the Taylor series expansion
y ( x ) = y (x0 ) + h y( x0 ) +

h 2
h 3
h 4 IV
y( x0 ) +
y(x0 ) +
y ( x0 ) + K
2!
3!
4!

Plug in the initial conditions:


y (h ) = 1 + h (1) +

3
4
h 2
(2) + h (2) + h (2) + Error
2!
3!
4!

Resulting in the equation:

h 3 h 4
y (h ) = 1 + h + h +
+
+ Error
3
12
2

Taylor Series Example

The results
h
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2

Second
y(
h )
1.00000
1.11000
1.24000
1.39000
1.56000
1.75000
1.96000
2.19000
2.44000
2.71000
3.00000
3.31000
3.64000
3.99000
4.36000
4.75000
5.16000
5.59000
6.04000
6.51000
7.00000

Third
y(
h )
1.00000
1.11033
1.24267
1.39900
1.58133
1.79167
2.03200
2.30433
2.61067
2.95300
3.33333
3.75367
4.21600
4.72233
5.27467
5.87500
6.52533
7.22767
7.98400
8.79633
9.66667

Fourth
y(
h )
1.00000
1.11034
1.24280
1.39968
1.58347
1.79688
2.04280
2.32434
2.64480
3.00768
3.41667
3.87568
4.38880
4.96034
5.59480
6.29688
7.07147
7.92368
8.85880
9.88234
11.00000

Exact
Solution
1.00000
1.11034
1.24281
1.39972
1.58365
1.79744
2.04424
2.32751
2.65108
3.01921
3.43656
3.90833
4.44023
5.03859
5.71040
6.46338
7.30606
8.24789
9.29929
10.47179
11.77811

Error
0.00000
-0.00034
-0.00281
-0.00972
-0.02365
-0.04744
-0.08424
-0.13751
-0.21108
-0.30921
-0.43656
-0.59833
-0.80023
-1.04859
-1.35040
-1.71338
-2.14606
-2.65789
-3.25929
-3.96179
-4.77811

CPE 310

Chapter 5 Examples part II

Taylor Series Method


Taylor series applicable to higher order equations,
but one needs more initial conditions

y( x ) = 3 + x y 2
The initial conditions are:

y (0 ) = 1
y (0 ) = 2

Taylor Series Method


We can use the initial conditions to get the higher
derivative, however it will become more
complicated.
y = 1 2 yy

y IV = 2 yy 2 y 2
y V = 2 yy 2 yy 4 yy = 6 yy 2 yy
etc.
The higher order terms can be calculated from
previous values and they are difficult to calculate.

CPE 310

Chapter 5 Examples part II

Euler Method
The Euler method can have the algorithm, where
the coefficients are upgraded each time step.

( )

yn +1 = yn + h yn + O h 2

error

The first derivative and the initial y values are


update each iteration.

Eulers Method Example


Consider

dy
= x+ y
dx
The initial condition is:
The step size is:

y (0 ) = 1

h = 0.02

The analytical solution is:

y ( x ) = 2e x x 1

CPE 310

Chapter 5 Examples part II

Eulers Method Example


The algorithm has a loop using the initial conditions
and definition of the derivative
The derivative is calculated as:

yi = xi + yi
The next y value is calculated:

yi +1 = yi + h yi
Take the next step:

xi +1 = xi + h

Eulers Method Example


The results
xn
0
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
0.18
0.2

yn
1.00000
1.02000
1.04080
1.06242
1.08486
1.10816
1.13232
1.15737
1.18332
1.21019
1.23799

y'n
1.00000
1.04000
1.08080
1.12242
1.16486
1.20816
1.25232
1.29737
1.34332
1.39019
1.43799

hy'n
0.02000
0.02080
0.02162
0.02245
0.02330
0.02416
0.02505
0.02595
0.02687
0.02780
0.02876

Exact
Solution
1.00000
1.02040
1.04162
1.06367
1.08657
1.11034
1.13499
1.16055
1.18702
1.21443
1.24281

Error
0.00000
-0.00040
-0.00082
-0.00126
-0.00171
-0.00218
-0.00267
-0.00318
-0.00370
-0.00425
-0.00482

CPE 310

Chapter 5 Examples part II

Modified Euler Method


The Modified Euler method uses the slope at the
new location and is a predictor-corrector technique.

y + yn +1
2
yn +1 = yn + h n
+ O h
2

( )

The method uses the average slope between the


two locations.

Modified Euler Method


The algorithm will be:

yn = f ( xn , yn )
*

Initial guess of the value yn +1 = y n + h yn

yn*+1 = f xn +1 , yn* +1
Updated value

yn + yn*+1

yn +1 = yn + h
2

CPE 310

Chapter 5 Examples part II

Modified Eulers Method


Example

Consider

dy
= x+ y
dx
y (0 ) = 1

The initial condition is:


The step size is:

h = 0.02

The analytical solution is:

y ( x ) = 2e x x 1

Modified Eulers Method


Example
The results are:
yn
1.00000
1.02040
1.04162
1.06366
1.08656
1.11033
1.13498
1.16053
1.18700
1.21441
1.24277

y'n
1.00000
1.04040
1.08162
1.12366
1.16656
1.21033
1.25498
1.30053
1.34700
1.39441
1.44277

hy'n
0.02000
0.02081
0.02163
0.02247
0.02333
0.02421
0.02510
0.02601
0.02694
0.02789
0.02886

Estimated
y n+1
1.02000
1.04121
1.06325
1.08614
1.10989
1.13453
1.16008
1.18654
1.21394
1.24229
1.27163

Solution
y'n+1
1.04000
1.08121
1.12325
1.16614
1.20989
1.25453
1.30008
1.34654
1.39394
1.44229
1.49163

Average
h(y'n+y'n+1 ) / 2
0.02040
0.02122
0.02205
0.02290
0.02376
0.02465
0.02555
0.02647
0.02741
0.02837
0.02934

Exact
Solution
1.00000
1.02040
1.04162
1.06367
1.08657
1.11034
1.13499
1.16055
1.18702
1.21443
1.24281

CPE 310

Chapter 5 Examples part II

Runge-Kutta Methods
To convey some idea of how the Runge-Kutta is
developed, lets look at the derivation of the 2nd
order. Two estimates

yn +1 = yn + ak1 + bk 2
k1 = hf ( xn , yn )
k 2 = hf ( xn + h, yn + k1 )

Runge-Kutta Methods
The Taylor series coefficients (3 equations/4 unknowns)
[a + b] = 1, b = 1 , b = 1
2
2
If you select a as
2
1
3
3
a= , b= , = , =
3
3
2
2
If you select a as

1
1
b = , = =1
2
2
Note: These coefficient would result in a modified Euler or
Midpoint Method
a=

CPE 310

Chapter 5 Examples part II

Runge-Kutta Method
(2nd Order) Example
Consider

Exact Solution

dy
= y2
dx

y=
y (0 ) = 1

The initial condition is:


The step size is:

Use the coefficients

1
1+ x

h = 0.1
a=

1
1
b = , = =1
2
2

Runge-Kutta Method
(2nd Order) Example
The values are

k1 = hf ( xi , yi )
k 2 = hf ( xi + h, yi + k1 )
yi +1 = yi +

1
[k1 + k2 ]
2

CPE 310

Chapter 5 Examples part II

Runge-Kutta Method
(2nd Order) Example
The values are equivalent of Modified Euler
yn
1.00000
0.90950
0.83396
0.76997
0.71507
0.66747
0.62579
0.58900
0.55629
0.52702
0.50067

y'n
-1.00000
-0.82719
-0.69549
-0.59286
-0.51133
-0.44551
-0.39161
-0.34692
-0.30946
-0.27775
-0.25067

k1

Estimate

Solution

k2

Exact

hy'n
-0.10000
-0.08272
-0.06955
-0.05929
-0.05113
-0.04455
-0.03916
-0.03469
-0.03095
-0.02778
-0.02507

y* n+1
0.90000
0.82678
0.76441
0.71069
0.66394
0.62292
0.58663
0.55431
0.52535
0.49925
0.47560

y*' n+1
-0.81000
-0.68357
-0.58433
-0.50507
-0.44082
-0.38802
-0.34413
-0.30726
-0.27599
-0.24925
-0.22620

h(y*'n+1 )
-0.08100
-0.06836
-0.05843
-0.05051
-0.04408
-0.03880
-0.03441
-0.03073
-0.02760
-0.02492
-0.02262

1.000000
0.909091
0.833333
0.769231
0.714286
0.666667
0.625000
0.588235
0.555556
0.526316
0.500000

Runge-Kutta Method
(2nd Order) Example [b]
The values are

a=

2
1
3
3
, b= , = , =
3
3
2
2

k1 = hf ( xi , yi )
k 2 = hf ( xi + h, yi + k1 )
yi +1 = yi + ak1 + bk 2

CPE 310

Chapter 5 Examples part II

Runge-Kutta Method
(2nd Order) Example [b]
The values are
yn
1.00000
0.90925
0.83358
0.76953
0.71460
0.66699
0.62532
0.58855
0.55586
0.52661
0.50028

y'n
-1.00000
-0.82674
-0.69486
-0.59217
-0.51066
-0.44488
-0.39103
-0.34639
-0.30898
-0.27731
-0.25028

k1

Estimate

Solution

k2

Exact

hy'n
-0.10000
-0.08267
-0.06949
-0.05922
-0.05107
-0.04449
-0.03910
-0.03464
-0.03090
-0.02773
-0.02503

y* n+1
0.85000
0.78524
0.72935
0.68070
0.63800
0.60026
0.56667
0.53659
0.50951
0.48501
0.46274

y*' n+1
-0.72250
-0.61660
-0.53195
-0.46335
-0.40705
-0.36031
-0.32111
-0.28793
-0.25960
-0.23523
-0.21412

h(y*'n+1 )
-0.07225
-0.06166
-0.05320
-0.04634
-0.04070
-0.03603
-0.03211
-0.02879
-0.02596
-0.02352
-0.02141

Exact
1.000000
0.909091
0.833333
0.769231
0.714286
0.666667
0.625000
0.588235
0.555556
0.526316
0.500000

The 4th order Runge-Kutta


The general form of the equations:
1
[k1 + 2k2 + 2k3 + k4 ]
6
k1 = h[ f ( x, y )]

y =


1
1
k 2 = h f x + h, y + k1
2
2


1
1
k3 = h f x + h, y + k 2
2
2

k 4 = h[ f ( x + h, y + k3 )]

CPE 310

Chapter 5 Examples part II

Runge-Kutta Method
(4th Order) Example
Consider

Exact Solution

y = 2 + 2x + x2 ex

dy
= y x2
dx

y (0 ) = 1

The initial condition is:


The step size is:

h = 0.1

The 4th order Runge-Kutta


The example of a single step:

k1 = h[ f ( x, y )] = 0.1 f (0,1) = 0.1 1 0 2 = 0.1



1
1
k 2 = h f x + h, y + k1 = 0.1 f (0.05,1.05) = 0.10475
2
2


1
1
k3 = h f x + h, y + k 2 = 0.1 f (0.05,1. + k 2 / 2 ) = 0.104988
2
2

k 4 = h[ f ( x + h, y + k3 )] = 0.1 f (0.1,1.104988) = 0.109499
yn +1 = yn +

1
[k1 + 2k2 + 2k3 + k 4 ] = 1.104829
6

CPE 310

Chapter 5 Examples part II

Runge-Kutta Method
(4th Order) Example
The values for the 4th order Runge-Kutta method
x
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1

f(x,y)

1
1.104829
1.218597
1.340141
1.468175
1.601278
1.73788
1.876246
2.014458
2.150396
2.281717

1
1.094829
1.178597
1.250141
1.308175
1.351278
1.37788
1.386246
1.374458
1.340396
1.281717

0.1
0.109483
0.11786
0.125014
0.130817
0.135128
0.137788
0.138625
0.137446
0.13404
0.128172

0.10475
0.113707
0.121503
0.128015
0.133108
0.136634
0.138427
0.138306
0.136068
0.131492
0.12433

0.104988
0.113918
0.121685
0.128165
0.133223
0.13671
0.138459
0.13829
0.135999
0.131364
0.124138

0.109499
0.117875
0.125028
0.130831
0.13514
0.137799
0.138634
0.137454
0.134046
0.128176
0.119586

Exact
1
1.104829
1.218597
1.340141
1.468175
1.601279
1.737881
1.876247
2.014459
2.150397
2.281718

Das könnte Ihnen auch gefallen