You are on page 1of 6

EXPERIMENT NO. 5 CALCULATE AREA UNDER THE CURVE USING SIMPSONS 1/3 RULE.

#include<iostream.h> #include<stdio.h> #include<conio.h> float f(float x) { return 1/(1+x*x); } void main() { clrscr(); float x0,xn,h,y; int i,n; cout<<"enter the values of x0,xn and n i.e.(no. of subintervals)\n"; cin>>x0>>xn>>n; h=(xn-x0)/n; y=f(x0)+f(xn); for(i=1;i<n;i++) { if(i%2!=0) { y+=4*f(x0+i*h); } else { y+=2*f(x0+i*h); } } cout<<"area under the curve is : "<<(h/3)*y<<endl; getch(); }

OUTPUT

EXPERIMENT NO. 6 CALCULATE AREA UNDER THE CURVE USING TRAPEZOIDAL RULE

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<iomanip.h> float f(float x) { return 1/(1+x*x); } void main() { clrscr(); float x0,xn,h,y; int i,n; cout<<"enter the values of x0,xn and n i.e. (no. of subintervals)\n"; cin>>x0>>xn>>n; h=(xn-x0)/n; y=f(x0)+f(xn); for(i=1;i<n;i++) { y+=2*f(x0+i*h); } cout<<"area under the curve is : "<<(h/2)*y<<endl; getch(); }

OUTPUT

EXPERIMENT NO. 7 CALCULATE THE INTERPOLATING VALUES OF A FUNCTION USING LAGRANGES INTERPOLATION METHOD .

#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); float ax[50],ay[50],nr,dr,x,y=0; int i,j,n; cout<<"enter the no. of sets of observation : "; cin>>n; cout<<"enter the set of values : "<<endl; cout<<"x"<<setw(2)<<"y"<<endl; for(i=0;i<n;i++) cin>>ax[i]>>setw(2)>>ay[i]; cout<<"\nenter the value of x for which the value of y is wanted : "; cin>>x; for(i=0;i<n;i++) { nr=dr=1; for(j=0;j<n;j++) { if(j!=i) { nr*=x-ax[j]; dr*=ax[i]-ax[j]; } } y+=(nr/dr)*ay[i]; } cout<<"\nwhen x="<<x<<", y="<<y<<endl; getch(); }

OUTPUT

You might also like