Trapezoidal Rule Program not working

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
Hello there..

I'm writing a trapezoidal rule program but even though it runs, it outputs the wrong value for finding the area under the curve for e^((-(x)^2)/2) from 1 to 2.

double trapRule(double limit_B,double limit_A,double number_Partitions);

int main()
{
double number_Partitions=0.0;
double limit_A=1.0;
double limit_B=2.0;
double area=0.0;

cout<<"Please enter the number of partitions"<<endl;
cin>>number_Partitions;
area=trapRule(limit_A,limit_B,number_Partitions);
cout<<"The area under the curve for the function is "<<area<<endl;

return 0;


}

double trapRule(double limit_A, double limit_B,double number_Partitions)
{
double delta_X=0.0;
double function=0.0;
double height=0.0;
double x=0.0;
double j=0.0;

delta_X=(limit_B-limit_A)/number_Partitions;
function=exp(pow(-x,2.0)/2.0);
x=(limit_A+delta_X);

for(j=x;j<2.0;j=x+delta_X)
{

height=height+exp((pow(-(j),2.0))/2.0);

}

double height_A=(0.5)*(exp(-1.0/2.0));
double height_B=(0.5)*(exp(-2.0));

double area_Function=delta_X*(height_A+height_B+height);

return area_Function;
}

Any help is much appreciated. I've tried debugging and it seems that my problem is in the for loop but not sure how to fix it.. I've tried different stuff but to no avail.
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
I think part of your problem is right here:

...
height=height+exp((pow(-(j),2.0))/2.0);
..

I think this should be:

height = height + exp(( pow(j,2.0) / -2.0);

To match your original function e^(- (x^2) / 2).
 

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
Originally posted by: QED
I think part of your problem is right here:

...
height=height+exp((pow(-(j),2.0))/2.0);
..

I think this should be:

height = height + exp(( pow(j,2.0) / -2.0);

To match your original function e^(- (x^2) / 2).

hey.. I tried that but to no avail..

I have been trying to fix this and so far, I have got it to present numbers instead of displaying nothing. Here are the numbers i got:

2 Partitions: .684566
3 Partitions: 1.12364
4 Partitions: 0.842733
100 Partitions: 0.993709
1000 Parititions: 1.0037

These numbers dont look right however.. :(
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Also, your looping "for" statement should be:

for(j=x;j<2.0;j=j+delta_X)

NOT

for(j=x;j<2.0;j=j+x);
 

MoMeanMugs

Golden Member
Apr 29, 2001
1,663
2
81
I was ready to tell you Euler's rule, but then I realized j isn't imaginary. Why did you do "function=exp(pow(-x,2.0)/2.0);"? You have x start at 0. e^0 is always 1. Also, am I correct that your percent error is positive?
 

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
Originally posted by: QED
Also, your looping "for" statement should be:

for(j=x;j<2.0;j=j+delta_X)

NOT

for(j=x;j<2.0;j=j+x);

it is currently j=x+delta_X but per your advice, I have changed it to j=j+delta_X.

Why did you do "function=exp(pow(-x,2.0)/2.0);"? You have x start at 0. e^0 is always 1

I set x = (limit_A+delta_X); before the loops start so at the beginning of the loop, the value of x = 1.