# The solution I find for the differential equation doesn't work in the interval I need

This is the problem: $$(1-x^2 ) y''-2xy'+2y=x-x^3,\qquad y(2)=0,\qquad y(4)=1.$$

So by inspection I know one of the solutions for the homogenous is $y_1 = x$. Now, to find the other solution I used the following equations:

$$U = \frac{1}{y_1^2}e^{-\int p dx}$$

$$y_2 = y_1 \int_{} U dx$$

Where $y_1=x$ and $\displaystyle p= \frac{-2x}{1-x^2}$

And the result I found is:

$$y_2 = 1+ \frac{x}{2} \ln(1-x)-\frac{x}{2} \ln(1+x)$$

It works very well... except it will not work for $x>1$ and the initial values I have are $y(2)=0$ and $y(4)=1$. Now, I just checked that this solution works too: $$y_2 = 1+ \frac{x}{2} \ln(x-1)-\frac{x}{2} \ln(1+x)$$ and it's actually the solution I need. The problem is I don't know how to get that solution. I mean, I don't want to say "so here you just change $\displaystyle \frac{x}{2} \ln(1-x)$ for $\displaystyle \frac{x}{2} \ln(x-1)$ and you have it!".

Well, that's all. I know how to do the rest.

You seem to have forgotten that $$\int\frac{1}{u}\,du = \ln|u| +C$$ rather than $$\int\frac{1}{u}\,du = \ln u + C.$$ That is, you forgot the absolute value in the integral.
Indeed, you have to do $$\int\frac{-2x}{1-x^2}\,dx.$$ I suspect you did the change of variable $u=1-x^2$, with $du = -2x\,dx$, so that you get $$\int\frac{-2x}{1-x^2}\,dx = \int\frac{du}{u} = \ln|u|+C = \ln|1-x^2|+C = \ln|x^2-1| + C$$ and proceeded from there, forgetting the absolute value.
Once you put the absolute value in, I think (if I didn't make a mistake myself) you'll find that the solution should actually be $$y_2 = 1 + \frac{x}{2}\ln|1-x| - \frac{x}{2}\ln|1+x|$$ and so in the interval you are interested in, $|1-x| = x-1$, while on the interval $(-1,1)$, you get $|1-x|=1-x$. You'll find that the solution on the interval $(-\infty,-1)$ is given by $1 + \frac{x}{2}\ln(1-x) - \frac{x}{2}\ln(-(x+1))$ as well.