Analytical solution to piecewise Poisson equation in 1D I would like help solving the following analytically
$$\frac{\partial^2 u(x)}{\partial x^2} = - p(x)$$
where $ p(x) = 
     \begin{cases}
       3 & : 0 < x  < 0.4\\
       x & : 0.5 < x < 1\\
       0 & : else
     \end{cases}
   $
and $u(0) = u(1) = 0$
Im stuck on this I have googled, and looked at textbooks, but what I would like is a worked example.
(not homework per se, something I need to understand before I attempt the homework!)
 A: Here is the approach using Green's function $G(x,y)$, which by definition is the solution of $-u''=\delta_y$ with zero boundary values, where $y\in (0,1)$ is fixed and $\delta_y$ is the Dirac delta. The presence of $\delta_y$ says that $u'(y+)-u'(y-)=-1$, and $u'$ is piecewise constant elsewhere. Hence,
$$u(x)=\begin{cases} ax,\quad &x<y \\ (a-1)(x-1), \quad &x>y\end{cases}\tag{1}$$
Since we don't want discontinuities in $u$ (they would contribute $\delta'_y$ to $u''$, which is a higher order singularity than we want), we must have $ay=(a-1)(y-1)$. The only solution is $a=1-y$. Thus, 
$$G(x,y)=\begin{cases} (1-y)x,\quad &x<y \\ y(1-x), \quad &x>y\end{cases}\tag{2}$$
or in more compact (and obviously symmetric) form, 
$$G(x,y)=\min(x,y)-xy\tag{3}$$
This is how  $G(x,1/3)$ looks:

Returning to the general problem $-u''=p$, we can now write down its solution at once:
$$
u(x)=\int_0^1 G(x,y)\,p(y)\,dy \tag{4}
$$
To evaluate the integral (4) for the function in your example, you should consider the cases $x\le 0.4$, $0.4\le x\le 0.5$ and $x\ge 0.5$ separately. 
If $x\le 0.4$: 
$$u(x)=\int_0^{x} y(1-x)\,3\,dy + \int_x^{0.4} x(1-y)\,3\,dy + \int_{0.5}^1 x(1-y)\,y\,dy \tag{5}$$
If $0.4\le x\le 0.5$: 
$$u(x)=\int_0^{0.4} y(1-x)\,3\,dy + \int_{0.5}^1 x(1-y)\,y\,dy \tag{6}$$
If $x\ge 0.5$: 
$$u(x)=\int_0^{0.4} y(1-x)\,3\,dy + \int_{0.5}^x y(1-x)\,y\,dy+\int_{x}^1 x(1-y)\,y\,dy \tag{7}$$
And this is the graph of the solution: 

Note that it is concave, and flat between $0.4$ and $0.5$ as it should be.
A: @5pm's answer using Green's functions is quite nice of course, and generalizes well to higher dimensions and infinite domains. But in 1D, you can get the solution simply by integrating twice. Observe that
$$\frac{\mathrm d}{\mathrm dx}u'(x)=u''(x),$$
so we integrate once from $0$ to $x$ to get
$$u'(x)-u'(0)=\int_0^xu''(t)\,\mathrm dt=-\int_0^xp(t)\,\mathrm dt.$$
Let $q(x)=\int_0^xp(t)\,\mathrm dt$, which you can evaluate analytically. This gives $u'(x)=u'(0)-q(x)$. Now $u'(0)$ is unknown, but no matter; we just integrate again to get
$$u(x)-u(0)=\int_0^xu'(t)\,\mathrm dt=\int_0^x\big(u'(0)-q(t)\big)\,\mathrm dt=u'(0)x-\int_0^xq(t)\,\mathrm dt.$$
Again, let $r(x)=\int_0^xq(t)\,\mathrm dt$. We obtain $u(x)=u(0)+u'(0)x-r(x)$. Applying the boundary conditions $u(0)=u(1)=0$ allows us to solve for $u'(0)$, because $r$ is known already:
$$0=u(1)=u(0)+u'(0)\cdot1-r(1)=u'(0)-r(1).$$
Therefore $u'(0)=r(1)$ and the solution is
$$u(x)=r(1)x-r(x).$$
