How to solve $ xf''(x)-xf'(x)+f(x)=2x-x^2 $? Book Differential Equations and Boundary Value Problems (by Charles Henry Edwards and David E Penney) describes that, generally, differential equations with variable coefficients require numerical methods to be solved, obviously there are such equations that have an analytical solution. It is one of them that I want to talk about in my question, here it is:
$$ xf''(x)-xf'(x)+f(x)=2x-x^2 $$
I know that it has an analytical solution because it is in a calculus worksheets, so it shouldn't be too complicated to solve too, but nothing I've tried works (I've tried the method of indeterminate coefficients, variation of constants and by Laplace transform), there is probably something I am missing.
When I look on Wolfram Alpha, it gives me the following answer:
$$ f(x)=c_2\left(x\text{Ei}(x)-e^x\right)+c_1x+x^2 $$
where $\text{Ei}$ is the exponential integral, I am a little surprised that it appears in the solution because it is not a trivial term. I don't know how Wolfram arrives at this solution, but given the head of the original equation, I'm pretty sure there is a simpler solution.
What method should be used to arrive at the general solution of this differential equation?
 A: Whenever you get a multiple of $xf'(x)-f(x)$ in the lower order terms, then $f(x)=x$ is a solution of the homogeneous DE. Now apply the reduction-of-order formula or set for the long way $f(x)=xg(x)$ to reduce the homogeneous equation to
$$
x(xg''(x)+2g'(x))-x^2g'(x)=0.
$$
This is first-order in $g'$, so that
$$
g'(x)=\exp\left(x-2\ln x \right)=\frac{e^x}{x^2}.
$$
To this you can apply partial integration once in a useful manner
$$
g(x)=-\frac{e^x}{x}+\int\frac{e^x}{x}dx. 
$$
The last term is the exponential integral (as we look for one basis solution, the integration constants can be set arbitrarily). The second basis function is thus $x·Ei(x)-e^x$
A: I'm giving the Wolfram solution.
Let $f(x) = x v(x)$, then $f' = x v' + v$ and $x^2 v'' + x (-x+2) v' = -x (x-2)$.
Now let $u(x) = v'(x)$, that gives $\displaystyle u' = \frac{-x -2u + x u + 2}{x} = \left( \frac{2}{x} -1 \right) (-u +1)$.
Divide both sides by $ (-u +1)$ and integrate, then $$\displaystyle \int \frac{u'}{-u +1 } \ dx = \int \left( \frac{2}{x} - 1 \right) \ dx$$
Solving the integrals we have $- \log | -u +1 |  = -x + 2 \log |x| + c_1$, therefore $u(x) = - \frac{\exp(x - c_1)}{x^2} + 1 = c_2 \frac{\exp(x)}{x^2} + 1$.
Substitute back in $u = v'$ and integrate, then $$\begin{array}{l} v(x) & = \displaystyle \int \left( c_2 \frac{\exp(x)}{x^2} + 1 \right) \ dx \\ & = x - \frac{c_3 \exp( x)}{x} + Ei(x) c_3 + c_4 \end{array}$$.
Now substitute back in $f(x) = x v(x)$ and you get the answer $f(x) = x^2 - c_3 \exp(x) + c_3 x Ei(x) +  c_4 x $
A: Hint:
$$xf''(x)-xf'(x)+f(x)=0$$
$y=x$
is an obvious solution of the homogenous DE then try reduction of order method. Substitute $y(x)=xg(x)$ in the DE to get a first order DE and solve it by integrating factor method.
