Loop'n Exponential Series Aid

I am trying to evaluate an exponential series over a specified range for x: from -2 to 2, with a total of 40 points. (i.e., -2.0, -1.9,.....+2.0)
These will points for x to be evaluated.
So the series is the form of: summation[((-3^n)*x^(2n+4))/n!]
What I have so far is:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double n;
double x;
double numr1;
double numr2;
double factorial;
double sumx;
double func;
int i;

for (i = 1; i <= 41 ; i++)
{
//here is where i need to get x to go from -2 to 2 in increments of 0.1

for (n = 0; n <= 24; n++)
{
factorial = 1;
factorial = factorial * n;
numr1 = pow (-3.0,n);
numr2 = pow (x, 2*n+4);
func = (numr1 * numr2)/factorial;
sumx = 1;
sumx += func;
cout << "The sum is: " << sumx << endl;
}
}


system ("pause");
return 0;
}
And the question is? What's the problem you're encountering?
okay, idk what the problem you're having is, but for starters, i++ will increment i by 1, not x.

You need to change the i for loop and put (x = -2.0,x==2.0,x=x+0.1) in it's place.

If you really want to use i for some reason, then you need to put x= -2.0; somewhere before the two loops, and then inside the loop you need to put x=x+0.1; after the n for loop but before the i for loops end brace.


Like this:

1
2
3
4
5
6
7
8
9
10
for (i is the incorrect way to do this)
{

  for(n seems right but idk what the problems you're having are)
  {
   *Let's throw some stuff in here*
  }

x=x+0.1
}


That will increment x, although I'm not sure if it will fix your whole program.
I can't really tell without some explanation of what the problem you're encountering is.



1
2
factorial = 1;
factorial = factorial * n;

here you first asign 1 to factorial and then mulyiply it by n
1*n=n so your factorial always equals n

if you want to get the factorial of n try
1
2
if(n==0)factorial=1;
else factorial=factorial*n
Last edited on
Topic archived. No new replies allowed.