Help with Array: Evaluating Integral of sin(x)

  • Thread starter Thread starter 999iscool
  • Start date Start date
  • Tags Tags
    Array
AI Thread Summary
The discussion revolves around writing a program to evaluate the integral of sin(x) using the left-hand rectangle rule across ten specified intervals. The program should store the results in an array of type double, with each element representing the integral result for the respective interval. The output should consist of these ten results, with an example showing that result[0] for the interval [0, 1) should be approximately 0.4597. Participants clarify the method of calculating the integral, emphasizing the use of left endpoints rather than right-hand rules. A sample code snippet is provided, demonstrating the integration process with a defined width for subintervals. Suggestions are made to optimize the code, such as simplifying the integrand function and creating a separate function to handle the integration calculations for each interval. The importance of confirming the accuracy of the results is highlighted, as discrepancies in outputs from classmates are noted.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top