Ordinary Differential Equations
Ordinary Differential Equations
Experiment-5
Ordinary Differential Equations
(Duration: 120 mins)
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).
y n +1 = y n + h ∗ g ( x n , y n ) (2)
First step:
y1 = y0 + h ∗ g ( x0 , y0 ) (3)
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
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)
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)