Beruflich Dokumente
Kultur Dokumente
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.
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("not valid for odd intervals. please enter even no. of intervals");
return 0;
ALGORITHM:
1.
2.
3.
4.
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);
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);
return 0;
}
ALGORITHM:
1.
2.
3.
4.
5.
7.
8.
9.