0% found this document useful (0 votes)
27 views4 pages

Ordinary Differential Equations

Uploaded by

batuhanabali0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Ordinary Differential Equations

Uploaded by

batuhanabali0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EE204 - Introduction to Scientific Programming for Engineers Experiment - 5

Experiment-5
Ordinary Differential Equations
(Duration: 120 mins)

Author: Ertunga Burak Koçal


[email protected]

Purpose: In this experiment, you are going to approximate solution of an ordinary differential equation
using different methods.

Introduction
The Runge-Kutta methods are family of implicit and explicit iterative methods for obtaining numeri-
cal approximations to solutions of ordinary differential equations.
Let an initial value problem be specified as below:
dy
= g( x, y), y ( x0 ) = y0 (1)
dx
dy
where y is an unknown function of x and dx is the rate at which y changes. The value of y( xlast ) can be
approximated using g( x, y), x0 , y0 and chosen step size (h).

The Euler Method


The Euler method is the simplest Runge-Kutta method with one stage.
-Pick a step-size h > 0 and calculate yn+1 for n = 0, 1, 2, 3... using

y n +1 = y n + h ∗ g ( x n , y n ) (2)

First step:
y1 = y0 + h ∗ g ( x0 , y0 ) (3)

The Midpoint Method


The midpoint method is a second-order Runge-Kutta method with two stages.
-Pick a step-size h > 0 and calculate yn+1 for n = 0, 1, 2, 3... using
h h ∗ g( xn , yn )
y n +1 = y n + h ∗ g ( x n + , y n + ) (4)
2 2

The Runge-Kutta Method


The Runge-Kutta method is also known as "classic Runge-Kutta method".
-Pick a step-size h > 0 and calculate yn+1 for n = 0, 1, 2, 3... using

k1 = g( xn , yn ) (5)
h hk
k2 = g( xn + , yn + 1 ) (6)
2 2
h hk2
k3 = g( xn + , yn + ) (7)
2 2
k4 = g( xn + h, yn + hk3 ) (8)
h ∗ (k1 + 2k2 + 2k3 + k4 )
y n +1 = y n + (9)
6

Department of Electrical and Electronics Engineering Izmir Institute of Technology


EE204 - Introduction to Scientific Programming for Engineers Experiment - 5

Problem Statement
Solve the following initial value problem over the interval [0,2] using stepSize(h) = 0.1 and y(0) = 1.

dy
= yx3 − 0.1y (10)
dx
Analytical solution is:
4 /4)−0.1x
y( x ) = e( x (11)

Lab Procedure
We highly recommend you to follow lab procedure without skipping any step and read each step
thoroughly before you start.
1- Ask x0 , y0 , xlast and stepSize values from user in main function. (10 pts) These values will be used
in both Euler and midpoint methods.
dy
2- Write two separate functions to calculate y( x ) and dx = g( x, y). Call these functions in main and
print calculated values for x = 0.2 and y = 0.8 values. (20 pts)
y(0.2) = 0.9805 g(0.2,0.8) = -0.0736

double fY(double x)
double fYdx(double x, double y)

Note:You should be able to extract the y values (yEuler,yMidpoint,yRK4) from the corresponding
functions to print them in the main.
3- Implement Euler method and check its operation. Do not use any loop to call Euler function more
than one time in main; you should call it only once. The number of steps should be determined
using x0 , xlast and stepSize and you should alocate proper amount of memory to store obtained results
considering the number of steps. Check Fig:1 (15 pts)

void euler(double (*fYdx1)(double, double), double *yEuler, double xFirst, double yFirst,
double xLast, double stepSize)

4- Implement midpoint method and check its operation. Do not use any loop to call midpoint function
more than one time in main; you should call it only once. The number of steps should be determined
using x0 , xlast and stepSize and you should alocate proper amount of memory to store obtained results
considering the number of steps. Check Fig:2 (30 pts)

void midpoint(double (*fYdx2)(double, double), double *yMidpoint, double xFirst, double


yFirst, double xLast, double stepSize)

5- Implement Runge-Kutta method and check its operation. Do not use any loop to call Runge-Kutta
function more than one time in main; you should call it only once. The number of steps should be
determined using x0 , xlast and stepSize and you should alocate proper amount of memory to store
obtained results considering the number of steps. Check Fig:3 (15 pts)

void RK4(double (*fYdx3)(double, double), double *yRK4, double xFirst, double yFirst, double
xLast, double stepSize)

6- Print calculated values obtained with all methods and function you wrote for y( x ) in the second step
for each iteration with values x0 = 0, y0 = 1, xlast = 2 and stepSize = 0.1. Comment on results. Check
Fig:4 (10 pts)

Department of Electrical and Electronics Engineering Izmir Institute of Technology


EE204 - Introduction to Scientific Programming for Engineers Experiment - 5

Figure 1: y( x ) values obtained with stepSize = 0.5 -Euler Method

Figure 2: y( x ) values obtained with stepSize = 0.5 -Midpoint Method

Department of Electrical and Electronics Engineering Izmir Institute of Technology


EE204 - Introduction to Scientific Programming for Engineers Experiment - 5

Figure 3: y( x ) values obtained with stepSize = 0.5 -Runge-Kutta Method

Figure 4: y( x ) values obtained with stepSize = 0.1

Department of Electrical and Electronics Engineering Izmir Institute of Technology

You might also like