I have a problem trying to find a solution of this problem:
Find a harmonic function $u(x,y)$ in the region ${\operatorname{Im}(z)>0}$ with: $$ u(x,0) = \begin{cases} 1 & \textrm{if } x\ge 0\\ 0 & \textrm{if } x<0 \\ \end{cases} $$
then I'd like to use that idea for solving this other problem:
Find a harmonic function $u(x,y)$ in the region ${\operatorname{Im}(z)>0}$ with: $$ u(x,0) = \begin{cases} T_1 & \textrm{if } x < a\\ T_2 & \textrm{if } a\le x\le b \\ T_2 & \textrm{if } x > b \end{cases} $$
I did this for the first problem:
I defined a $g(z)= z = x+iy$
then I said, for an $h$ in polar coordinates:
$h(g,0)=0$
$h(g,\pi)=1$
so $h(g,\theta)=A\theta+B$
where $A=\frac{1}{\pi}$ and $B=0$
then I said:
$$ u(x,y)=h(\operatorname{Re}(g),\operatorname{Im}(g))=\frac{1}{\pi}\arctan\left(\frac{y}{x}\right) $$
but when I compute $u(x,0)$ it looks like this:
$$u(x,0)=\frac{1}{\pi}\arctan(0)=0, \forall x$$
so it must not be a solution.
What should I do in this situation? What's wrong in the method that I proposed?
There are two problems. First, your boundary conditions are wrong.
$$u(x > 0,0) = h(r,0) = 1$$ $$u(x<0,0) = h(r,\pi) = 0$$
therefore $h(r,\theta) = 1- \dfrac{\theta}{\pi}$
Second, converting back to Cartesian isn't as straightforward as taking the arctangent, since the function $\arctan\left(\frac{y}{x}\right)$ only gives values between $(-\pi/2,\pi/2]$, and it isn't the case here. You have to split up the domain like this
$$ \theta = \begin{cases} \arctan\left(\frac{y}{x}\right), & 0 < \theta < \frac{\pi}{2} \\ \arctan\left(\frac{y}{x}\right) + \pi, & \frac{\pi}{2} < \theta < \pi \end{cases} $$
because when $x < 0$, $y > 0$, $\arctan\left(\frac{y}{x}\right)$ gives an angle in the range $(-\frac{\pi}{2},0]$, which is not what we want.
Finally, we have
$$ u(x,y) = -\frac{1}{\pi}\arctan\left(\frac{y}{x}\right) + \begin{cases} 1, & x> 0 \\ 0, & x < 0 \end{cases} $$
Alternatively, you can also write
$$ u(x,y) = \frac{1}{2} + \frac{1}{\pi}\arctan\left(\frac{x}{y}\right) $$
by noting that $\theta = \frac{\pi}{2} - \phi$ where $\frac{x}{y} = \cot\theta = \tan\phi$