Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
200 views

Unit 4 C Programs: Trapezoidal Method Algorithm, Flowchart and Code in C

The document describes algorithms and C code implementations for numerical integration methods. It includes the trapezoidal method, Simpson's 1/3 rule, and Simpson's 3/8 rule. The trapezoidal method splits the area into trapezoids and sums their individual areas. Simpson's rules approximate the function as a parabola between points. The C code examples implement these methods to calculate the integral of sample functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
200 views

Unit 4 C Programs: Trapezoidal Method Algorithm, Flowchart and Code in C

The document describes algorithms and C code implementations for numerical integration methods. It includes the trapezoidal method, Simpson's 1/3 rule, and Simpson's 3/8 rule. The trapezoidal method splits the area into trapezoids and sums their individual areas. Simpson's rules approximate the function as a parabola between points. The C code examples implement these methods to calculate the integral of sample functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C PROGRAM FOR NUMERICAL METHODS UNIT 4

UNIT 4 C PROGRAMS

TRAPEZOIDAL METHOD ALGORITHM, FLOWCHART AND CODE IN C


The basic working principle of the trapezoidal method c program is that trapezoidal method splits the
area under the curve into a number of trapeziums. Then, area of each trapezium is calculated, and all
the individual areas are summed up to give the total area under the curve which is the value of
definite integral.

C program for Trapezoidal method - integral graph


In the above figure, area under the curve between the points x 0 and xn is to be determined. In order
to determine the area, we divide the total interval (xn– x0) into ‘n’ small interval each of length ‘h’:
h=(xn– x0)/n
After that, the C source code for trapezoidal method uses the following formula to calculate the value
of definite integral:

C program for Trapezoidal method - integration formula


Trapezoidal Method Algorithm:
1. Start
2. Define and Declare function
3. Input initial boundary value, final boundary value and length of interval
4. Calculate number of strips, n = (final boundary value –final boundary value)/length of
interval
5. Perform following operation in loop
x[i]=x0+i*h
y[i]=f(x[i])
print y[i]
6. Initialize se=0, s0=0
7. Do the following using loop
If i %2 = 0

COMPILED BY: ER.SARBESH CHAUDHARY 1


C PROGRAM FOR NUMERICAL METHODS UNIT 4

s0=s0+y[i]
Otherwise
Se=se+y[i]

8. ans= h/3*(y[0]+y[n]+4*s0+2*se)
9. print the ans
10. stop
Trapezoidal Method Flowchart:

COMPILED BY: ER.SARBESH CHAUDHARY 2


C PROGRAM FOR NUMERICAL METHODS UNIT 4

The source code for trapezoidal method given below is short and simple to understand. It uses a user
defined function to calculate the value of function i.e f(x) =1 /(1 + x2). The variable data type used in
the program are integer and float types. There are only three variables to be input to the program –
x0 ( initial boundary value), xn ( final boundary value) and h (step size).
Source Code for Trapezoidal Method in C:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
COMPILED BY: ER.SARBESH CHAUDHARY 3
C PROGRAM FOR NUMERICAL METHODS UNIT 4

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}

Input/Output:

SIMPSON’S 1/3 RULE ALGORITHM AND CODE IN C


In the source code below, a function f(x) = 1/(1+x) has been defined. The calculation using Simpson
1/3 rule in C is based on the fact that the small portion between any two points is a parabola. The
program follows the following steps for calculation of the integral.

 As the program gets executed, first of all it asks for the value of lower boundary value of x i.e.
x0, upper boundary value of x i.e. xn and width of the strip, h.
 Then the program finds the value of number of strip as n=( xn – x0 )/h and checks whether it
is even or odd. If the value of ‘n’ is odd, the program refines the value of ‘h’ so that the value
of ‘n’ comes to be even.
 After that, this C program calculates value of f(x) i.e ‘y’ at different intermediate values of ‘x’
and displays values of all intermediate values of ‘y’.
 After the calculation of values of ‘c’, the program uses the following formula to calculate the
value of integral in loop.
 Integral = *((y0 + yn ) +4(y1 + y3 + ……….+ yn-1 ) + 2(y2 + y4 +……….+ yn-2 ))
 Finally, it prints the values of integral which is stored as ‘ans’ in the program.

COMPILED BY: ER.SARBESH CHAUDHARY 4


C PROGRAM FOR NUMERICAL METHODS UNIT 4

If f(x) represents the length, the value of integral will be area, and if f(x) is area, the output of Simpson
1/3 rule C program will be volume. Hence, numerical integration can be carried out using the program
below; it is very easy to use, simple to understand, and gives reliable and accurate results.
f(x) = 1/(1+x2)
Source code for Simpson’s 1/3 Rule in C:
/**********************************

****** Simpson’s 1/3 Rule *******


**********************************/

#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x*x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}

COMPILED BY: ER.SARBESH CHAUDHARY 5


C PROGRAM FOR NUMERICAL METHODS UNIT 4

else
{
se=se+y[i];
}

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);

getch();
}
Input/Output:

SIMPSON’S 3/8 RULE CODE IN C

Source Code for Simpson’s 3/8 Rulein C:

//integration of given function using Simpson's 3/8 rule


#include<stdio.h>
float y(float x)
{
return 1/(1+x*x); //function of which integration is to be calculated
}
int main()
{

COMPILED BY: ER.SARBESH CHAUDHARY 6


C PROGRAM FOR NUMERICAL METHODS UNIT 4

float x0,xn,h,s;
int i,n,j,flag;
printf("Enter x0, xn, no. of subintervals n: ");
scanf("%f%f%d",&x0,&xn,&n);
h = (xn-x0)/n;
s = y(x0)+y(xn);
for(i = 1; i<=n-1;i++){
for(j=1;j<=n-1;j++){
if(i==3*j){
flag = 1;
break;
}
else
flag = 0;
}
if(flag==0)
s += 3*y(x0+i*h);
else
s += 2*y(x0+i*h);
}
printf("\n\nValue of integral is %6.4f\n",(3*h/8)*s);
return 0;
}

Input/Output:

THE END
COMPILED BY: ER.SARBESH CHAUDHARY 7

You might also like