Numerical Differentiation
(FDM, CDM, & BDM)
Objective : - To write a C++ Program for Numerical Differentiation using Forward Difference Method(FDM), Central Difference Method(CDM) & Backward Difference Method(BDM).
Theory :
Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
long float fun(long float a)
{
long float r=(a*a*a); //Enter Your Function Here
return r;
}
void main()
{
clrscr();
char c1;
int s;
long float x,dx,ffd=0,fbd=0,fcd=0,sfd=0,sbd=0,scd=0;
cout<<setprecision(5);
cout<<"\n 1. First Order Differentiation \n 2. Second Order Differentiation ";
cout<<"\n Enter Your Choice -----> ";
cin>> s;
clrscr();
switch (s)
{
case 1:
{
cout<<"\n Enter the Value of X : ";
cin>>x;
do
{
cout<<"\n Enter the value of dx : ";
cin>>dx;
clrscr();
cout<<"\n\n ****** I Order Differentation ****** \n\n";
ffd=(fun(x+dx)-fun(x))/dx;
fbd=(fun(x)-fun(x-dx))/dx;
fcd=(fun(x+dx)-fun(x-dx))/(2*dx);
cout<<" The Value of X is :"<<x<<endl;
cout<<" The Value of dX is :"<<dx<<endl;
cout<<" Forward Difference "<<ffd<<endl;
cout<<" Backward Difference "<<fbd<<endl;
cout<<" Central Difference "<<fcd<<endl;
cout<<" \n Do you Want to continiue with different value of dX (Y/N) :";
cin>>c1;
clrscr();
}while(c1== 'y');
break;
} //End of Case 1
case 2:
{
cout<<"\n Enter the Value of X : ";
cin>>x;
do
{
cout<<"\n Enter the value of dx : ";
cin>>dx;
clrscr();
cout<<"\n\n ****** II Order Differentation ****** \n\n";
sfd=(fun(x+(2*dx))-(2*fun(x+dx))+fun(x))/(dx*dx);
sbd=(fun(x)-(2*fun(x-dx))+fun(x-(2*dx)))/(dx*dx);
scd=(fun(x+dx)-(2*fun(x))+fun(x-dx))/(dx*dx);
cout<<" The Value of X is :"<<x<<endl;
cout<<" The Value of dX is :"<<dx<<endl;
cout<<" Forward Difference "<<sfd<<endl;
cout<<" Backward Difference "<<sbd<<endl;
cout<<" Central Difference "<<scd<<endl;
cout<<" \n Do you Want to continiue with different value of dX (Y/N) :";
cin>>c1;
clrscr();
}while(c1== 'y');
break;
}//End of Case 2
default :
{
cout<<" ****** Invalid Selection ******";
break;
}
}//End of Switch Case
getch();
} // End of Main Program