0% found this document useful (0 votes)
11 views4 pages

NumericalIntegration Assignment-2

This assignment requires the implementation of the Trapezoid Rule and Simpson's Rule for numerical integration using C++ or Python. Students will apply these methods to test functions, compare their accuracy, and analyze how the number of sub-intervals affects error. The assignment includes specific programming tasks, output formatting requirements, and a grading rubric, emphasizing individual work and academic integrity.

Uploaded by

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

NumericalIntegration Assignment-2

This assignment requires the implementation of the Trapezoid Rule and Simpson's Rule for numerical integration using C++ or Python. Students will apply these methods to test functions, compare their accuracy, and analyze how the number of sub-intervals affects error. The assignment includes specific programming tasks, output formatting requirements, and a grading rubric, emphasizing individual work and academic integrity.

Uploaded by

PlasmaYT
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NUMERICAL METHODS

Programming Assignment
Numerical Integration: Trapezoid Rule & Simpson's Rule

Course: Numerical Methods Assignment #: 02


Student Name: Student ID:
______________________________ ______________________________
Due Date: Total Marks: 100 Points
______________________________

1. Overview
In this assignment you will implement two fundamental numerical integration techniques — the
Trapezoid Rule and Simpson's Rule — using either C++ or Python. You will apply both methods
to a set of test functions, compare their accuracy, and analyse the trade-offs between them.
By the end of this assignment you should be able to:
•​ Explain the mathematical foundation of the Trapezoid and Simpson's rules.
•​ Implement both methods from scratch (no built-in integration libraries).
•​ Evaluate and compare numerical accuracy against exact analytical results.
•​ Investigate how the choice of n (number of sub-intervals) affects error.

2. Mathematical Background
2.1 Trapezoid Rule
The Trapezoid Rule approximates the definite integral by dividing [a, b] into n equal
sub-intervals of width h = (b - a) / n and summing the areas of the trapezoids formed:
∫[a,b] f(x) dx ≈ (h/2) [ f(x₀) + 2f(x₁) + 2f(x₂) + … + 2f(x_{n-1}) +
f(x ) ]
The global truncation error is O(h²), meaning doubling n roughly quarters the error.

2.2 Simpson's Rule (Composite 1/3 Rule)


Simpson's Rule fits a parabola through every three consecutive points. n must be even:
∫[a,b] f(x) dx ≈ (h/3) [ f(x₀) + 4f(x₁) + 2f(x₂) + 4f(x₃) + … +
4f(x_{n-1}) + f(x ) ]
The global truncation error is O(h⁴) — far more accurate than the Trapezoid Rule for smooth
functions.
3. Programming Requirements
3.1 Language Choice
You must implement your solution in ONE of the following languages:
•​ C++ (standard C++11 or later)

3.2 What to Implement


Task 1 — Trapezoid Rule Function (20 pts)
Write a function/method that accepts f (function), a (lower bound), b (upper bound), and n
(number of intervals) and returns the numerical integral using the Trapezoid Rule.

Task 2 — Simpson's Rule Function (20 pts)


Write a function/method that accepts the same parameters as above and returns the numerical
integral using Composite Simpson's Rule. Enforce that n must be even; if an odd n is supplied,
increment it by 1 and display a warning.

Task 3 — Test Functions & Results (25 pts)


Apply both methods to ALL three test functions below using n = 4, 10, 100, and 1000. For each
case print: method used, function, n, numerical result, exact result, and absolute error.
# Function f(x) Interval [a, b] Exact Value
1 f(x) = x² [0, 1] 1/3 ≈ 0.333333

2 f(x) = sin(x) [0, π] 2.000000

3 f(x) = e^x [0, 1] e − 1 ≈


1.718282
4. Required Program Output
Your program must produce clearly formatted console output. Below is a sample output format
— yours should look similar:
============================================
Numerical Integration Results
============================================
Function: f(x) = x^2 Interval: [0, 1]
--------------------------------------------
Method n Result
--------------------------------------------
Trapezoid 4 0.343750
Trapezoid 10 0.335000
Trapezoid 100 0.333350
Trapezoid 1000 0.333334
Simpson 4 0.333333
Simpson 10 0.333333
============================================

5. Submission Instructions
1.​ Submit a single source file named: NumericalIntegration_<StudentID>.cpp or
NumericalIntegration_<StudentID>.py
2.​ The file must compile / run without errors or external dependencies beyond the standard
library.
3.​ Include your full name and student ID in a comment block at the top of the file.
4.​ Include clear inline comments explaining each step of the algorithm.
5.​ Upload through the course portal before the deadline. Late submissions lose 10 points
per day.

6. Grading Rubric

Criterion Max Points Score


Trapezoid Rule Implementation 20
Simpson's Rule Implementation 20
Correctness of Results 25
Code Quality & Comments 15
Comparison & Analysis Report 15
Submission & Formatting 5
Total 100
7. Academic Integrity
You must complete this assignment individually. You may discuss general concepts with
classmates, but all code must be written by you. Sharing code, copying from the internet or AI
code generators without understanding,will result in a grade of zero for the assignment.

8. Helpful Hints
•​ Use [Link] (Python) or M_PI (C++) for the value of π.
•​ Use [Link], [Link] (Python) or sin(), exp() from <cmath> (C++).
•​ Always test with small n first to verify your formula before running large n.
•​ For Simpson's Rule, double-check the alternating 4, 2, 4, 2, … coefficient pattern.
•​ Printf / format strings make aligning your output table much easier.

You might also like