I'm going to be up front, the following question is in fact a homework question. Here's the deal, though, the mathematics alone in this question are over my head and I just know that I stand zero chance of being able to figure this thing out on my own in the short amount of time I have.
Bottom line, I'll make a nice donation to this website if someone offers me a working solution.
For those still reading, the question is:
Write a program, that uses the following algorithms, to compute, and later output to the screen, the value of the exponential function f(x) = ex. To do this, you will use the power series representation of ex, where ex is the sum, from n = 0 to ∞, of xn / n! and n! = n • (n - 1) • (n - 2) • … • 2 • 1. The value x, is entered, via the keyboard, by the user.
Of course, any more traditional nudge in the right direction help is also appreciated. Although, I'm not convinced that will be enough.
I'm following this, as I have been looking for an answer to the exact same question for the last half an hour. I got someone to vaguely explain the math function to me, but even then, I don't know how to create a loop to output the answer without it becoming an endless loop! Because n = zero to INFINITY.... if you figure out the answer please let me know! I will be eternally grateful!!
I appreciate your reply, fun2code, but as suspected, a nudge is proving thoroughly insufficient in this case (my case--hope it helped you, krisnorine). I still have no idea what to do.
I'm going to substitute a sufficiently high value for "infinity" in order for the loop to effectively run, and see what happens...
Essentially we are trying to find: e^x, which is the sum series of x^n / n! where n = 0 to "inifinity".
An example of n! where n=5 is:
5*4*3*2*1 = 120
We did a sum series in the previous question of our assignment (I do believe we are working on the same assignment right now!) so I am going to try and use that same logic with this new equation, somehow.
To be honest, I don't entirely understand it either, and I don't imagine that my explanation was very clear.
Sorry I can't be much more help to you - perhaps we need to arrange a study group for the next assignment!!
double fi(int n, double x)
{
int i;
double r=1;
if (n==0)
return 1;
else {
for (i=0;i<n;i++)
r = r * x / (i+1);
return r;
}
}
double f(double x)
{
double y = 0;
double u;
int i = 0;
do {
u = fi(i,x);
if (u==0)
break;
else {
y += u;
i++;
}
} while (1);
return y;
}
So ex is 1 + x + x2/2! + x3/3! + .... + xn/n! + ....
While computing each term in this series in a loop ( n = 1, 2, 3, ... ) and summing them up,
for some value of n, the term xn/n! would become too small to be representable, would be equal to zero.
We can stop at that point and print out the sum so far as the result.
Calculatiing the next term is easy: note that termn == termn-1 * x / n
For instance: x5/5! == x4/4! * x / 5
I tried doing this just now and it took me forever to figure out why I was getting the wrong answer. Do not have your factorial function return an int. Have it return a long or a long long instead. Same thing if you write your own power function.
I just read JLBorges' post in its entirety and figured out that there was no need to use a power function or factorial function.
Calculatiing the next term is easy: note that termn == termn-1 * x / n
For instance: x5/5! == x4/4! * x / 5
That and the fact that I was coding in cpp.sh. It crashed when it entered an infinite loop during compilation for some reason. No idea why, it worked earlier and I didn't change the code, just wanted to run it again so that I could input a different number for X. So I'm too lazy to rewrite the code.
And JLBorges just gave you a much superior answer below.
We haven't covered arrays yet, so I shouldn't use them. However, I have one more class before the assignment is due, I think we may be starting on them then.
This code doesn't allow the user to enter the numbers in the array via the keyboard anyways (if that's even possible).
Where do these numbers: -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0, even come from? Are they just randomly chosen numbers for the purpose of your example or..?
I chose a scientific computing with java module in my second year and omg my grade sucked. The time I spent searching for any similar code online would have been better spent grinding it out. My grade may not have sucked so bad if I had followed this logic. Wait until you see the solution code lol, a few lines.
I also chose a masters level module c++ for finance in my third year (crazy ass I know, but programming is a skill I think is essential these days, to have a clue is useful not just for CV) and didn't do so bad so far. Just the anxiety of the result made me doubt whether I was near correct which wastes valuable time.
Some people on here are very helpful but like the sticky says they will likely not do homework for you. If your lecturer is giving you Taylor Series things then you must have covered it at some point, anyway it's not that hard once you know the algorithm, more memory and reproducing at first.
You usually get marks for clarity of code, developer documentation, comments, accuracy of output, etc etc. If they can see what you were trying they will not penalise, if you cannot understand your code then you risk 0.
So grab your books, learn what you don't get study that, ask questions, grind it out. Use the F10 to step through code to see what is happening.