Damascus University 
Faculty of Information Technology Engineering 
Simpson and Lagranje Dalambair math methods
CODE: 
#include<iostream> 
#include<cmath> 
#include"graphics.h" 
using namespace std; 
void simpson(double x0,double xn,int en, double f[100][100],double& sum1) 
{ 
double h; 
double* b= new double[en] ; // F( Xi) 
h=(xn-x0)/en; // h 
//Find points 
cout<<"Now put for each point Xi .. its F(xi): n;" 
for(int i=0; i<=en;i)++ 
{ 
cout<<"F( "<<x0+(i*h;" = )"<<) 
cin>>b[i;] 
} 
//calculate the INTEGRATION!!!! 
//double sum1; 
sum1=b[0]+b[en]; // sum=f0 + fn 
for(int i=1; i<en; i)++ 
{ 
if(i % 2 !=0) 
sum1=sum1+4*b[i;] 
else 
sum1=sum1+2*b[i;] 
} 
sum1=sum1*(h/3) ;// I= h/3 [f0 + 4f1+ 2f2 + 4f3+ 2f4+ .... +fn] 
// copy to the F array 
for(int i=0;i<=en;i)++ 
{ 
f[1][i]=b[i;] 
f[0][i]=x0+i*h; 
} 
} 
void fillarr(double a[][100] , int n) 
{ 
for(int i=0; i<=1; i++) // first row X0 
for(int j=0; j<=n ; j++) //second row Y0 
{
if( i==0) 
cout<<"X "<<j;" = "<< 
else 
cout<<"Y "<<j;" = "<< 
cin>>a[i][j;] 
} 
} 
double p(double a[][100],int n , double x) 
{ 
double res,sum,temp; 
sum=0; 
for(int i=0; i<=n; i)++ 
{ 
res=1; 
for(int j=0; j<=n ; j)++ 
{ 
if(j != i) 
res=res*(x-a[0][j;)] 
} 
temp=1; 
for(int k=0;k<=n; k)++ 
{ 
if(k!=i) 
temp=temp*(a[0][i]-a[0][k;)] 
} 
res=res/temp ; 
res=res*a[1][i]; // * (y0,y1..., 
sum=sum+res; 
} 
return sum; 
} 
void print(double a[][100] , int n) 
{ 
cout<<"P"<<n<<"(x;" =) 
double temp; 
for(int i=0; i<=n;i)++ 
{ 
temp=1; 
for(int j=0; j<=n;j)++ 
{ 
if(j!=i)
temp=temp*(a[0][i]-a[0][j;)] 
} 
temp=a[1][i]/temp; 
if(temp>0 && i!=0) 
cout<<"+"<<temp; 
else if (temp==0) 
cout<<"0;" 
else 
cout<<temp; 
if(temp!=0) 
{ 
for(int k=0; k<= n ; k)++ 
{ 
if(k!= i) 
{ 
if(a[0][k]<0) 
cout<<"(x + "<<fabs(a[0][k;" )"<<)] 
else if(a[0][k]>0) 
cout<<"(x - "<<a[0][k;" )"<<] 
else 
cout<<"( x;") 
} 
} 
} 
} 
cout<<endl; 
} 
void get_xy(double wxb ,double wyb ,double wxt ,double wyt ,int vxt ,int vyt ,int vxb ,int vyb ,double wx ,double wy ,int& vx, int & vy) 
{ 
double sx,sy; 
sx=abs((vxb-vxt)/(wxt-wxb;) ) 
sy=abs((vyb-vyt)/(wyt-wyb;) ) 
vx=(vxt+( wx-wxb)*sx;) 
vy=(vyb-( wy-wyb)*sy;) 
} 
void draw_function (double a[][100] , int n) 
{ 
// Variables for graphics 
initwindow(1000, 650, "Function Ploting"); //initilize windows 
int maxx=getmaxx;)( 
int maxy=getmaxy;)( 
double x1,x2,y1,y2; 
int nx1,nx2,ny1,ny2;
double x00=-70 ,xm=70, n_graph=2000; //domain of x [x00, xm] 
double wyb=-49.8, wyt=50; 
double dx; dx=abs(xm-x00)/n_graph; 
x1=x00 ; 
y1=p(a,n-1,x1;) 
get_xy(x00,wyb,xm,wyt,0,0,maxx,maxy,x1,y1,nx1,ny1); // get x y after appropriate coordinates 
for(int i=1; i<=n_graph; i)++ 
{ 
x2=x00+(i*dx;) 
y2=p(a,n-1,x2;) 
get_xy(x00,wyb,xm,wyt,0,0,maxx,maxy,x2,y2,nx2,ny2;) 
line(nx1,ny1,nx2,ny2;) 
nx1=nx2; 
ny1=ny2; 
} 
outtextxy(30,0,"Approximetely Drawing");outtextxy(maxx-200,maxy-30,"Student:kinan-keshkeh;)" 
*/ draw Ordinates */ setcolor(10); line(0, maxy/ 2,maxx,maxy / 2); line(maxx / 2,0,maxx / 2,maxy ;) 
} 
void main_menu(int & q) 
{ 
cout<<"|----------------------------------------------------------------------|n;" 
cout<<"| CHOICE MENU |n; " 
cout<<"|-----|----|-----------------------------------------------------------|n;" 
cout<<"| |(1)-| (Interpolation)Lagranje Dalambair 'press (1) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(2)-| (Integration )Simpson 'press (2) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(0)-| Exit 'press (0) ' |n ;" 
cout<<"| --|----|------------------------------------------- |n;" 
cin>>q; 
} 
void menu2(int & q1) 
{ 
cout<<"|----------------------------------------------------------------------|n;"
cout<<"| CHOICE MENU |n; " 
cout<<"|-----|----|-----------------------------------------------------------|n;" 
cout<<"| |(1)-| calculate (Integration )Simpson 'press (1) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(2)-| Draw F(x) 'press (2) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(00)| to Main_Menu.. 'press (00) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cin>>q1; 
} 
void menu(int & y) 
{ 
cout<<"|----------------------------------------------------------------------|n;" 
cout<<"| CHOICE MENU |n; " 
cout<<"|-----|----|-----------------------------------------------------------|n;" 
cout<<"| |(1)-| Put the Xi / Yi 'press (1) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(2)-| print the Lagranj Function for this points 'press (2) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(3)-| Find Pn(b) 'press (3) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(4)-| Draw F(x) 'press (4) ' |n;" 
cout<<"| --|----|------------------------------------------- |n;" 
cout<<"| |(0)-| to Main_Menu.. 'press (0) ' |n;" 
cout<<"|-----|----|-----------------------------------------------------------|n;" 
cout<<"|----------------------------------------------------------------------|n;" 
cout<<"enter your choice;" : 
cin>>y; 
} 
void main)( 
{ 
//double v1,v2; //for draw domain [v1,v2] 
cout.setf(ios::fixed); // to print just 4 numbers after the Point 
cout.setf(ios::showpoint;) 
cout.precision(2;) 
int q; main_menu(q;) 
while (q!=0) 
{ 
switch(q) 
{
case 1{ : 
//////////////////////////////////////////////////////Lagranj///////////////////////////////////////////////////// 
int n,y; double a[100][100;] 
double x; 
menu(y); //Print the menu and get choice y 
while (y!=0) 
{ 
switch(y) 
{ 
case 1 : { cout<<" How many points?? n;" 
cout<<"put the number of points : N= n; " 
cin>>n; 
fillarr(a,n-1;) 
menu(y ;) 
break; 
} 
case 2{ : 
cout<<"Two numbers after POINT: ex (0.00) (!!more than 2 may not seen!!! )n;" 
print(a,n-1 ;) 
menu(y ;) 
break; 
} 
case 3{ : 
cout<<"Put the X0 : f(x0)....n"; cin>>x; 
cout<<"P"<<n-1<<"("<<x<<") = "<<p(a,n-1,x)<<endl; 
menu(y ;) 
break; 
} 
case 4: 
{ 
draw_function(a,n-1;) 
menu(y ;) 
break; 
} 
case 0 : { cout<<" :) :) End program My wishes :) :) !! n;" 
menu(y ;) 
break } ; 
default:{ cout<<"Error in choice !!! n ;" 
menu(y); //Print the menu and get choice y
break} ; 
// }switch 
cout<<".................................... n;" 
} 
main_menu(q;) 
break;}//case 1 
//////////////////////////////////////////////////////Simpson///////////////////////////////////////////////////// 
case 2: 
{ 
int q1; double x0,xn; int en; double sum1,sum2; 
double f[100][100;] 
menu2(q1;) 
while (q1!=0) 
{ 
switch(q1) 
{ 
case 1: 
{ 
cout<<" Put the Domain [ X0, Xn ]n;" 
cout<<" X0 = "; cin>>x0; cout<<" Xn = "; cin>>xn; 
cout<<" Put the NUmber of Domains u want to Divide into them(even) : n"; cin>>en; 
if(en %2 !=0) // en isn't Even 
{ 
cout<<"YOUR "<<en<<" is not Even and i will calculat by another way !! n;" 
cout<<"simpson(fo -->fn-1) + simpson(fn-1-->fn)n;" 
simpson(x0,xn-((xn-x0)/en),en-1,f,sum1); //fo -->fn-1 
simpson(xn-((xn-x0)/en),xn,en-1,f,sum2); //fn-1-->fn 
cout<<"The Integration fo -->fn-1 = I1 = "<<sum1<<endl; 
cout<<"The Integration fn-1-->fn = I2 = "<<sum2<<endl; 
cout<<"The Integration = I = "<<sum1+sum2<<endl; 
} 
else 
{ 
simpson(x0,xn,en,f,sum1;) 
cout<<"The Integration = I = "<<sum1<<endl; 
} 
menu2(q1;) 
break; 
}
case 2: { draw_function(f,en-1);menu2(q1); break}; 
case 00 : { break} ; 
default:{ cout<<"Error choice!!!n"; menu2(q1); break} ; 
//}switch 
} 
main_menu(q;) 
break; 
//}case 2 
case 0 : 
{ 
cout<<":) Best wishes >>>End Program>>>>>>>>>>>>>>>> n;" 
break}; 
default:{ cout<<"Error In choice!!!!!!!!! n"; main_menu(q); break } ; 
} 
} 
//while q != 0 
system("pause;)" 
}

Simpson and lagranje dalambair math methods

  • 1.
    Damascus University Facultyof Information Technology Engineering Simpson and Lagranje Dalambair math methods
  • 2.
    CODE: #include<iostream> #include<cmath> #include"graphics.h" using namespace std; void simpson(double x0,double xn,int en, double f[100][100],double& sum1) { double h; double* b= new double[en] ; // F( Xi) h=(xn-x0)/en; // h //Find points cout<<"Now put for each point Xi .. its F(xi): n;" for(int i=0; i<=en;i)++ { cout<<"F( "<<x0+(i*h;" = )"<<) cin>>b[i;] } //calculate the INTEGRATION!!!! //double sum1; sum1=b[0]+b[en]; // sum=f0 + fn for(int i=1; i<en; i)++ { if(i % 2 !=0) sum1=sum1+4*b[i;] else sum1=sum1+2*b[i;] } sum1=sum1*(h/3) ;// I= h/3 [f0 + 4f1+ 2f2 + 4f3+ 2f4+ .... +fn] // copy to the F array for(int i=0;i<=en;i)++ { f[1][i]=b[i;] f[0][i]=x0+i*h; } } void fillarr(double a[][100] , int n) { for(int i=0; i<=1; i++) // first row X0 for(int j=0; j<=n ; j++) //second row Y0 {
  • 3.
    if( i==0) cout<<"X"<<j;" = "<< else cout<<"Y "<<j;" = "<< cin>>a[i][j;] } } double p(double a[][100],int n , double x) { double res,sum,temp; sum=0; for(int i=0; i<=n; i)++ { res=1; for(int j=0; j<=n ; j)++ { if(j != i) res=res*(x-a[0][j;)] } temp=1; for(int k=0;k<=n; k)++ { if(k!=i) temp=temp*(a[0][i]-a[0][k;)] } res=res/temp ; res=res*a[1][i]; // * (y0,y1..., sum=sum+res; } return sum; } void print(double a[][100] , int n) { cout<<"P"<<n<<"(x;" =) double temp; for(int i=0; i<=n;i)++ { temp=1; for(int j=0; j<=n;j)++ { if(j!=i)
  • 4.
    temp=temp*(a[0][i]-a[0][j;)] } temp=a[1][i]/temp; if(temp>0 && i!=0) cout<<"+"<<temp; else if (temp==0) cout<<"0;" else cout<<temp; if(temp!=0) { for(int k=0; k<= n ; k)++ { if(k!= i) { if(a[0][k]<0) cout<<"(x + "<<fabs(a[0][k;" )"<<)] else if(a[0][k]>0) cout<<"(x - "<<a[0][k;" )"<<] else cout<<"( x;") } } } } cout<<endl; } void get_xy(double wxb ,double wyb ,double wxt ,double wyt ,int vxt ,int vyt ,int vxb ,int vyb ,double wx ,double wy ,int& vx, int & vy) { double sx,sy; sx=abs((vxb-vxt)/(wxt-wxb;) ) sy=abs((vyb-vyt)/(wyt-wyb;) ) vx=(vxt+( wx-wxb)*sx;) vy=(vyb-( wy-wyb)*sy;) } void draw_function (double a[][100] , int n) { // Variables for graphics initwindow(1000, 650, "Function Ploting"); //initilize windows int maxx=getmaxx;)( int maxy=getmaxy;)( double x1,x2,y1,y2; int nx1,nx2,ny1,ny2;
  • 5.
    double x00=-70 ,xm=70,n_graph=2000; //domain of x [x00, xm] double wyb=-49.8, wyt=50; double dx; dx=abs(xm-x00)/n_graph; x1=x00 ; y1=p(a,n-1,x1;) get_xy(x00,wyb,xm,wyt,0,0,maxx,maxy,x1,y1,nx1,ny1); // get x y after appropriate coordinates for(int i=1; i<=n_graph; i)++ { x2=x00+(i*dx;) y2=p(a,n-1,x2;) get_xy(x00,wyb,xm,wyt,0,0,maxx,maxy,x2,y2,nx2,ny2;) line(nx1,ny1,nx2,ny2;) nx1=nx2; ny1=ny2; } outtextxy(30,0,"Approximetely Drawing");outtextxy(maxx-200,maxy-30,"Student:kinan-keshkeh;)" */ draw Ordinates */ setcolor(10); line(0, maxy/ 2,maxx,maxy / 2); line(maxx / 2,0,maxx / 2,maxy ;) } void main_menu(int & q) { cout<<"|----------------------------------------------------------------------|n;" cout<<"| CHOICE MENU |n; " cout<<"|-----|----|-----------------------------------------------------------|n;" cout<<"| |(1)-| (Interpolation)Lagranje Dalambair 'press (1) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(2)-| (Integration )Simpson 'press (2) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(0)-| Exit 'press (0) ' |n ;" cout<<"| --|----|------------------------------------------- |n;" cin>>q; } void menu2(int & q1) { cout<<"|----------------------------------------------------------------------|n;"
  • 6.
    cout<<"| CHOICE MENU|n; " cout<<"|-----|----|-----------------------------------------------------------|n;" cout<<"| |(1)-| calculate (Integration )Simpson 'press (1) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(2)-| Draw F(x) 'press (2) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(00)| to Main_Menu.. 'press (00) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cin>>q1; } void menu(int & y) { cout<<"|----------------------------------------------------------------------|n;" cout<<"| CHOICE MENU |n; " cout<<"|-----|----|-----------------------------------------------------------|n;" cout<<"| |(1)-| Put the Xi / Yi 'press (1) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(2)-| print the Lagranj Function for this points 'press (2) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(3)-| Find Pn(b) 'press (3) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(4)-| Draw F(x) 'press (4) ' |n;" cout<<"| --|----|------------------------------------------- |n;" cout<<"| |(0)-| to Main_Menu.. 'press (0) ' |n;" cout<<"|-----|----|-----------------------------------------------------------|n;" cout<<"|----------------------------------------------------------------------|n;" cout<<"enter your choice;" : cin>>y; } void main)( { //double v1,v2; //for draw domain [v1,v2] cout.setf(ios::fixed); // to print just 4 numbers after the Point cout.setf(ios::showpoint;) cout.precision(2;) int q; main_menu(q;) while (q!=0) { switch(q) {
  • 7.
    case 1{ : //////////////////////////////////////////////////////Lagranj///////////////////////////////////////////////////// int n,y; double a[100][100;] double x; menu(y); //Print the menu and get choice y while (y!=0) { switch(y) { case 1 : { cout<<" How many points?? n;" cout<<"put the number of points : N= n; " cin>>n; fillarr(a,n-1;) menu(y ;) break; } case 2{ : cout<<"Two numbers after POINT: ex (0.00) (!!more than 2 may not seen!!! )n;" print(a,n-1 ;) menu(y ;) break; } case 3{ : cout<<"Put the X0 : f(x0)....n"; cin>>x; cout<<"P"<<n-1<<"("<<x<<") = "<<p(a,n-1,x)<<endl; menu(y ;) break; } case 4: { draw_function(a,n-1;) menu(y ;) break; } case 0 : { cout<<" :) :) End program My wishes :) :) !! n;" menu(y ;) break } ; default:{ cout<<"Error in choice !!! n ;" menu(y); //Print the menu and get choice y
  • 8.
    break} ; //}switch cout<<".................................... n;" } main_menu(q;) break;}//case 1 //////////////////////////////////////////////////////Simpson///////////////////////////////////////////////////// case 2: { int q1; double x0,xn; int en; double sum1,sum2; double f[100][100;] menu2(q1;) while (q1!=0) { switch(q1) { case 1: { cout<<" Put the Domain [ X0, Xn ]n;" cout<<" X0 = "; cin>>x0; cout<<" Xn = "; cin>>xn; cout<<" Put the NUmber of Domains u want to Divide into them(even) : n"; cin>>en; if(en %2 !=0) // en isn't Even { cout<<"YOUR "<<en<<" is not Even and i will calculat by another way !! n;" cout<<"simpson(fo -->fn-1) + simpson(fn-1-->fn)n;" simpson(x0,xn-((xn-x0)/en),en-1,f,sum1); //fo -->fn-1 simpson(xn-((xn-x0)/en),xn,en-1,f,sum2); //fn-1-->fn cout<<"The Integration fo -->fn-1 = I1 = "<<sum1<<endl; cout<<"The Integration fn-1-->fn = I2 = "<<sum2<<endl; cout<<"The Integration = I = "<<sum1+sum2<<endl; } else { simpson(x0,xn,en,f,sum1;) cout<<"The Integration = I = "<<sum1<<endl; } menu2(q1;) break; }
  • 9.
    case 2: {draw_function(f,en-1);menu2(q1); break}; case 00 : { break} ; default:{ cout<<"Error choice!!!n"; menu2(q1); break} ; //}switch } main_menu(q;) break; //}case 2 case 0 : { cout<<":) Best wishes >>>End Program>>>>>>>>>>>>>>>> n;" break}; default:{ cout<<"Error In choice!!!!!!!!! n"; main_menu(q); break } ; } } //while q != 0 system("pause;)" }