Gill’s 4th Order Method to solve Differential Equations
Last Updated :
15 Nov, 2021
Given the following inputs:
- An ordinary differential equation that defines the value of dy/dx in the form x and y.
- Initial value of y, i.e., y(0).
The task is to find the value of the unknown function y at a given point x, i.e. y(x).
Examples:
Input: x0 = 0, y = 3.0, x = 5.0, h = 0.2
Output: y(x) = 3.410426
Input: x0 = 0, y = 1, x = 3, h = 0.3
Output: y(x) = 1.669395
Approach:
Gill’s method is used to find an approximate value of y for a given x. Below is the formula used to compute the next value yn+1 from the previous value yn.
Therefore:
yn+1 = value of y at (x = n + 1)
yn = value of y at (x = n)
where
0 ? n ? (x - x0)/h
h is step height
xn+1 = x0 + h
The essential formula to compute the value of y(n+1):
The formula basically computes the next value yn+1 using current yn:
- K1 is the increment based on the slope at the beginning of the interval, using y.
- K2 is the increment based on the slope, using
- K3 is the increment based on the slope, using
- K4 is the increment based on the slope, using
The method is a fourth-order method, meaning that the local truncation error is of the order of O(h5).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float dydx( float x, float y)
{
return (x - y) / 2;
}
float Gill( float x0, float y0,
float x, float h)
{
int n = ( int )((x - x0) / h);
float k1, k2, k3, k4;
float y = y0;
for ( int i = 1; i <= n; i++) {
k1 = h * dydx(x0, y);
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1);
k3 = h * dydx(x0 + 0.5 * h,
y + 0.5 * (-1 + sqrt (2)) * k1
+ k2 * (1 - 0.5 * sqrt (2)));
k4 = h * dydx(x0 + h,
y - (0.5 * sqrt (2)) * k2
+ k3 * (1 + 0.5 * sqrt (2)));
y = y + (1.0 / 6) * (k1 + (2 - sqrt (2)) * k2
+ (2 + sqrt (2)) * k3 + k4);
x0 = x0 + h;
}
return y;
}
int main()
{
float x0 = 0, y = 3.0,
x = 5.0, h = 0.2;
printf ( "y(x) = %.6f" ,
Gill(x0, y, x, h));
return 0;
}
|
Java
class GFG{
static double dydx( double x, double y)
{
return (x - y) / 2 ;
}
static double Gill( double x0, double y0,
double x, double h)
{
int n = ( int )((x - x0) / h);
double k1, k2, k3, k4;
double y = y0;
for ( int i = 1 ; i <= n; i++)
{
k1 = h * dydx(x0, y);
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1);
k3 = h * dydx(x0 + 0.5 * h,
y + 0.5 * (- 1 + Math.sqrt( 2 )) *
k1 + k2 * ( 1 - 0.5 * Math.sqrt( 2 )));
k4 = h * dydx(x0 + h,
y - ( 0.5 * Math.sqrt( 2 )) *
k2 + k3 * ( 1 + 0.5 * Math.sqrt( 2 )));
y = y + ( 1.0 / 6 ) * (k1 + ( 2 - Math.sqrt( 2 )) *
k2 + ( 2 + Math.sqrt( 2 )) *
k3 + k4);
x0 = x0 + h;
}
return y;
}
public static void main(String[] args)
{
double x0 = 0 , y = 3.0 ,
x = 5.0 , h = 0.2 ;
System.out.printf( "y(x) = %.6f" , Gill(x0, y, x, h));
}
}
|
Python3
from math import sqrt
def dydx(x, y):
return (x - y) / 2
def Gill(x0, y0, x, h):
n = ((x - x0) / h)
y = y0
for i in range ( 1 , int (n + 1 ), 1 ):
k1 = h * dydx(x0, y)
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1)
k3 = h * dydx(x0 + 0.5 * h,
y + 0.5 * ( - 1 + sqrt( 2 )) *
k1 + k2 * ( 1 - 0.5 * sqrt( 2 )))
k4 = h * dydx(x0 + h, y - ( 0.5 * sqrt( 2 )) *
k2 + k3 * ( 1 + 0.5 * sqrt( 2 )))
y = y + ( 1 / 6 ) * (k1 + ( 2 - sqrt( 2 )) *
k2 + ( 2 + sqrt( 2 )) *
k3 + k4)
x0 = x0 + h
return y
if __name__ = = '__main__' :
x0 = 0
y = 3.0
x = 5.0
h = 0.2
print ( "y(x) =" , round (Gill(x0, y, x, h), 6 ))
|
C#
using System;
class GFG{
static double dydx( double x, double y)
{
return (x - y) / 2;
}
static double Gill( double x0, double y0,
double x, double h)
{
int n = ( int )((x - x0) / h);
double k1, k2, k3, k4;
double y = y0;
for ( int i = 1; i <= n; i++)
{
k1 = h * dydx(x0, y);
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1);
k3 = h * dydx(x0 + 0.5 * h,
y + 0.5 * (-1 + Math.Sqrt(2)) *
k1 + k2 * (1 - 0.5 * Math.Sqrt(2)));
k4 = h * dydx(x0 + h,
y - (0.5 * Math.Sqrt(2)) *
k2 + k3 * (1 + 0.5 * Math.Sqrt(2)));
y = y + (1.0 / 6) * (k1 + (2 - Math.Sqrt(2)) *
k2 + (2 + Math.Sqrt(2)) *
k3 + k4);
x0 = x0 + h;
}
return y;
}
public static void Main(String[] args)
{
double x0 = 0, y = 3.0,
x = 5.0, h = 0.2;
Console.Write( "y(x) = {0:F6}" , Gill(x0, y, x, h));
}
}
|
Javascript
<script>
function dydx(x, y)
{
return (x - y) / 2;
}
function Gill(x0, y0, x, h)
{
let n = ((x - x0) / h);
let k1, k2, k3, k4;
let y = y0;
for (let i = 1; i <= n; i++)
{
k1 = h * dydx(x0, y);
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1);
k3 = h * dydx(x0 + 0.5 * h,
y + 0.5 * (-1 + Math.sqrt(2)) *
k1 + k2 * (1 - 0.5 * Math.sqrt(2)));
k4 = h * dydx(x0 + h,
y - (0.5 * Math.sqrt(2)) *
k2 + k3 * (1 + 0.5 * Math.sqrt(2)));
y = y + (1.0 / 6) * (k1 + (2 - Math.sqrt(2)) *
k2 + (2 + Math.sqrt(2)) *
k3 + k4);
x0 = x0 + h;
}
return y;
}
let x0 = 0, y = 3.0,
x = 5.0, h = 0.2;
document.write( "y(x) = " , Gill(x0, y, x, h).toFixed(6));
</script>
|
Time Complexity: O(n3/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...