Write a program to demonstrate Simpson's 1/3 rule in C language.
Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
3
/* Program to execte Simpson's 1/3 rule*/
4
#include<stdio.h>
5
#include<conio.h>
6
#include<math.h>
7
float calc ( float );
8
void main()
9
{
10
float x0,xn,h,y;
11
int i,sn;
12
printf("Enter values of x0, xn and number of sub-intervals: ");
13
scanf("%f%f%d", &x0, &xn, &sn);
14
if(sn%2==0) {
15
16
h=(xn-x0)/sn;
17
y=calc(x0) + calc(xn);
18
for(i =1; i < sn; i ++) {
19
if(i%2==0)
20
y +=2*calc(x0+i*h);
21
else
22
y +=4*calc(x0+i*h);
23
}
24
y=(h/3)*y;
25
26
printf("Value of integral is %f",y);
27
getch();
28
} else
29
printf("Unsuitable number of intervals");
30
}
31
float calc(float x)
32
{
33
return 1/(1+x*x);
34
}
35
36
37
Output
1
2
Enter values of x0, xn and number of sub-intervals: 0 6 6
3
Value of integral is 1.366174
4
Rate the Code
Reviews are published after a quick safety check (usually within 24 hours).
Reviews are public. Developers can see basic device specifications to respond. Learn more
How to run Simpson's 1/3 Rule program in C-Language?
To run Simpson's 1/3 Rule program in C-Language, follow the below mentioned steps:
Copy the Code: Press the copy button in the code block to copy the complete code.
Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
Paste the code: Paste the copied code into Turbo C++’s editor.
Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
Enter the Input: Provide the required inputs or your desired inputs, if applicable.
Receive the Result: Get the result of the program on the screen based on the provided inputs.
More about Simpson’s 1/3 Rule
Discovered by: English mathematician Thomas Simpson
Discovered Year: 1743 (published in the book Dissertation of a Variety of Physical and Analytical Subjects by himself)
Simpson’s 1/3 Rule calculates the approximate area under the curve. But this rule is more accurate than the Trapezoidal Rule.
How does Simpson’s 1/3 Rule work?
The curve is divided into small, even parts with the help of curves (parabolic arcs) instead of a straight line to get a more accurate result.
Use the below-mentioned formula resultuse the formula image
If n=6 intervals, then the number of points from y0to y6will be 7.
And y1, y3, y5, and so on are odd, and y2, y4, y6, and so on are the even points.
Why is Simpson’s 1/3 Rule used?
To get a more accurate area under the curve by dividing the small intervals with parabolic arcs instead of a straight line.
Real-life use case of Simpson’s 1/3 Rule
Naval Architecture & Shipbuilding (Vessel Volume): To find out the irregular cross-sectional area and total displacement volume for the ships, marine engineers use Simpson’s 1/3 Rule.
Mechanical & Civil Engineering: To find out the area, mass, or center of gravity of an irregular-shaped object.
Physics & Thermodynamics: To find out the area of energy with variable force or varying pressure (P-v) graph.
Limitations of Simpson’s 1/3 Rule
Even Interval (parts) should exist: Simpson’s 1/3 Rule applies only if and only if the sub-parts of the curved area are an even number.
Equal Spacing (h): the gap between the intervals should be same else the method will fail.
Alternate Methods
Simpson’s 3/8 Rule: More accurate method than Simpson’s 1/3 Rule because it uses cubic polynomials.
Note: sometimes Simpson’s 1/3 Rule and Simpson’s 3/8 Rule can both be used to get an extremely accurate result.
Gaussian Quadrature/ Romberg Integration: if data is uneven or you want to get ultra-high precision, then these advanced adaptive integration methods are used.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.
Submit your modified code
Your submissions are published after a quick safety check (usually within 7 days).
Submissions are public. Developers can see basic device specifications to respond. Learn more
How to submit your modified code?
If you are a student, teacher, or anyone who wants to submit your code for credit, then follow the steps below to easily submit your code to the dhimanp platform.
Important Note:
Code will be accepted with zero errors.
All code should be text only (no file attachments allowed).
Make sure you have all the code in Notepad.
Code should include comments and proper formatting so that your code is easily understandable.
Below are the steps:
Add Program Purpose and Your Details: Open Notepad, add the purpose of the program on line 1, and add your full Name, school or college name, and city name on line 2 as comments. You may skip the school or college name if you don’t have one.
Add Your Code: Copy your code and paste it into Notepad on line 3.
Optional for More Files: If you have more than one file, then paste each file's code into Notepad one by one, with the file name too, from line 3.
Insert code into the Page Review Input box: Copy the Notepad text content and paste this content into the review input box below.
Submit: Enter your name in the input field (name is optional here) and click on the Submit button.
1
// Program: numerical analysis simpson one third rule program
2
// Submitted by: Aarav Sharma, ABC Public School, Delhi
3
4
//file1.c
5
#include <stdio.h>
6
int main()
7
{
8
// Your code here
9
return 0;
10
}
How do we verify and give credit?
After receiving your submitted code, we will review it, verify it, and run your program.
If your program is 100% safe, secure, and unique, then we will add the code as a new Page with proper definitions and description details of this code.
We will add your full name, school or college name, and city as you provide.
This process may take up to 1 week.
Disclaimer
The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.
Privacy Policy
If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.
We are not using this data to track you and are not selling this data to anyone.
If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.