Help with Array: Evaluating Integral of sin(x)

  • Thread starter 999iscool
  • Start date
  • Tags
    Array
In summary: My other classmates are giving me weird numbers.Maybe you're getting different results because your classmates are using different algorithms?
  • #1
999iscool
12
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
  • #2
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:
  • #3
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:
  • #5
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
 
  • #6
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.
 

Related to Help with Array: Evaluating Integral of sin(x)

1. What is an array in the context of evaluating the integral of sin(x)?

An array is a data structure that stores a collection of values in a linear fashion. In the context of evaluating the integral of sin(x), an array is used to store the values of the function at different points, making it easier to calculate the integral.

2. How do you use an array to evaluate the integral of sin(x)?

To use an array for evaluating the integral of sin(x), you first need to define the spacing or interval between the points at which the function will be evaluated. Then, using a loop, you can calculate the value of sin(x) at each point and store it in the array. Finally, you can use the array to calculate the integral using numerical integration techniques.

3. What are the advantages of using an array for evaluating the integral of sin(x)?

Using an array allows for faster and more accurate calculations of the integral of sin(x) compared to manually inputting values. It also allows for a more efficient use of memory, as the values are stored in a single data structure rather than individually.

4. Are there any limitations to using an array for evaluating the integral of sin(x)?

One limitation of using an array is that it requires a predetermined spacing between the points at which the function is evaluated. This can result in less accurate calculations if the spacing is not chosen carefully. Additionally, the larger the number of points used in the array, the longer the calculation time may be.

5. Can an array be used to evaluate the integral of other trigonometric functions?

Yes, an array can be used to evaluate the integral of other trigonometric functions, such as cosine or tangent. The process is similar to using an array for the integral of sin(x), except the values of the respective functions are calculated and stored in the array. This allows for efficient and accurate calculations of a variety of integrals involving trigonometric functions.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
922
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
1
Views
715
Back
Top