Question
CS1325 Homework #5-1 Assigned October 26, due November 2 at 11:59 PM This homework assignment gives you the opportunity to practice C to solve an
CS1325 Homework #5-1 Assigned October 26, due November 2 at 11:59 PM This homework assignment gives you the opportunity to practice C to solve an elementary engineering problem. HW5-1 (Graded out of 100)
1. Introduction For this program, lets explore some of the ways we can use C to perform numerical integration. Well restrict our attention to definite integrals on smooth curves only and look at two methods, called the rectangle rule and the trapezoidal rule respectively (http://en.wikipedia.org/wiki/Numerical_integration).
With the rectangle rule, if we wish to calculate the area under a curve between two end points, a and b, we can evaluate the function at the midpoint between a and b and use that value to create a rectangle whose area will approximate the area under the curve between a and b.
A diagram follows: Here, the area of the red rectangle is consider an approximation for the definite integral between a and b. This area can be expressed by the following equation: f(x)dx = (b - a)f((a+b)/(2))
When this technique is applied to a larger region of the curve, we can divide the whole large region into smaller sub-regions of equal size, and apply the rectangle rule to each one. We may get something like the following:
An approximation for the definite integral between, say, -2 and 2, in this case, will then be the sum of the areas of the rectangles. That sum will produce an approximation for the integral over the entire region. With the trapezoidal rule, we can do something similar. The difference is that trapezoids instead of rectangles are used to create the underlying approximation. Here is a diagram:
The area of the trapezoid (and therefore an approximation of the area under the curve) is given by the following equation: f(x)dx = ((b -a)) * (f(a) + f(b)) / (2)
When applied to a larger region that is divided into many sub-regions, we get something like:
Here the technique is very similar: We divide the total region to be integrated into smaller subregions of equal size, calculate area of the trapezoid for each region, and then sum them all up to produce an approximation for the whole integral.
2. Assignment Lets write a C program that will calculate an approximate value for the definite integral of a curve using each of the two methods, the rectangle rule and the trapezoidal rule. Hopefully well be able to observe how the approximate value improves as we increase the number of subintervals (decrease the size of the sub-intervals).
Let us define these functions:
a) myFunc, which calculates a single value based on the underlying function that is being integrated. It basically returns f(x) for some input value x. As an illustration, we will set myFunc to be a polynomial function f(x) = x3 - 3x 2 + 2. Prototype is double myFunc(double x);
b) calcAreaRect, which calculates the area between two endpoints a1 and b1 using the rectangle method. This function will call myFunc. Prototype is double calcAreaRect(double a1, double b1);
c) calcAreaTrap, which calculates the area between two endpoints a1 and b1 using the trapezoidal method. This function will call myFunc. Prototype is double calcAreaTrap(double a1, double b1);
d) calcIntegral, which returns the approximation value between two end points, a and b, based on either the rectangle or trapezoidal method. calcIntegral will: (1) Divide the region between a and b into num_intervals subregions. (2) Calculate the approximate value for each sub-region according to the function calcAreaRect or calcAreaTrap. (3) Sum them all up to get the final result and return it. Prototype is double calcIntegral(int method, double a, double b, int num_intervals); // method = 1 indicates rectangle method, method = 2 indicates trapezoidal method
3. Outline of programs logic
Ask the user which method he/she would like to use to do the integration, i.e., rectangle or trapezoid, and then to specify the endpoints. Do input validation on the method.
set num_intervals = 2
while (user has not selected to quit && num_intervals <= 128) calculate the integral based on the indicated method and the value num_intervals, and display the result ask if the user wants to quit num_intervals = num_intervals *2 print the exact value of the integral, calculated as g(b) g(a), where g(x) = x4 /4 x 3 + 2x for verification Curve of f(x) = x3 - 3x2 + 2.
4. Additional requirements Make sure you meet all the requirements to avoid losing points 1. Follow the requirements in the Homework Notes.
Can you please follow the directions and add comments, Thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started