Sie sind auf Seite 1von 7

CS ASSIGNMENT 10

BY: BHAVESH MENDHEKAR


1501 ME 15

Q.1
CODE:
#include<stdio.h>
#include<math.h>
float func(float x)
{
float g = 1 + pow(x,2);
return (1/g);
}

int main()
{
float x0,xn,h,s;
int i,n;
printf("Enter x0, xn, no. of subintervals: ");
scanf("%f %f %d",&x0,&xn,&n);
h = (xn-x0)/n;

s = func(x0) + func(xn);
for(i = 1; i < n; i++)
{
s += 2*func(x0+i*h);
}
printf("Value of integral 1/(1 + x^2)is %f\n",(h/2)*s);
return 0;
}

ALGORITHM:
1.
2.
3.
4.
5.
6.

Write math.h header file.


In main function, read x0, xn, n.
Calculate h.
Value of integral by trapezoidal rule = x (y0 + 2y1 + 2y2 + + 2yn1 + yn)/2.
In separate function, return value of the 1/(1+x^2).
Print the integral value.

Q.2
CODE:
# include <stdio.h>
# include <math.h>
float func(float x)
{
return 1/(1 + pow(x,2));
}
int main()
{
float x0,xn,h,s,x1;
int i,n;

printf("Enter x0, xn, no. of intervals");


scanf("%f %f %d", &x0, &xn, &n);
if(n%2==0)
{
h = (xn - x0)/n;
x1=x0+h;
s = func(x0) + func(xn);
for(i=0; i<n; i+=2)
{
s+= 2*func(x0+i*h);
}
for(i=1; i<n; i+=2)
{
s+= 4*func(x1+i*h);
}
printf("Final value of ntegration is %f", (h/3)*s);
}
else
{
}
}

printf("not valid for odd intervals. please enter even no. of intervals");
return 0;

ALGORITHM:
1.
2.
3.
4.

Write math.h header file.


In the main function, read x0, xn, n.
If n is odd, print Simpsons 1/3 rule is not valid.
If n is even, integral = h/3 [(f(0) + f(n) + 2(f2 + . + f(n-2)) + 4(f(1) +.. +

f(n-1)) ].
5. In a separate function, return the values of 1/(1+ x^2).
6. Print the value of integral.

Q.3
CODE:

# include <stdio.h>
# include <math.h>
float func(float x)
{
return 1/(1+pow(x,2));
}

int main()
{
float x0,xn,h,s,x1;
int i,n;
printf("Enter x0, xn");
scanf("%f %f %d", &x0, &xn);
h = (xn - x0)/3;
s = func(x0) + func(xn) + 3*func((2*x0+xn)/3) + 3*func((x0+2*xn)/3);

printf("Final value of integration is %f", (3*h/8)*s);


return 0;
}

ALGORITHM:
1. Write math.h header file.
2. In a separate function, return the values of 1/(1+ x^2).
3. In the main function, read x0, xn, n.

4. Calculate h = (xn-x0)/3.
5. Integral = (xn-x0)[ f(x0) + 3f{(2x0+xn)/3} + 3f{(x0+2Xn)/3} + f(xn)]/8.
6. Print the value of the integral.

Q.4
CODE:
#include <stdio.h>
#include <math.h>
float func(float x)
{
return 1/(1 + pow(x,2));
}

int main()
{
float x0,xn,h,h1,s1,s2,s,x1;
int i,n;
printf("Enter x0, xn, no. of subintervals: ");
scanf("%f %f %d",&x0,&xn,&n);

h = (xn-x0)/n;
s = func(x0) + func(xn);
for(i = 1; i < n; i++)
{

s += 2*func(x0+i*h);
}
printf("Value of integral 1/(1 + x^2) by trapezoidal rule is %f\n",(h/2)*s);

if(n%2==0)
{

x1=x0+h;
s1 = func(x0) + func(xn);
for(i=0; i<n; i+=2)
{
s1+= 2*func(x0+i*h);
}
for(i=1; i<n; i+=2)
{
s1+= 4*func(x1+i*h);
}
printf("Value of integral 1/(1 + x^2) by simpson's 1/3 rule is %f\n", (h/3)*s1);
}

else
{
printf("simpson 1/3 rule is not valid for odd intervals. please enter even no. of
intervals");

h1 = (xn - x0)/3;
s2 = func(x0) + func(xn) + 3*func((2*x0+xn)/3) + 3*func((x0+2*xn)/3);

printf("Value of integral 1/(1 + x^2) by simpson's 3/8 rule is %f", (3*h1/8)*s2);

return 0;
}

ALGORITHM:
1.
2.
3.
4.
5.
7.
8.
9.

Write math.h header file.


In a separate function, return the values of 1/(1+ x^2).
In the main function, read x0, xn, n.
Calculate h = (xn-x0)/n.
Calculate h1 = (xn-xo)/3.
Integral by trapezoidal rule = x (y0 + 2y1 + 2y2 + + 2yn1 + yn)/2.
Check for validity of simpsons 1/3 rule. Only valid if n is even, otherwise not.
Integral by simpsons 1/3 rule = = h/3 [(f(0) + f(n) + 2(f2 + . + f(n-2)) +

4(f(1) +.. + f(n-1)) ].


7. Integral by simpsons 3/8 rule = (xn-x0)[ f(x0) + 3f{(2x0+xn)/3} +
3f{(x0+2Xn)/3} + f(xn)]/8.
10. Print the results.

Das könnte Ihnen auch gefallen