Euler Method 1

Post on 14-Jul-2016

16 views 1 download

description

penerapan metode euler untuk fisika komputasi dengan matlab

Transcript of Euler Method 1

Numerical Methodsto Solve Ordinary

Differential Equationsby

Agus Kartono

Most often, systems of differential equations can not be solved analytically.

Therefore, it is important to be able to approach the problem in other ways.

Today there are numerical methods that produce numerical approximations to solutions of differential equations.

Here, we introduce the oldest and simplest such method, originated by Euler about 1768.

Ordinary Differential Equations

Leonhard Euler (1707-1783).

Euler methods are simple methods of solving first-order ODE, particularly suitable for quick programming because of their great simplicity, although their accuracy is not high.

Euler methods include three versions, namely:

1) forward Euler method2) modified Euler method3) backward Euler method

EULER Methods

Often a Taylor series is used to approximate a solution which is then truncated.

Taylor Series:

Taylor Series

ixx

i dxdfxf

1

ixx

i dxfdxf

2

22

...

!3!23

32

21

1 hxf

hxf

hxfxfxf iiiii

The forward Euler method for is derived by rewriting the forward difference approximation:

or

where is used.

Forward Euler Method

hxfxyhxyxyxy iiiii '1

hyxfxyxy iiii ,1

yxfy ,'

iii yxfxy ,'

The typical steps of forward Euler's method are given below:

Step 1. define f(x, y)Step 2. input initial values x0 and y0Step 3. input step sizes h and number of steps nStep 4. calculate x and y:for i=0:nx(i+1)=x(i)+hy(i+1)=y(i)+h*f(x(i),y(i))endStep 5. output x and yStep 6. end

Steps for MATLAB implementation

As an application, consider the following initial value problem:

,

which was chosen because we know the analytical solution and we can use it for check. Its exact or analytical solution is found to be:

Example:

yxyxf

dxdyy ,' 10 y

12 xxy

m file in MATLABfunction [x,y]=euler_forward(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem% Euler forward method% File prepared by Agus Kartono% Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x = zeros(n+1,1); y = zeros(n+1,1); x(1) = xinit; y(1) = yinit; % Calculation of x and y for i=1:n x(i+1)=x(i)+h; y(i+1)=y(i)+h*f(x(i),y(i)); end plot(x,y);

Running m file in MATLABfunction f = f(x,y)f=x/y;

>>[x,y]=euler_forward(@f,0,1,1,10)

First, the modified Euler method is more accurate than the forward Euler method.

Second, it is more stable. It is derived by applying the trapezoidal rule to the solution of .

Modified Euler Method

yxfy ,'

iiiiii yxfyxfhxyxy ,,2 111

The typical steps of modified Euler's method are given below:

Step 1. define f(x, y)Step 2. input initial values x0 and y0Step 3. input step sizes h and number of steps nStep 4. calculate x and y:for i=1:nx(i+1)=x(i)+hynew= y(i)+h*f(x(i),y(i))y(i+1)=y(i)+ h/2*(f(x(i),y(i))+f(x(i+1),ynew));endStep 5. output x and yStep 6. plot (x,y)

Steps for MATLAB implementation

m file in MATLABfunction [x,y]=euler_modified(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem% Euler modified method% File prepared by Agus Kartono% Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x = zeros(n+1,1); y = zeros(n+1,1); x(1) = xinit; y(1) = yinit; % Calculation of x and y for i=1:n x(i+1)=x(i)+h; ynew=y(i)+h*f(x(i),y(i)); y(i+1)=y(i)+(h/2)*(f(x(i),y(i))+f(x(i+1),ynew)); end plot(x,y);

The backward Euler method is based on the backward difference approximation and written as:

The accuracy of this method is quite the same as that of the forward Euler method.

Backward Euler Method

111 , iiii yxhfxyxy

The typical steps of backward Euler's method are given below:

Step 1. define f(x, y)Step 2. input initial values x0 and y0Step 3. input step sizes h and number of steps nStep 4. calculate x and y:for i=1:nx(i+1)=x(i)+hynew= y(i)+h*f(x(i),y(i))y(i+1)=y(i)+h*f(x(i+1),ynew)endStep 5. output x and yStep 6. plot (x,y)

Steps for MATLAB implementation

m file in MATLABfunction [x,y]=euler_backward(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem% Euler backward method% File prepared by Agus Kartono% Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x = zeros(n+1,1); y = zeros(n+1,1); x(1) = xinit; y(1) = yinit; % Calculation of x and y for i=1:n x(i+1)=x(i)+h; ynew=y(i)+h*f(x(i),y(i)); y(i+1)=y(i)+h*f(x(i+1),ynew); end plot(x,y);

Second-Order Runge-Kutta:Estimation of the slope is the derivative at the mid-point between t and t+∆t. Figure in below illustrates this idea. The equations are then:

Runge-Kutta Algorithms

ttxfk nn ,1

tttkxfk nn

2

,21

2

21 ktxtx nn

As an application, consider the following initial value problem:

,

which was chosen because we know the analytical solution and we can use it for check. Its exact or analytical solution is found to be:

Example:

xttxf

dtdxx ,' 10 x

12 ttx

Figure: Principle of the Second-order Runge-Kutta Method

The typical steps of Second-Order Runge-Kutta 's method are given below:

Step 1. define f(t, x)Step 2. input initial values x0 and t0Step 3. input step sizes ∆t = h and number of steps mStep 4. calculate x and t:for n=1:mt(n+1)=t(n)+hk1= h*f(x(n),t(n))k2= h*f(x(n)+k1/2,t(n)+h/2)x(n+1)=x(n)+k2endStep 5. output t and xStep 6. plot (t,x)

Steps for MATLAB implementation

m file in MATLABfunction rk2_new % Runge-Kutta approximation for ODE initial value problem% Second-order Runge-Kutta method% File prepared by Agus Kartono% Calculation of h from tinit, tfinal, and m tinit = 0;xinit = 1;tfinal = 1;m = 10;h=(tfinal-tinit)/m; % Initialization of x and t as column vectors x = zeros(m+1,1); t = zeros(m+1,1); x(1) = xinit; t(1) = tinit; % Calculation of x and t for i=1:m t(i+1)=t(i)+h; k1=h*frk(x(i),t(i)); k2=h*frk(x(i)+h/2,t(i)+k1/2); x(i+1)=x(i)+k2; end plot(t,x);

The formula for the fourth order Runge-Kutta method (RK4)

function rungekuttah = 0.5;t = 0;w = 0.5;fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);for i=1:4k1 = h*frk4(t,w);k2 = h*frk4(t+h/2, w+k1/2);k3 = h*frk4(t+h/2, w+k2/2);k4 = h*frk4(t+h, w+k3);w = w + (k1+2*k2+2*k3+k4)/6;t = t + h;fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);end

function v = frk4(t,y)v = y-t^2+1;

function rk45epsilon = 0.00001;h = 0.2;t = 0;w = 0.5;i = 0;fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);while t<2h = min(h, 2-t);k1 = h*f(t,w);k2 = h*f(t+h/4, w+k1/4);k3 = h*f(t+3*h/8, w+3*k1/32+9*k2/32);k4 = h*f(t+12*h/13, w+1932*k1/2197-7200*k2/2197+7296*k3/2197);

k5 = h*f(t+h, w+439*k1/216-8*k2+3680*k3/513-845*k4/4104);k6 = h*f(t+h/2, w-8*k1/27+2*k2-3544*k3/2565+1859*k4/4104-11*k5/40);w1 = w + 25*k1/216+1408*k3/2565+2197*k4/4104-k5/5;w2 = w + 16*k1/135+6656*k3/12825+28561*k4/56430-9*k5/50+2*k6/55;R = abs(w1-w2)/h;delta = 0.84*(epsilon/R)^(1/4);if R<=epsilont = t+h;w = w1;i = i+1;fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);h = delta*h;elseh = delta*h;endend

function v = frk45(t,y)v = y-t^2+1;