Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define f(x) (sin(x)-log(x)+exp(x)) //1.4 0.2 0.1
- // #define f(x) x*x*x*x //3 -3 1
- int main()
- {
- float b,a,h,x[20],sum=0;
- int n;
- printf("Enter the values of upper limit,lower limit and h\n");
- scanf("%f %f %f",&b,&a,&h);
- n = ceil(((b-a)/h));
- for(int i=0 ; i<=n ; i++){
- x[i] = a+ (i*h);
- }
- for(int i=1 ; i<n ; i++){
- sum += f(x[i]);
- }
- float ans = (h/2) * ( f(x[0]) + f(x[n]) + (2*sum) ) ;
- printf ("\nFinal answer is %f",ans);
- return 0;
- }
- /*
- Output 1
- Enter the values of upper limit,lower limit and h
- 1.4 .2 .1
- Final answer is 4.056173
- Output 2
- Enter the values of upper limit,lower limit and h
- 3 -3 1
- Final answer is 115.000000
- */
- Algo
- define function
- read a,b and h , where a lower , b upper limit and h size of interval
- n = (b-a)/h + 1
- declare float array x of size of n
- start a loop from i=0 to n-1
- x[i] = a + (i*h)
- end i loop
- calulate sum of all ordinates excluding extreme
- declare sum = 0
- start loop from i=1 to n-2
- sum = sum + f(x[i])
- end loop i
- ans = h/2 * ( f(x[0]) + f(x[n-1]) + (2*sum)
- print ans
- stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement