You are on page 1of 17

APPLIED NUMERICAL

ANALYSIS LABORATORY
JADAVPUR UNIVERSITY
DEPARTMENT OF CHEMICAL
ENGINEERING
CLASS: UG-II
SECTION: A-1
GROUP NUMBER: 4
GROUP MEMBERS:
DEBASMITA KUMAR
(ROLL:001310301014)
ANJIK
CHOWDHURY(ROLL:001310301015)
ASHWIN
AGARWAL(ROLL:001310301016)

BIPRATEEP
BANERJEE(ROLL:001310301017)
DATE OF SUBMISSION: - 23/02/2015

DAY 5
PROBLEM STATEMENT:
In these problems use different time steps. Plot the
results using any package and compare with
explicit Eulers technique.
The empirical equation for the worlds population, y
dy
as a function of time is given by dt =4.25713 10 y , y =10
12

2.010101

The base year is 1840 A.D. Obtain the worlds


population at different years henceforth.

Debasmita Kumar

MATHEMATICAL BACKGROUND
EXPLICIT EULER METHOD
Suppose that we want to approximate the solution of the initial value
problem
y ' ( t )=f (t , y )

where y=f (t)


y ( t0 ) = y 0
Choose a value h for the size of every step and set
t n=t 0+ n h

Now, one step of the Euler method from


The value of

yn

t n t n+1=t n +h .

is an approximation of the solution to the ODE at time

tn
y n y ( tn )

The Euler method is explicit, i.e. the solution


of

yi

y n+1

is an explicit function

for i n .

Note that the method increments a solution through an interval h while


using derivative information from only the beginning of the interval. As a
2
result, the step's error is O(h ) .
Derivation
The Euler method can be derived in a number of ways. Firstly, there is the
geometrical description mentioned above.
Another possibility is to consider the Taylor expansion of the function
about

The differential equation states that


. If this is substituted in
the Taylor expansion and the quadratic and higher-order terms are
ignored, the Euler method arises. The Taylor expansion is used below to
analyze the error committed by the Euler method, and it can be extended
to produce RungeKutta methods.
A closely related derivation is to substitute the forward finite
difference formula for the derivative,

in the differential equation


. Again, this yields the Euler
method. A similar computation leads to the midpoint rule and
the backward Euler method.
Finally, one can integrate the differential equation from
apply the fundamental theorem of calculus to get:

to

and

Now approximate the integral by the left-hand rectangle method (with


only one rectangle):

Combining both equations, one finds again the Euler method. This line of
thought can be continued to arrive at various linear multistep methods.
IMPLICIT EULER METHOD
Consider the ordinary differential equation
y ' ( t )=f (t , y )

with initial value


and

y0

y ( t 0 ) = y 0 . Here the function f and the initial data

are known; the function y depends on the real variable t and is

unknown. A numerical method produces a sequence


that

yk

t0

approximates y (t 0+ kh) , where

y 0 , y 1 , y 2 , ..

such

is called the step size.

The backward Euler method computes the approximations using


y k+1 = y k + hf (t k+1 , y k+1 )

The backward Euler method is an implicit method: the new


approximation y k+1 appears on both sides of the equation, and thus the
method needs to solve an algebraic equation for the unknown
Sometimes, this can be done by fixed-point iteration:

y k+1 .

[i +1]
[i ]
y [0]
k+1 = y k , y k +1 = y k +hf (t k +1 , y k+1 )

If this sequence converges (within a given tolerance), then the method


takes its limit as the new approximation

Alternatively, one can use (some modification of) the NewtonRaphson


method to solve the algebraic equation.
Derivation
Integrating the differential equation
yields

from

to

Now approximate the integral on the right by the right-hand rectangle


method (with one rectangle):
Finally, use that
is supposed to approximate
the backward Euler method follows.

and the formula for

ALGORITHM
Step 1: Start

Step 2: Declaring variables steps, x0, n, i of type int and h, y0 of type


long double
Step 3: Read value for h
Step 4: if(h>1) then GOTO Step 3
Step 5: Read values for x0, n, y0
Step 6: Declare arrays population[3][n] and year[n] of type int
Step 7: i=0
Step 8: Read value for year[i]
Step 9: steps=(year[i]-x0)/h
Step 10: Declare variables y1, y2, f of type long double and j=1 of
type int
Step 11: y1= y0
Step 12: f= 4.25713*10^(-12)* y1^ 2.010101
Step 13: y2= y1+(h*f)
Step 14: y1=y2
Step 15: j=j+1
Step 16: if(j<=steps) then GOTO Step 12
Step 17: population[1][i]=y2
Step 18: Declare variables j, flag of type int and y1, y2, x0, xn, fx,
fdx, fxn of type
long double

Step 19: y1=y0


Step 20: x0=y1
Step 21: define F(y) y-(h*(4.25713*pow(10,-12)*pow(y,2.010101)))y1
Step 22: define FD(y) 1-(2.010101*h*(4.25713*pow(10,12)*pow(y,1.010101)))
Step 23: j=1
Step 24: fx=F(x0)
Step 25: fdx=FD(x0)
Step 26: xn=x0-fx/fdx
Step 27: if (fabs((xn-x0)/xn) < 0.00001) then flag=0
else x0=xn
Step 28: if (flag=1) GOTO Step 24
Step 29: x0=y1
Step 30: y2=xn
Step 31: y1=y2
Step 32: j=j+1
Step 33: if(j<=steps) GOTO Step 24
Step 34: population[2][i]=y2
Step 35: y=0.99010/pow((8.030*pow(10,-10)-(4.25713*pow(10,12)*(year-x0))),0.99)
Step 36: population[3][i]=y2
Step 37: i=i+1
Step 38: if(i<=steps) GOTO Step 8
Step 39: Print year[n] and population[3][n] arrays
Step 40: Stop

SOURCE CODE (C++)


//Day-5
//Program to solve a differential equation by Explicit Euler and Implicit Euler methods
#include <iostream>
#include <math.h>
#include <iomanip>
#define EPS 0.00001
#define F(y) y-(h*(4.25713*pow(10,-12)*pow(y,2.010101)))-y1
#define FD(y) 1-(2.010101*h*(4.25713*pow(10,-12)*pow(y,1.010101)))
using namespace std;
//Function to solve the differential equation by Explicit Euler method
long double Explicit_Euler(long double y0, long double h, int steps)
{
int j;
long double f, y1, y2;
y1=y0;
//for-loop to solve differential equation by Explicit Euler method
for(j=1;j<=steps;j++)
{
f= 4.25713*pow(10,-12)*pow(y1, 2.010101);
y2= y1+(h*f);
y1=y2;
}
return y1;
}
//Function to solve the differential equation by Implicit Euler method
long double Implicit_Euler(long double y0, long double h, long int steps)
{
long int j,flag=1;
long double y1, y2, x0,xn,fx, fdx,fxn;

y1=y0;
x0=y1;
//do-loop to solve the differential equation by Implicit Euler method
for(j=1;j<=steps;j++)
{
//do-loop to find the root by Newton-Raphson method
do
{
fx=F(x0);
fdx=FD(x0);
xn=x0-fx/fdx;
if (fabs((xn-x0)/xn) < EPS)
flag=0;
else
x0=xn;
}while(flag==1);//do-loop ends

x0=y1;
y2=xn;
y1=y2;
}//for loop ends
}

return y2;

//Function to find analytical solution of differential equation


long double Exact_Solution(long int year,long int x0)
{
long double y;
y=0.99010/pow((8.030*pow(10,-10)-(4.25713*pow(10,-12)*(year-x0))),0.99);
return y;
}
//main function
int main()
{
//Declaring variables
//steps stores the number of steps for which iterations should be computed
//x0 is the base year
//n is the number of years for which population has to calculated
//i is a counter
long int steps, x0, n, i;
//h stores the step size
//y0 stores the population in the base year
long double h, y0;
cout<<"We have the empirical equation for the world's population as a function of
time\nThis is given as a differential equation:\n\ndy/dt=4.25713*10^12*y^2.010101"<<endl;
cout<<"\nThis program solves this differential equation.\nWe will use the Explicit and
Implicit Euler methods.\nFinally we will tabulate these along with the analytical
solution."<<endl;
cout<<"\n\nEnter the step size for Explicit Euler calculation. \nIt should ideally be a
number less than 1. \nSmaller step size gives more accurate results."<<endl;
//do-loop to check for step size
do
{
cin>>h;

if(h>1)
cout<<"\nUse a smaller step size"<<endl;
}while(h>1);//do-loop ends
cout<<"\nEnter base year"<<endl;
cin>>x0;
cout<<"\nEnter number of years for which you want to calculate population"<<endl;
cin>>n;
cout<<"\nEnter population for base year"<<endl;
cin>>y0;
/*population is an array that stores the population computed by Explicit, Implicit Euler methods
and the analytical solution*/
//year is an array to store the years for which population is calculated
int population[3][n];
int year[i];
cout<<"\nEnter the years for which you want to calculate population"<<endl;

//for-loop to store values of population in the array


for(i=0;i<n;i++)
{
cin>>year[i];
steps=(year[i]-x0)/h;
population[1][i]=Explicit_Euler(y0,h,steps);
population[2][i]=Implicit_Euler(y0,h,steps);
population[3][i]=Exact_Solution(year[i], x0);
}//for loop ends
cout<<"\nThe populations for the respective years calculated by 3 methods
are:"<<endl;
cout<<"\nYear Explicit Euler Implicit Euler Exact Solution"<<endl;
//for-loop to print the population values in a tabular form
for(i=0;i<n;i++)
{
cout<<year[i]<<setw(3)<<" "<<population[1][i]<<setw(7)<<" "<<population[2]
[i]<<
setw(7)<<" "<<population[3][i]<<endl;
}//for loop ends
return 0;
}

SAMPLE OUTPUT
We have the empirical equation for the world's population as a
function of time
This is given as a differential equation:
dy/dt=4.25713*10^-12*y^2.010101
This program solves this differential equation.
We will use the Explicit and Implicit Euler methods.
Finally we will tabulate these along with the analytical solution.
Enter the step size for Explicit Euler calculation.
It should ideally be a number less than 1.
Smaller step size gives more accurate results.
0.01
Enter base year
1840

Enter number of years for which you want to calculate population


10
Enter population for base year
1000000000
Enter the years for which you want to calculate population
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
The populations for the respective years calculated by 3 methods
are:
Year

Explicit Euler

Implicit Euler

Exact Solution

1850 1055403622

1055409962

1055434158

1860 1117342958

1117357587

1117380944

1870 1187049399

1187074933

1187096783

1880 1266084756

1266124748

1266144133

1890 1356460126

1356519490

1356535016

1900 1460810100

1460895750

1460905362

1910 1582654857

1582776742

1582777361

1920 1726806704

1726979511

1726966413

1930 1900023325

1900269394

1900235139

1940 2112102149

2112456737

2112389196

-------------------------------Process exited after 27.82 seconds with return value 0


Press any key to continue . . .

PLOT

YEAR

ANALYTIC
AL
SOLUTION

185
0
1860

10554341
58
11173809
44
11870967
83
12661441
33
13565350
16
14609053
62
15827773
61
17269664
13
19002351
39
21123891
96

1870
1880
1890
1900
1910
1920
1930
1940

Population vs Year(Analytical Solution)


2200000000
2000000000
1800000000
1600000000
1400000000
1200000000
1000000000
1840

1860

1880

1900

1920

EXACT SOLUTION

1940

1960

Population vs Year (Explicit Euler Method)


2200000000
2000000000
1800000000
1600000000
1400000000
1200000000
1000000000
1840

1860

1880

1900

1920

1940

EXPLICIT EULER

YEA
R

EXPLICIT
EULER

1850

105540362
2
111734295
8
118704939
9
126608475
6
IMPLICIT
135646012
EULER
6
146081010
105540996
0
1582654852
111735758
7
1726806707
118707493
4
1900023323
126612474
5
2112102148
135651949
9
0
146089575
0
158277674
2
172697951
1
190026939
4
211245673
7

1860
1870
1880
YEAR
1890
1900
1850
1910
1860
1920
1870
1930
1880
1940
1890
1900
1910
1920
1930
1940

1960

Population vs Year(Implicit Euler Method)


2200000000
2000000000
1800000000
1600000000
1400000000
1200000000
1000000000
1840

1860

1880

1900

1920

1940

1960

IMPLICIT EULER

COMMENTS
A few points should be noted about the program.
Firstly, the selection of step size is important for the accuracy of
the solution for both methods. The smaller the step size the
lesser is the error or deviation from the analytical solution. That
is why an if-check has been used to see if the step size input by
the user is sufficiently small or not.
For the Implicit Euler method, we need to use the NewtonRaphson method to solve for y n+1 . For Newton-Raphson method,
we take the initial value as the population for the base year
since we know that the root will be something of the same order.
Also for the computations involved in the Implicit Euler Method,
we have pre-defined two variables F(y) and FD(y) at the
beginning of the program. This avoids unnecessarily defining
new functions or making the existing ones cumbersome. The
condition for termination of Newton Raphson is that the
consecutive values of the iterate have a difference less than the
tolerance value.
For the analytical solution, the function for y was calculated from
before by simple integration. Now with the values of time input
by the user, we can find the value of population by simple
substitution.

In order to avoid the use of too many variables and also to easily
print the population values in a tabular form for the 3 different
methods we use an array to store the values of population and
an array for the years for which the user wishes to calculate the
projected value of population.
The advantage of using the Implicit Euler Method lies in the fact
that for a stiff problem it requires lesser number of computations
as compared to the Explicit Euler Method to reach the solution.
Hence we may say that the Implicit Euler method is slightly more
accurate than the Explicit Euler method.

You might also like