Saturday 15 September 2012

Program to Implement Traezodial and simpson methods.


Traezodial method:

Algorithm:

Step 1. Read x1, x2, e { x1 and x2 are the two end points of  the 
            internal the allowed error in integral is e}

Step 2. h=x2-x1

Step 3. SI = (f(x1) + f(x2))/2;

Step 4. I = h-si

Step 5. i=1 Repeat

Step 6. x=x1 + h/2

Step 7. for J= 1 to I do

Step 8. SI= SI + f(x)

Step 9. x=x+h
Endfor

Step 10. i=21

Step 11. h=h/2 { Note that the internal has been halved above and 
             the number of points where the function has to be computed     
             is doubled}

Step 12.i0=i1

Step 13. i1 = h.si

Step 14. until / I1-i0 / <=c./i1/

Step 15. Write I1,h,i

Step 16. Stop

 Program:


#include<stdio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,integral;
int i;
clrscr();
printf("enter the value ofa,b,n:");
scanf("%f %f %f",&a,&b,&n);
printf("enter the values of x:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
x[0]=a;
for(i=1;i<=n-1;i++)
{
x[i]=x[i-1]+h;
sum=sum+2*y[i];
}
sum=sum+y[b];
integral=sum*(h/2);
printf("approximate integral value is: %f",integral);
getch();
}












Input/Output:

Enter the values of a,b,n
1
2
3
Enter the values of x:
1
2
3
Enter the values of y:
1
2
3
Approximate integral value is 2.166667
Conclusion: The program is error free






























           Simpsons Method:

   Algorithm:

Step 1. Read x1,x2,e

Step 2. h=(x2-x1)/2

Step 3. i=2

Step 4. si=f(x1) + f(x2)

Step 5. s2=0

Step 6. s4=f(x1+h)

Step 7. I0=0

Step 8.  In =(s+4s4). (h/3)
             Repeat

Step 9. s2=s2+s4 {s2 stores already computed functional value and s4 the value computed in   the new    nitration }

Step 10. s4=0

Step 11. x=x1+h/2

Step 12. for j=1 to I do

Step 13. s4=s4+f(x)

Step 14. x=x+h

Step 15. h=h/2

Step 16. i=2i

Step 17. io=in

Step 18. in= (s1+2s2+4s4) . (h/3)
Step 19. until |In-Io|≤e. /in

Step 20. Write In,h,i

Step 21. STOP

                Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,itgl;
int i;
clrscr();
printf("enter the values of a,b,n");
scanf("%f%f%f",&a,&b,&n);
printf("enter the values of x");
for(i=0;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y");
for(i=0;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
a=x[0];
b=x[n];
for(i=0;i<=(n-2);i++)
{
x[i]=x[i]+h;
if(i%2==0)
{
sum=sum+4*y[i];
}
else
{
sum=sum+2*y[i];
}
}
itgl=sum*(h/3);
printf("integral value%f",itgl);
getch();
}





Input/Output:

Enter the values of a,b,n
1
2
3
Enter the value of x
4
5
6
7
Enter the values of y
8
9
1
2
Integral value is 5.555556


Conclusion: The program is error free

VIVA QUESATIONS
1)  Define Binary search ?
Ans: Binary search is a vast improvement over the sequential search. For binary search to work, the item in the list  must be in assorted order. The approach employed in the binary search is divid and conquer. If the list to be sorted for a specific item is not sorted, binary search fails.



No comments:

Post a Comment