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.
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.
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.