# Solve the differential equation $y'=|x|$, $y(-1)=2$

Given the differential equation,

$y'=|x|$, $y(-1)=2$

I believe I understand how to solve the implicit solution but have questions about using the initial condition to solve the explicit solution. I have outlined my solution below in case there is a mistake I have overseen.

I rewrite the differential equation piecewise and solve each piece independently.

$y'=x$ for $x\ge0$

$y'=-x$ for $x<0$

This yields,

$y_1=\frac{1}{2}x^2+c_1$ for $x\ge0$

$y_2=-\frac{1}{2}x^2+c_2$ for $x<0$

I use the initial condition to solve for $c_2$ and get,

$y_2=-\frac{1}{2}x^2+\frac{5}{2}$ for $x<0$

Does this mean that the solution is only $y_2$? I am thinking that if possible we want to find $c_1$ such that the solution is continuous and differentiable for the largest possible interval of definition. If $c_1=c_2$ then the piecewise solution is continuous and differentiable for all reals and satisfies the initial condition.

Those constants aren't independent (there's really only one). $|x|$ is continuous, so the derivative $y'$(and therefore $y$ itself) must also be continuous. So you really only have the one function, defined over all $\mathbb{R}$:
$$y=-\frac 1 2 x^2 + \frac 5 2, x<0$$ $$y=\frac 1 2 x^2 + \frac 5 2, x\geq 0$$
This passes through $(-1,2)$ and solves the equation, like you said. In general the interval on which the solution is defined is taken to be the largest possible such interval. In this case that's the entire real number line.
This differential equation can be integrated directly. We must calculate $$y(x) = 2 + \int_{-1}^x dt\, |t|.$$ Note that we have built in the boundary condition $y(-1) = 2$.
If $x < 0$, $$\begin{eqnarray*} y(x) &=& 2 - \int_{-1}^x dt\, t \\ &=& -\frac{1}{2}x^2 + \frac{5}{2}, \end{eqnarray*}$$ otherwise $$\begin{eqnarray*} y(x) &=& 2 - \int_{-1}^0 dt\, t + \int_0^x dt\, t \\ &=& \frac{1}{2}x^2 + \frac{5}{2}. \end{eqnarray*}$$