runge-kutta methods - wordpress.com · 2018-05-26 · problem write a user-defined matlab function...

23
Runge-Kutta Methods Luis Sanchez

Upload: others

Post on 11-Apr-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Runge-Kutta Methods

Luis Sanchez

Page 2: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Second Order Runge-Kutta Method

The general form of second-order Runge-Kutta methods is:

where c1, c2, a2 , and b21 are constants. The values of these

constants vary with the specific second-order method.

Con:

Page 3: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Modified Euler method in the form of a second-order Runge-Kutta method

For the modified Euler method, the constants are:

Substituting these constants yields:

Page 4: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Midpoint method in the form of a second-orderRunge-Kutta method

For the midpoint method, the constants are:

Substituting these constants yields:

Page 5: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Heun's method

In Heun's method the constants are:

Substituting these constants yields:

Page 6: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Example 1Second Order Runge-Kutta Method in form of modified Euler

8254.3)92.1618(2

01.04

2

92.16))01.()18.0(1(),(

0.18)1(),(

:1 Step

2101

3

0

2

01002

3

0

2

0001

KKh

yy

xyhKyhxfK

xyyxfK

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

4,1

1),(

0.01h

00

32

yx

xyyxf

Page 7: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Example 1Second Order Runge-Kutta Method: Modified Euler

6648.3)45.1566.16(2

01.08254.3

2

45.15))01.()1666.0(1(),(

66.16)1(),(

:2 Step

2112

3

1

2

11112

3

1

2

1111

KKh

yy

xyhKyhxfK

xyyxfK

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

8254.3,01.1

1),(

0.01h

11

32

yx

xyyxf

Page 8: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Example 1Summary of the solution

6648.30212

8254.30111

0000.40010

.

.

.

yxi ii

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

Summary of the solution

Page 9: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Numerically Solving ODE in Matlab

[1,2]. interval over the,4)1(,1

:Problem

32 yxydx

dy

• Step 1: Create a M-file for dy/dx as firstode.m

function yprime=firstode(x,y);

yprime=1+y^2+x^3;

Using Euler modified with h=0.01, 0.02 y 0.5

Page 10: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Step 2: Create a M-file to implement Euler modified. The Matlab program must

return two column vectors, the first with values of x and the second with value of y.

Page 11: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Step 3: At the Matlab command window>> a = 1; b = 2;

>> h1 = 0.01; yini = -4;

>> [X1,Y1 ]=odeRK2EulerModified(@firstode,a,b,h1,yini);

>> h1 = 0.02;

>> [X2,Y2 ]=odeRK2EulerModified(@firstode,a,b,h2,yini);

>> h1 = 0.5;

>> [X3,Y3 ]=odeRK2EulerModified(@firstode,a,b,h3,yini);

>> plot(X1,Y1,'*r',X2,Y2,'*g',X3,Y3,'*b’)

>> fprintf('%4.2f %4.6f \n',X1,Y1)

Page 12: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Runge-Kutta Methods

321

213

12

1

46

1)()(

)2,2

1(

)2

1,

2

1(

),(

(RK3) Kutta RungeOrder Third

KKKxyhxy

hKhKyhxfK

hKyhxfK

yxfK

ii

ii

ii

Page 13: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

ProblemConsider the following first-order ODE:

(a) Solve with the Heun's method using h = 0.5, 1.

(b) Solve with the classical third-order Runge-Kutta method

using h = 1.

The analytical solution of the ODE is:

In each part, calculate the error between the true solution and the

numerical solution at the points where the numerical solution is

determined.

Page 14: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Classical Fourth-Order Runge-Kutta Method

The most popular RK methods are fourth-order, and the most commonly used form is:

where:

yi1 yi 1

6k1 2k2 2k3 k4 h

k1 f ti, yi

k2 f ti 1

2h, yi

1

2k1h

k3 f ti 1

2h, yi

1

2k2h

k4 f ti h, yi k3h

Page 15: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Exercise

Solving by hand a first-order ODE using the fourth-order Runge-Kutta method.

Using h = 0.5.

Solution:

The first point of the solution is (0,3), which is the point where the initial

condition is given. The values of x and y at the first point are x1=0 and y1=3.

The rest of the solution is done in steps. In each step the next value of the

independent variable is calculated by:

Page 16: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

The value of the dependent variable Yi+1 is calculated by first

evaluating K1, K2, K3 and K4 using:

And then substituting the Ks:

First step: In the first step i = 1

Page 17: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Second step: In the second step i = 2

Page 18: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Third step: In the third step i = 3

Page 19: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method
Page 20: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Problem

Write a user-defined MATLAB function that solves a first-order

ODE using the classical fourth order Runge-Kutta method.

Solution

To solve the problem, a user-defined MATLAB function called odeRK4, which solves a

first-order initial value ODE, is written. The function is then used in a script file, which

also generates a plot that shows a comparison between the numerical and the exact

solutions. The ODE itself is written in a separate user-defined function that is used by the

odeRK4 function.

Using h=0.05.

Page 21: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method
Page 22: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

Funcion ODEFxy

>> a=0; b=2.5;>> h=0.05; yini=3;>> [x,y]=odeRK4(@ODEFxy,a,b,h,yini);Solusion Exacta>> xp=a:0.1:b;>>yp=70/9*exp(-0.3*xp)-43/9*exp(-1.2*xp);>>plot(x,y, '*r' ,xp,yp)

At the Matlab command

window

Page 23: Runge-Kutta Methods - WordPress.com · 2018-05-26 · Problem Write a user-defined MATLAB function that solves a first-order ODE using the classical fourth order Runge-Kutta method

ProblemAn inductor L = 15 H and a resistor R = 1000 ohms are connected in series with an AC power source providing voltage of V = 10sin(2πνt) Vots, where ν= 100 kHz, as shown in the figure. The current I(t) in the circuit is determined from the solution of the equation:

Solve the equation and plot the current as a function of

time for 0 <= t<=1 x 10-4 s with I(0) = 0. Using h=10-9 s.