Solving the differential equation $ty' + 2y = t^2-t+1$ I wish to solve the following differential equation:
$$ty' + 2y = t^2 - t + 1$$
for $t > 0$ and $y(1) = 2$.
Seeing as I want to use the method of integrating factors, I divide everything by $t$:
$$y' + \frac{2}{t}y = t - 1 + \frac{1}{t}$$
Let $u(t) = \int \frac{2}{t} dt = 2\ln t$
The constanst $C$ is ignored for now, seeing as a constant be included as we finish the task.
We have that $$y(t) = e^{2\ln t}\int e^{-2\ln t}(t - 1 + \frac{1}{t})dt$$
$$y(t) = t^2 \int \frac{1}{t²}(t - 1 + \frac{1}{t})dt$$
$$y(t) = t^2 \int (\frac{1}{t} - \frac{1}{t^2} + \frac{1}{t^3})dt$$
$$y(t) = t^2(\ln t +\frac{1}{t} - \frac{1}{2t^2} + C)$$
Seeing as we have $y(1) = 2$:
$1 - \frac{1}{2} + C= 2 \rightarrow C = \frac{3}{2}$
Thus we have that
$$y(t) = t^2(\ln t + \frac{1}{t} - \frac{1}{2t^2} + \frac{3}{2})$$
I have trouble finding the error in this computation, although AtP says its wrong. 
 A: You've already determined that you used the formula incorrectly (+1 for that, by the way), but let's take a glance at why the formula is the way it is, so you can regenerate it on the fly, rather than having to rely on memorization.
Suppose we know that $$y'+g(t)y=r(t)\tag{$\diamondsuit$}$$ for all $t\in S$ (above, $S$ is the set of positive reals), and that we wish to solve for $y.$ Note that if we have a function $f(t)$ that is differentiable for all $t\in S,$ then the product rule tells us that $$\frac{d}{dt}\bigl[f(t)y\bigr]=f(t)y'+f'(t)y\tag{$\spadesuit$}$$ for all $t\in S.$ If we further assume that $f(t)$ is non-zero for all $t\in S,$ note that $(\diamondsuit)$ holds for all $t\in S$ if and only if $$f(t)y'+f(t)g(t)y=f(t)r(t)\tag{$\heartsuit$}$$ holds for all $t\in S.$ That is, if $f$ is a function that is defined, non-zero, and differentiable at every $t\in S,$ then exactly the same solution $y$ works for both $(\diamondsuit)$ and $(\heartsuit),$ if any such solution exists.
Now, if we happen to know that $f(t)g(t)=f'(t)$ for all $t\in S,$ then we can use $(\spadesuit)$ to make our task simpler. In particular, note that if $f$ is a function that is defined, non-zero, and differentiable at every $t\in S,$ then the following conditions are equivalent: $$f'(t)=f(t)g(t)\\\frac{f'(t)}{f(t)}=g(t)\\\frac{d}{dt}\left[\ln\bigl|f(t)\bigr|\right]=g(t)$$ So, if we happen to know that $g$ is integrable on $S,$ then in order to rewrite $(\heartsuit)$ in the form $$\frac{d}{dt}\bigl[f(t)y\bigr]=f(t)r(t)\tag{$\clubsuit$}$$ using $(\spadesuit)$, then we need to be sure that $$\ln\left|f(t)\right|=K_0+\int g(t)\,dt$$ for some constant $K_0,$ or equivalently, that $$\left|f(t)\right|=e^{K_0+\int g(t)\,dt}\\f(t)=e^{K_0+\int g(t)\,dt}\\f(t)=K_1e^{\int g(t)\,dt}$$ for some positive real $K_1.$ Indeed, any such $f$ will be defined, positive, and differentiable at every $t\in S,$ with $$f'(t)=K_1e^{\int g(t)\,dt}\cdot g(t)=f(t)g(t)$$ by chain rule, so $(\clubsuit)$ holds for all $t\in S,$ as desired. That is, $$\frac{d}{dt}\left[K_1e^{\int g(t)\,dt}y\right]=K_1e^{\int g(t)\,dt}r(t)\\K_1\cdot\frac{d}{dt}\left[e^{\int g(t)\,dt}y\right]=K_1e^{\int g(t)\,dt}r(t).$$ Since $K_1$ is positive, then it makes no difference to the actual outcome (since we can divide it out on both sides of the equation), and so we may as well suppose that $K_1=1$ ($K_0=0$), so that we are taking $$f(t)=e^{\int g(t)\,dt},\tag{$\star$}$$ which is precisely your integrating factor $e^{u(t)}.$ At that point, we proceed to solve: $$y'+g(t)y=r(t)\\e^{\int g(t)\,dt}y'+e^{\int g(t)\,dt}g(t)y=e^{\int g(t)\,dt}r(t)\\\frac{d}{dt}\left[e^{\int g(t)\,dt}y\right]=e^{\int g(t)\,dt}r(t)\\e^{\int g(t)\,dt}y=\int\left[e^{\int g(t)\,dt}r(t)\right]\,dt\\y=e^{-\int g(t)\,dt}\int\left[e^{\int g(t)\,dt}r(t)\right]\,dt$$ That last is exactly the formula $$y=e^{-u(t)}\int\left[e^{u(t)}r(t)\right]\,dt$$ that you had the slip-up with.

Now, let's apply it to your particular case, in which $$f(t)=e^{\int\frac2t\,dt}=e^{2\ln t}=t^2.$$ Then $$y'+\frac2ty=t-1+\frac1t\\t^2y'+2ty=t^3-t^2+t\\\frac{d}{dt}\left[t^2y\right]=t^3-t^2+t\\t^2y=\int\left[t^3-t^2+t\right]\,dt\\t^2y=\frac14t^4-\frac13t^3+\frac12t^2+C\\y=\frac14t^2-\frac13t+\frac12+\frac{C}{t^2},$$ so since $$2=y(1)=\frac14-\frac13+\frac12+C=\frac5{12}+C,$$ then $C=\frac{19}{12},$ and so $$y=\frac14t^2-\frac13t+\frac12+\frac{19}{12t^2},$$ which can be verified to be the solution to the initial value problem. It's a bit more work to actually multiply everything by the integrating factor and go from there, but it is a more reliable route than trying to memorize a formula, in my opinion.
A: I have naturally used the method wrong. The formula is 
$$e^{-u(t)}\int e^{u(t)}(r(t))dt$$
Where $r(t)$ is the righthand side. Sorry for the blunder.
A: First you should find a solution $y_h$ for the homogeneous equation:
$$ty'+2y=0$$
we have
$$\frac {y'}y=-\frac 2 t\iff \ln y=-2\ln t+C$$
hence
$$y_h=\frac{\lambda}{t^2},\quad \lambda\in\mathbb R$$
then we look for a particular solution $y_p$ on the form $at^2+bt+c$ and the general solution is
$$y=y_h+y_p$$
finaly we determine $\lambda$ with the equality $y(1)=2$.
