Help with Array: Evaluating Integral of sin(x)

  • Thread starter Thread starter 999iscool
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around evaluating the integral of sin(x) using the left-hand rectangle rule with 2000 subintervals across specified intervals. Participants are seeking clarification on the expected output format and the correctness of their calculations, particularly focusing on the interval [0, 1).

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant outlines the task of writing a program to evaluate the integral of sin(x) and asks for help with the output format.
  • Another participant suggests that the output should consist of 10 elements in an array, each representing the result of integrating sin(x) over the specified intervals.
  • A participant questions the method used to obtain a specific result, indicating confusion between the left-hand and right-hand Riemann sums.
  • There are multiple mentions of the expected result for the interval [0, 1), with one participant correcting their earlier claim about the value to be closer to 0.4597.
  • One participant expresses a need for confirmation of the integral result for [0, 1) and mentions discrepancies in results provided by classmates.
  • Suggestions are made to improve the code, including simplifying the integrand function and proposing a separate function to handle the integration for each interval.

Areas of Agreement / Disagreement

Participants express uncertainty regarding the correct application of the left-hand rectangle rule versus the right-hand rule, and there is no consensus on the exact output values for the integrals across the intervals. Discrepancies in results from different participants indicate ongoing debate.

Contextual Notes

There are unresolved questions about the correctness of the integration method used and the specific values expected for each interval. The discussion reflects varying interpretations of the Riemann sum approach.

999iscool
Messages
12
Reaction score
0
You will write a program that evaluates the integral of sin(x) using the left-hand rectangle rule with
2000 subintervals, over 10 intervals. The intervals to test are [0, 1), [1, 2), …, [8, 9), [9, 10). You will
declare an array of type double that can hold 10 elements, and you will use this array to hold all 10
results you get from evaluating each interval.

What I need help with is the output.

What should be our output?
For example, [0, 1) ?
 
Technology news on Phys.org
The output should be the 10 elements in the array. Each array element is the number you get from integrating sin(x) on one of the intervals.

For example, result[0] should have a number that is close to -.5403.
Edit: that should be .4597.
 
Last edited:
Mark44 said:
The output should be the 10 elements in the array. Each array element is the number you get from integrating sin(x) on one of the intervals.

For example, result[0] should have a number that is close to -.5403.

How did you get that number? I thought plain riemann sum (in this case, the right hand rule)

Here is the short version (with no array), tested [0,1)
Code:
#include <iostream>
#include <cmath>
   using namespace std;
   
double integrand(double);

int main()
{
	const int SIZE = 10;
	const double WIDTH = 0.0005;  // (b-a)/n
	
	double sum_array[SIZE];
	double a = 0.0;
	double b = 1.0;
	double sum = 0.00;
	double y1 = 0;

	for (double k = a; k < b; k += WIDTH)
	{
		y1 = integrand(k);
		sum += y1;
	}
	cout << (sum*WIDTH) << endl;
	return 0;	
}

double integrand(double x)
{
	double y;
	y = sin(x);
	return y;
}
edited the code again (added (sum*WIDTH). I forgot about this)

I got 0.45968 for [0, 1) using left endpoint
 
Last edited:
Forgot to add 1. Make that .4597.
 
Mark44 said:
Forgot to add 1. Make that .4597.

LOL
OKay, so it is 1-cos(1) for [0, 1)

Just really need to confirm that. Otherwise, even if I have perfect codes, with no REAL solution i still can't test it.
My other classmates are giving me weird numbers.

Thanks
 
999iscool said:
How did you get that number? I thought plain riemann sum (in this case, the right hand rule)
Your first post says to use the left endpoints.
999iscool said:
Here is the short version (with no array), tested [0,1)
Code:
#include <iostream>
#include <cmath>
   using namespace std;
   
double integrand(double);

int main()
{
	const int SIZE = 10;
	const double WIDTH = 0.0005;  // (b-a)/n
	
	double sum_array[SIZE];
	double a = 0.0;
	double b = 1.0;
	double sum = 0.00;
	double y1 = 0;

	for (double k = a; k < b; k += WIDTH)
	{
		y1 = integrand(k);
		sum += y1;
	}
	cout << (sum*WIDTH) << endl;
	return 0;	
}

double integrand(double x)
{
	double y;
	y = sin(x);
	return y;
}


edited the code again (added (sum*WIDTH). I forgot about this)

I got 0.45968 for [0, 1) using left endpoint
Yeah, that's what you should get.

Some suggestions. Your integrand function can be shortened.
Code:
double integrand(double x)
{
    return sin(x);
}

Also, you could write a function that does essentially what you do in main. It should take three parameters: left endpoint, right endpoint, number of subintervals, and should return the computed value for that interval. The 10-element array would still be in main -- just loop through each element in the array and call your calculating function for each of the intervals 0 - 1, 1 - 2, ... 9 -10.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
Replies
235
Views
15K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
20
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
11
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K