0% found this document useful (0 votes)
8 views6 pages

Simpsons 1/3 Rule C Program

Program on c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Simpsons 1/3 Rule C Program

Program on c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Simpsons 1/3 Rule C Program

Let’s understand the Simpson’s 1/3rd rule method in numerical analysis and implement Simpsons
1/3 rule in C programming language.

What is Simpsons 1/3 Rule?

The Simpson’s 1/3rd rule is used in numerical integration. Integration is the process of measuring
the area under a function plotted on a graph. The Simpson’s 1/3rd rule was developed by a
mathematician named Thomas Simpson.
The Simpson’s 1/3rd integration method is primarily used for numerical approximation of definite
integrals. This specifically means that Simpson’s integration rule is used in complex integration
calculations.
It is a method to approximately calculate the definite integral. The Simpson’s theorem is used to
find the area under a given curve. The Simpson’s method corresponds to the 3-point Newton-
Cotes quadrature rule as well.
The Simpson’s integration method is a little time consuming compared to other methods in
numerical analysis and is also a little difficult to implement computationally.

Simpson’s Rule Formula

Algorithm For Simpson’s 1/3 Rule

Step 1: Input Values For Lower Boundary, Upper Boundary and Width

Step 2: Calculate value for the formula: value = (upper_boundary - lower_boundary) / width

Step 3: Compute Width with this formula: width = (upper_boundary - lower_boundary) / value

Step 4: Consider y = f(x). Calculate the values of Y(Lower Bound to Upper Bound)

for the corresponding X(Lower Bound to Upper Bound)

Step 5: Substitute the above values in the Simpsons 1/3 Rule Formula
Note: This C Program for Simpson’s 1/3 rule is compiled with GNU GCC compiler on CodeLite
IDE. However, it is compatible with all other operating systems.
Method 1: C Program For Simpson’s 1/3rd Rule using Function

include<stdio.h>

float function(float temp);

int main()

{ int count = 0, value;

float x[30], y[30], lower_boundary, upper_boundary, width, m = 0, n = 0, result;

printf("\nEnter value for Lower Boundary:\t");

scanf("%f", &lower_boundary);

printf("\nEnter value for Upper Boundary:\t");

scanf("%f", &upper_boundary);

printf("\nEnter value for Width:\t");

scanf("%f", &width);

value = (upper_boundary - lower_boundary) / width;

if(value % 2 == 1)

{ value = value + 1;

width = (upper_boundary - lower_boundary) / value;

printf("\nModified Values:\n");

printf("\nMid Point:\t%d\n", value);

printf("\nWidth:\t%f\n", width);

printf("\nY values\n");

while(count <= value)

{ x[count] = lower_boundary + count * width;


y[count] = function(x[count]);

printf("\nY[%d] = %f", count, y[count]);

count++;

count = 1;

while(count < value)

{ if(count % 2 == 1)

m = m + y[count];

else

{ n = n + y[count];

count++; }

result = width / 3 * (y[0] + y[value] + 4 * m + 2 * n);

printf("\n\nSimpson's Rule Integration:\t%f\n", result);

return 0;}

float function(float temp)

{ return(1 / (1 + temp));

}
Trapezoidal Rule C Program
Let’s understand the trapezoidal method in numerical analysis and implement trapezoidal rule in C
programming language.

What is Trapezoidal Rule?

This numerical analysis method is used to approximating the definite integral. The trapezoidal numerical
method works on the principle of straight line approximation.

This numerical method is also popularly known as Trapezoid Rule or Trapezium Rule.
This numerical analysis method is slower in convergence as compared to Simpson’s rule in numerical
method.

Trapezium Rule Formula

This is a technique to approximate the definite integrals. It approximates the region under the graph of the
function f(x) and calculates its area.
This method becomes more accurate and outputs perfect results when periodic functions are integrated over
their periods.

Note: This C program to solve trapezoidal rule is compiled with GNU GCC compiler on Linux Ubuntu
operating system. However, this code is compatible with all other operating systems.
If you try to compile this C program for Trapezium Rule in Linux, you will get the following error:

This is because the pow() method cannot be found in the library files. To overcome this error, you will have to
explicitly include the math.h header file.
C Program To Implement Trapezoidal Rule using Function

#include<stdio.h>

#include<math.h>

float trapezoidal_rule(float x)

{ return sqrt(x);

int main()

int count, interval;

float lower_limit, upper_limit, result = 0, sum = 0, length;

printf("\nEnter the Intervals:\t");

scanf("%d", &interval);

printf("\nEnter Lower Limit:\t");

scanf("%f", &lower_limit);

printf("\nEnter Upper Limit:\t");

scanf("%f", &upper_limit);

length = (upper_limit - lower_limit) / interval;

for(count = 1; count <= interval - 1; count++)

sum = sum + trapezoidal_rule(lower_limit + count*length);

}
result = (trapezoidal_rule(lower_limit) + trapezoidal_rule(upper_limit) + 2*sum)
* (length/2);

printf("\nTrapezoidal Rule Value:\t%f\n", result);

return 0;

Output

You might also like