I have 13 hours to finish!! HELP PLEASE

Oct 22, 2016 at 6:27pm
closed account (ENhkSL3A)
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..
Last edited on Oct 27, 2016 at 2:58pm
Oct 22, 2016 at 7:02pm
As with any program: break it up in smaller peaces.

I would start with writing a program where the user can input a formula/function and that saves that function in a useful way.

Subsequently you will have to figure out the math part for either 10, 100 or 1000 intervals and check it by manual calculation.

Finally you will have to complete the task.
Oct 23, 2016 at 4:37pm
closed account (ENhkSL3A)
Thanks for the advice Nico! :)
Oct 24, 2016 at 7:44pm
closed account (ENhkSL3A)
Okay, this is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
using namespace std;
double f(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;
    cout << "How many pieces do you want to use? ";
    cin >> n;
 
    h = (b-a)/n;
    area = (f(a)+f(b))/2;
     
 
    for(int i=1;i<n;i++)
		{
		area+= f(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 f(double x) 
	   {
		   return x*x*x*x+3;
	   }
	   


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..
Oct 24, 2016 at 8:34pm
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
Last edited on Oct 24, 2016 at 8:35pm
Oct 25, 2016 at 2:23am
closed account (ENhkSL3A)
This is what my professor said when I asked her to clarify what she wanted:

Hello Katie:


Well, actually I envision 3 nested loops needed in this program.


The outer most loop: from 1 to 3 for the each of the equations to evaluate


The next loop for n = 10, 100, 1000 for the number of intervals to do the summation over


The innermost loop from 1 to n to actually do the summation for each case of n


Hope this helps.


H. R....
Oct 27, 2016 at 4:11pm
closed account (ENhkSL3A)
I really need help guys :(
Oct 27, 2016 at 4:19pm
closed account (ENhkSL3A)
Refrazing my question in a less complicated way.

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
Oct 27, 2016 at 4:52pm
Without knowing anything about the math, I would guess you want to think in this direction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cmath>
using namespace 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;
}
Last edited on Oct 27, 2016 at 6:13pm
Oct 27, 2016 at 10:21pm
closed account (ENhkSL3A)
Aright guys! Here is my new code so far. Much work is needed but this is a lot better!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cmath>
using namespace 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;
			}
		}
	}

Oct 28, 2016 at 12:04am
Hello kns2872,

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.
Last edited on Oct 28, 2016 at 12:09am
Oct 28, 2016 at 12:24am
closed account (ENhkSL3A)
Thanks! I actually meant to change that and completely forgot! :)
Topic archived. No new replies allowed.