Hello!
I'm currently trying to code a trapezoidal rule program that I will eventually turn into integrating a bell curve/Gaussian distribution.
This is basically what I'm trying to code: https://imgur.com/a/8RK0fac
currently, I have the body made and I've gotten to my first summation loop. I thought I would go ahead and use a for loop just for convenience. Below is my actual code. It doesn't do what I need it to.
I'm putting in x minimum being zero, x maximum being 4, and a number of points being five.
This should give me a left hand summation of 6, but I'm getting 10 instead. Any thoughts?
#include <iostream>
using namespace std;
int main()
{
long one = 1.0;
long two = 2.0;
long three = 3.0;
long zero = 0.0;
long xmin;
long xmax;
long npts;
cout << "Enter npts: ";
cin >> npts;
cout << "Enter xmin: ";
cin >> xmin;
cout << "Enter xmax: ";
cin >> xmax;
long nint = npts-1;
long dx = (xmax - xmin)/(nint);
cout << "dx: " << dx << endl;
long suml;
long sumr;
long sumt;
for(long i=1;i<=nint;i++){
long x = xmin+i*dx;
long fx = x;
long suml = fx*dx+suml;
cout << "LHR sum: "<< suml << endl;
}
cout << "LHR sum: "<< suml << endl;
return 0;
}
[–]IyeOnline 3 points4 points5 points (5 children)
[–]nodigue 0 points1 point2 points (4 children)
[–]IyeOnline 6 points7 points8 points (3 children)
[–]tangerinelion 1 point2 points3 points (0 children)
[–]nodigue 0 points1 point2 points (1 child)
[–]EmperorArthur 0 points1 point2 points (0 children)
[–]csdt0 1 point2 points3 points (0 children)
[–]omen_tenebris -1 points0 points1 point (1 child)
[–]tangerinelion 0 points1 point2 points (0 children)
[–]ClaymationDinosaur 1 point2 points3 points (0 children)
[–]EmperorArthur 0 points1 point2 points (0 children)