The Expansion Coefficients for a Particle in a Box This is an expansion of two previous questions I had before (I am very confused by it). I am now encountering a problem with an integral. We want a general expression for the probability of finding E. We have the expression for the wave function for the particle in a box:
$$\psi(x)=x\sin(\frac{\pi x}{a})$$
We evaluate $c_n$ to find $|c_n|^2$, the probability our measured E will be at level n. To calculate $c_n$, I did 
$$c_n = \int_0^a \sin(\pi\, n\frac{x}{a})x\sin(\pi\frac{x}{a})dx$$
All times some normalization constant which is not important for my question. We evaluate this integral using mathematica, and find that it equals
$$\frac{a^2(-2n(1+\cos(n\pi))-(-1+n^2)\pi\sin(n\pi))}{(-1+n^2)^2\pi^2}$$
Obviously, we have a problem for $n=1$: we get zero over zero. Is there something we did wrong with this integral? The only way I can get an answer that isn't in-determinant is if I take n to be 1 before integration. Do we have a piecewise function for our general expression? I am just very confused. Any help would be appreciated.
 A: Let's imagine another problem. Let's suppose you were asked to find the positive roots of $nx^2+5x-7=0$ for $n=0,1,2,3,...$. Let's say you decide to plug this into mathematica and it gives you the quadratic formula out. For $n>0$ this works great and you say good. Then you try $n=0$ and you get $0/0$. Of course if you set $n=0$ before giving the problem to mathematica, you get the right answer. So what happened? 
Well it's obvious. The answer mathematica gives you isn't correct for $n=0$; if your teacher asked you to solve $5x-7=0$ and you said $x=0/0$ you would not get full credit. Mathematica is apparently designed not to care about special cases for parameters in some situations. 
This is ok because you are apparently smart enough to notice that there is a special case and deal with this case yourself (which you did by putting $n=1$ in before the integration in your case). Remember you should treat mathematica as a tool for making tedious computations easier but you should try not to have mathematica do any actual thinking for you. It's always best to do the thinking for yourself so you know what is going on. You don't want to be as reliant on mathematica as these guys are on their teleprompter.
A: Hints: It is easy to do the integral yourself without the aid of Mathematica. 


*

*For instance use the product-to-sum-formula for the two sines. 

*Then integrate by part to get rid of the $x$ power. 

*You will need the following primitive integrals (aka. antiderivatives or indefinite integrals):
$$ \int \!dx ~\cos(bx)~=~ \left\{\begin{array}{ccc} \frac{\sin(bx)}{b} &\text{for}& b\neq 0, \\ x&\text{for}& b= 0,\end{array} \right. $$
and
$$ \left\{\begin{array}{ccc}\int \!dx ~\frac{\sin(bx)}{b} &=&  \frac{1-\cos(bx)}{b^2} &\text{for}& b\neq 0, \\ \int \!dx~x  &=& \frac{x^2}{2}&\text{for}& b= 0.\end{array} \right. $$
Here the various integration constants have been chosen such that the $b=0$ case can be viewed as the limit $b\to 0$ of the $b\neq0$ case.

*Then the above comment of Jerry Schirmer clearly applies: You can either recover the limit $b\to 0$ from the $b\neq0$ case, or  treat the $b=0$ case separately. 
A: The integral
$$c_n = \int_0^a \sin(\pi\, n\frac{x}{a})x\sin(\pi\frac{x}{a})dx$$
is actually a shorthand for a countably-infinite number of equations, starting with $c_1 = \int_0^a \sin(\pi\, \frac{x}{a})x\sin(\pi\frac{x}{a})dx$. Your first inclination to keep the parameter $n$ in there, and solve for the general form of $c_n$ is good, as this gives you the general solution with just one integration. Noticing that naively inserting $n=1$ into the general antiderivative gives an indeterminate form is also good; that illustrates how such an approach might fail. 
There are a few ways of getting around this. One way would be to temporarily allow $n$ to be a continuous parameter, and to take the limit $n\rightarrow 1$ in the result Mathematica gave you. That will require you to use L'Hopital's rule or some other technique for solving such indeterminate forms. That will probably be time-consuming, but as BMS reports in a comment, it will give you the same answer as the other approach. 
The second approach is to recall that you are always allowed to go back to the individual equations themselves, and solve them individually. In this case, you only need to do that for the $n=1$ case. Here, you set $n=1$ before evaluating the integral. Then there is no problem with the result you get. Mathematica should usually work, but do be aware that it is easy to mis-type your integrand, and that sometimes Mathematca will make assumptions about your parameters that will violate the physics of your problem. As Qmechanic notes, integration by parts is a valuable skill to have if you don't already. You can simplify the trigonometric functions as he suggests, or you can replace the trig functions with exponential if you are familiar with Euler's formula. Either way, you will still need to do an integration by parts.
A: We can get a bit of insight by series expanding the integral in $n$ around $n=1$. Since you are asking about Mathematica here's some Mathematica code to do it for us:
f = 1/a^2 Integrate[Sin[n \[Pi] x/a] x Sin[\[Pi] x/a], {x, 0, a}];
coeff = SeriesCoefficient[Numerator[f], {n, 1, m}] // FullSimplify

Here we are expanding the numerator of your answer in powers of $(n-1)^m$. coeff is the coefficient of the terms in the expansion:
$$ \begin{cases}
0 & m<2\\
\pi^{m-1}\left(-\frac{1}{2}(m-3)m((\pi m)\sin)-\frac{1}{2}2\pi(m-1)((\pi m)\cos)\right) & m\geq2
\end{cases} $$
The important point is that the series begins at $m=2$, i.e. there are no terms lower than $(n-1)^2$. We can check this:
In:= Numerator[f]==(n-1)^2 Sum[coeff (n-1)^(m-2),{m,2,\[Infinity]}]//Simplify
Out= True

The numerator is therefore proportional to $(n-1)^2$ where the remaining factor is a nice well behaved function (in fact it's an entire function):
Plot[#, {n, 0, 10}] &@Sum[coeff (n - 1)^(m - 2), {m, 2, \[Infinity]}]


So the zeros of the numerator and denominator cancel leaving a finite expression. The only other place where there could be a problem is $n=-1$ but a similar cancellation happens there as well. The final answer is well defined everywhere in the complex plane:
Grid[{{f, SpanFromLeft}, {"Re", "Im"}, {
  Plot3D[Re[f] /. n -> x + I y, {x, -2, 2}, {y, -2, 2}, ImageSize -> Medium],
  Plot3D[Im[f] /. n -> x + I y, {x, -2, 2}, {y, -2, 2}, ImageSize -> Medium]
}}]


It's the same situation as the function $(\sin{z})/z$. It looks singular at $z=0$ but in fact there is a secret $z$ "hiding" in $\sin{z}$ which makes everything okay. $(\sin{z})/z$ is well defined everywhere in the complex plane.
That much might have been obvious to you. But why do you get the same answer this way as if you put $n=1$ in the integral before hand? It's because the integral "knows" about the analytic structure of the function you are trying to get. You can do the series expansion of the integrand about $n=1$ and integrate term by term. I'll let you do this exercise - it's no problem in Mathematica. Because the function is analytic everywhere and the series converges absolutely it doesn't matter what order you do the series expansion and integration in. You'll always get the same answer... unless you butcher it by telling Mathematica to //FullSimplify[#, Assumptions :> n [Element] Integers]& the integral. Then Mathematica simplifies it in a way that doesn't preserve analyticity and - you'll see if you do it - you can't get the $n=1$ case.
