The objective of this Program is to use for loops and nested loop structures to design the C++ code that will perform numeric integration to estimate the area under a curve.
Background information:
In Calculus a definite integral can be used to find the area between a curve defined by a function π¦ = π(π₯) and the x-axis. In the readings for Unit 7 there is a .pdf document Unit 7 - Areas Stewart Calculus 8e with a complete development of this concept printed from the James Stewart Calculus book used in the Calculus courses at NRCC. Please review this document.
A concise explanation for our purposes follows:
The definition of the integral uses limits and Riemann sums:
Let y ο½ f (x) be a continuous polynomial function defined over an interval a ο£ x ο£ b.
Divide the interval [a,b] into n subintervals of equal width οx ο½ b ο a .
n
The endpoints of the subintervals are x0 , x1 , x2 ,..., xn where x0 ο½ a, xn ο½ b .
Choose sample points x * ο½ ( x ο« x ) / 2 in each subinterval.
These are midpoints of the subintervals.
The subintervals divide the region between the curve and the x-axis into n rectangles. The area of each rectangle is f (x * ) οx , (area = height Γ width)
Add Up all the Areas to get Riemann Sum
Note the indexing is from 1 to n, where n is the number of subintervals used.
The true area is the definite integral.
We donβt have the means to evaluate a limit directly within a C++ program, but we can get an estimate of the limit value for very large values of n.
Program 6 Description:
Write a program to calculate the Riemann Sum as an estimate of an area under a curve.
You will run the program for 3 different functions, the functions to be supplied to the program as input.
For each function, you will use 3 different subinterval sizes to find 3 different estimates of area.
The 3 different subinterval sizes use n = 10, 100, 1000.
This is coded in the program, they are not input values.
Your program should print the equation of the polynomial function used, the interval used, and the calculated areas for each of the three different subinterval sizes.
Use double for all real variable declarations.
Give your answers to 4 decimal places.
Your program should request the user to input the function to be used.
For now you may assume a polynomial in x, with no degree higher than 4, and the user will input the coefficients for each term.
For example: π¦ = ππ₯3 + ππ₯2 + ππ₯1 + π so the input values for the function to be used would be π3, π2, π1, π0 .
You should use at least 3 nested for loops; an outer loop for the 3 different functions, a next inner loop for each of the successive values of n = 10, 100, 1000, and then a very most inner loop to calculate an area estimate using the Riemann Sum formulation.
The pseudo-code for this program might be as follows:
Set up an outer loop to process each of the functions.
Input the coefficients for a polynomial function y ο½ f (x)
Input values to define the interval a ο£ x ο£ b .
Set up a nested loop for n = 10, 100, 1000 (The code is fixed to the 3 . cases, these are not inputs)
For each value of n, the region will be subdivided into n . rectangles.
Find the value for deltax: (b-a)/n (deltax = οx from the development . above = the width of each interval).
Set up a nested loop to calculate an area for each rectangle, foreach n.
Find a sample point π₯π in the middle of the οx interval
Use the sample point to obtain the height of the rectangle as the . absolute value of the function value at the
. sample point.
Find the area of that rectangle as the product π΄π = |π(π₯π )| Γ βπ₯ .
Add up all the rectangular areas (there are n of them) to get an estimate for the true area.
Print out all pertinent info, and process the next function.
Test your program with the following functions, read in from screen or file.
y=ο½x for 0<=x<=4 The true area is 8
y=-x^2+3x+4 for -1<=x<=4 The true area is 20(5/6)
y=0.5x^3-3x^2+6x-2 for -2<=x<=2 The true area is 28.7622
Guys... I suck at calculus... I have no idea where to begin with this. It is like a bunch of a whole different language to me. Some helpful suggestions? I'm honestly freaking out..
The instructions say that the three subintervals should be in the code. Where should I put that? Also, what exactly do I put? I think I'm doing this right so far but honestly I have like zero knowledge about this Riemann Sum. It is like bumbojumbo to me... I've not taken calculus yet.. :( So... yeah, bare with me..
If your formula is defined for the range from 0 to 10 (for whatever reason),
- I would expect the 10-intervals to be for example 0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 and 9.9.
- I would expect the 100-intervals to be for example 0, 0.11, 0.22, 0.33 and so on until 9.99.
- I would expect the 1000-intervals to be for example 0, 0.011, 0.022 and so on to 9.999.
This would provide you with results of increasing accuracy.
But I am guessing, so ask yourself: Does that make sense in your opinion?
Now suppose you put your calculations in a function and you provide it at least 3 arguments for example: minimum, stepsize, maximum or startValue, stepsize, numberOfSteps.
Then your three subintervals are probably no more than a couple of loops and a simple (maxValue-minValue)/numberOfIntervalSteps
c++ program finding the estimated area under a curve defined by a function y = f(x)
found over the interval a <= x <= b
using the subinterval sizes n= 10, 100, 1000 <<--- this is coded
#include <iostream>
#include <cmath>
usingnamespace std;
double formula(double x);
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "October 23, 2016" << endl;
cout << "Program 6" << endl;
cout << "Nested loops, for loops, and Riemann Sum" << endl;
double a;
double b;
int n;
double h;
double area;
double areab;
cout << "What is the left x value? ";
cin >> a;
cout << "What is the right x value? ";
cin >> b;
/// start changes
// cout << "How many pieces do you want to use? ";
// cin >> n;
double step1000 = (b-a)/1000.0;
double area10 = 0.0; // container for the sum
double area100 = 0.0; // container for the sum
double area1000 = 0.0; // container for the sum
double yValueForTheCurrentX;
for (int i=0;i<10;i++)
{
for (int j=0; j<10; j++)
{
for (int k=1; k<=10; k++)
{
// do the math for x value ((i*100.0 + j*10.0 + k*1.0)*step1000 );
area1000 = area1000 + yValueForTheCurrentX; // sum 1000 values, you should correct this to account for the X-interval too.
}
area100 = area100 + yValueForTheCurrentX; // sum only 100 values, you should correct this to account for the X-interval too.
}
area10 = area10 + yValueForTheCurrentX; // sum only 10 values, you should correct this to account for the X-interval too.
}
// do something with the values that you just calculated
cout << "The area based on 10 samples is " << area10 << ", based on 100 samples it is " << area100 << ", based on 1000 samples it is " << area1000 << "\n";
return 0;
}
void unusedOldCode()
{
double a;
double b;
int n;
double h;
double area;
double areab;
/// end changes
h = (b-a)/n;
area = (formula(a)+formula(b))/2;
for(int i=1;i<n;i++)
{
area+= formula(a+i*h);
}
areab = area*h;
cout << "The area under the curve of f(x) = x^4+3 is ";
cout.precision(4);
cout << areab << endl;
cout << a << "<= x <=" << b << endl;
}
double formula(double x)
{
return x; // *x*x*x+3;
}
#include <iostream>
#include <cmath>
usingnamespace std;
double f(double x, a, b, c, d);
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "October 23, 2016" << endl;
cout << "Program 6" << endl;
cout << "Nested loops, for loops, and Riemann Sum" << endl;
//defining variables
double a, b, c;
int start, end;
int areaA, areaB, areaC;
int intsizeA, intsizeB, intsizeC;
cout << "Enter the left 'x' value: ";
cin >> start; //Getting a. Changed variable name to start.
cout << "Enter the right 'x' value: ";
cin >> end; // Getting b now. Changed variable name to end.
intsizeA = (end - start) / 10; //Telling the program to subtract the numbers given and divide by 10 to get the sub intervals.
intsizeB = (end - start) / 100;
intsizeC = (end - start) / 1000;
for(int i=0; i < 10; i++)
{
areaA += f(start + i * 10) * intsizeA;
cout << "The continuous plynomial function: " << a << "*x^3 + " << b << "*x^2 + " << c << "*x + " << d << endl;
cout << "The polynomial function is defined over the interval: "<< start << end << endl;
cout << "The subinterval is 10." << endl; //Extra info
cout << "The area under the curve of f(x) is: " << areaA << endl;
for(int i=0; i < 100; i++)
{
areaB += f(start + i * 100) * intsizeB;
cout << "The continuous plynomial function: " << a << "*x^3 + " << b << "*x^2 + " << c << "*x + " << d << endl;
cout << "The subinterval is 100." << endl; //Extra info I felt should be included with the other things you asked for!
cout << "The polynomial function is defined over the interval: "<< start << end << endl;
cout << "The area under the curve of f(x) is: " << areaB << endl;
for(int i=0; i < 1000; i++)
{
areaC += f(start + i * 1000) * intsizeC;
cout << "The continuous plynomial function: " << a << "*x^3 + " << b << "*x^2 + " << c << "*x + " << d << endl;
cout << "The polynomial function is defined over the interval: "<< start << end << endl;
cout << "The subinterval is 1000." << endl; //Extra info.
cout << "The area under the curve of f(x) is: " << areaC << endl;
}
}
}
In your new code you have the proto type for function x, but not the definition.
based on what was there to work with look at lines 25 -27 you are doing integer math which should result in a decimal value and then storing that in an int variable. This drops the decimal portion that you need later. On line 17 change the int to double and it will work better.
Any chance you have some test values for 'a' and 'b' with a known result? If not it will work out.
Andy
P.S. In your proto type double f(double x, a, b, c, d); 'a', 'b', 'c' and 'd' need to be typed as double. Each parameter need its own type.