0

I'm dummy in mathematic, but in the Universite I got an exercise to write in C++ graphic of this mathematical expression: $$ \sum_{k=0}^{\infty} (-1)^{k+1}\frac{x^{2k}}{k!} $$ where $x\in (-10,10)$.

So, I dont know how to calculate it. But of course, best way is to reduce it, because I need to count it with accuracy 0.001 and it must be equall with $-e^{-2x}$

okonik
  • 3

2 Answers2

0

$$e^x=\sum_{n=0}^\infty\frac{x^n}{n!}$$ By replacing $x$ with $(-x)^2$ we get : $$e^{-x^2}\ =\ \sum_{n=0}^\infty\frac{(-x^2)^n}{n!}\ =\ \sum_{n=0}^\infty\frac{(-1\cdot x^2)^n}{n!}\ =\ \sum_{n=0}^\infty\frac{(-1^n)(x^2)^n}{n!}\ =\ \sum_{n=0}^\infty(-1)^n\frac{x^{2n}}{n!}$$ Ultimately : $$-e^{-x^2}\ =\ (-1)\sum_{n=0}^\infty(-1)^n\frac{x^{2n}}{n!}\ =\ \sum_{n=0}^\infty(-1)^{n+1}\frac{x^{2n}}{n!}$$ The area inbetween the horizontal Ox axis and the graphic of the function $e^{-x^2}$ is $\sqrt\pi$ (see link), whose value is about 1.77245385.

Lucian
  • 48,334
  • 2
  • 83
  • 154
  • The worst thing about the place, what bothered me the most in my series - is a factorial. If it possible to get rid of it? – okonik Oct 21 '13 at 20:06
  • Why get rid of it ? And why should it bother you ? You simply initiate a sum S at $0$, and a term T at $1$. Then, with each new iteration, the term is multiplied with $\frac xn$ $($where x is given, and n is the iterator$)$ and it is then added to the sum. The process repeats itself until the desired precision is reached. – Lucian Oct 21 '13 at 20:40
0

These series does not simplify in the way you would think. If you want to write a graphic for $$\sum_{k=0}^{\infty} (-1)^{k+1}\frac{x^{2k}}{k!} = - e^{-x^2},$$ with error less than $0.001$, then you are going to need to take partial sums, that is fix some $n$, and take the partial sum $\displaystyle\sum_{k=0}^n$. You will probably do this with a looping mechanism. You will then need to decide how many terms you need to get that precision. If you were stumped on how to actually compute the sum, you just increment $k$ starting at $k=0$, add the term with $k=1$, then $k=2$ and so forth. Say you want like 4 terms. Then that is

\begin{align*} \sum_{k=0}^{3} (-1)^{k+1}\frac{x^{2k}}{k!} &= -\frac{1}{0!}+\frac{x^2}{1!}-\frac{x^4}{2!}+\frac{x^6}{3!} \\ &=-1+x^2-\frac{x^4}{2}+\frac{x^6}{6} \\ \end{align*} If you want a better approximation, take more terms.

I expect you will write some sort of For loop over $k$ to build a big partial sum, each time incrementing some variable y+= (the sum term you need to create evaluated at some fixed $x$ value - your real job ;) ), and that loop could run inside another for loop that goes over a list of $x$ values. That's a nested for loop, outer over $x$, inner over $k$, and you save the outputs for each $x$ in a table for graphing. As for computing error, this might help, If no one really cares about a rigorous error analysis, then just compute and test by trial and error how many terms works compared to the actual function that the sum represents.

I just wrote the equivalent in Mathematica, and 350 terms "looks" good, but "looks" might not be the standard called for in the assignment. It gets worse around the edges of your boundary. I would write the program, then choose the size of the series to take that is acceptable with the error analysis after the fact.

J. W. Perry
  • 5,447
  • 3
  • 22
  • 28
  • I don't think $($s$)$he's referring to calculating the value of $e^{-x^2}$, but the $($area of$)$ the graphic, which is $\sqrt\pi$ – Lucian Oct 21 '13 at 20:14
  • The problem seems to be related to computer programming rather than pure maths. Whether plotting the actual graph, or applying numeric integration, in a self-made C++ program. Perhaps this question should've been asked on a programming board rather than here ? – Lucian Oct 21 '13 at 20:24
  • @Lucian at this point this is entirely possible. I originally thought that the main OP problem here was just how to evaluate a partial sum. Also realizing that if he has to estimate with an infinite sum he has to just a finite number of terms. We shall see. It is definitely a part of a programming exercise at minimum. – J. W. Perry Oct 21 '13 at 20:26
  • Where I said nothing about area? I said that x belongs from -10 to 10. Ok. Sorry I didn't said about details. Sum is continue while is accuracy 0.001. x starts at -10 at continue to 10 with step 0.25 – okonik Oct 21 '13 at 20:29
  • Good. That is what I thought. What I said is what you want to do. Use a for loop, build up a bunch of sums, then compare that partial pile of sums to the function that the infinite sum converges to. If you use enough sums then it will evaluate to inside your error. – J. W. Perry Oct 21 '13 at 20:37
  • So we need to draw a point with x=-9.75, next -9.50 . Program must draw a graphic by points basing on first half of equation and draw with lines basing on second half of equation. (Can't edit my comment at top) – okonik Oct 21 '13 at 20:46
  • Yeah that sounds good. Just get the values for the big partial sum and store them I'm a table for each $x$ value. You might also compare the various values with the actual $-e^{-x^2}$ at each point and see if the difference is less than 0.001. Increase your loop size as appropriate. Note you can edit comments for a short time, after that you can only delete. – J. W. Perry Oct 21 '13 at 20:55