I want to find the exact solution of a given elliptic PDE $u_{xx}+u_{yy}=-2\sin x\cos y$ where boundary conditions are $u(x,0)=u(x,\pi)=0$ and $u(0,y)=u(\pi,y)=0$.
I found out that $u(x,y)=\sin x\cos y$ satisfies the PDE if the boundary condition is neglected, but this particular solution does not satisfy the first condition $u(x,0)=u(x,\pi)=0$ because $u(x,0)=\sin x$ and $u(x,\pi)=-\sin x$. In this kind of problem then, how should I modify the solution $u(x,y)=\sin x\cos y$ to fit the boundary problem? Or is there more general way to solve this problem?
Let $v(x,y):=u(x,y)-\sin(x)\,\cos(y)$. Then, $$\frac{\partial^2}{\partial x^2}\,v(x,y)+\frac{\partial^2}{\partial y^2}\,v(x,y)=0$$ with the boundary conditions $$v(x,0)=-\sin(x)\,,$$ $$v(x,\pi)=+\sin(x)\,,$$ and $$v(0,y)=0\,,$$ and $$v(\pi,y)=0\,.$$ Using the standard separation-of-variables technique, we get $v(x,y)=X(x)\,Y(y)$ with $$X''(x)=-\lambda^2\,X(x)\text{ and }Y''(y)=+\lambda^2\,Y(y)$$ for some constant $\lambda$. We may choose $\lambda$ so that $\text{Re}(\lambda)>0$, or $\text{Re}(\lambda)=0$ and $\text{Im}(\lambda)\geq 0$.
That is, we may write $$X(x)=\sin(\lambda\,x+\phi)\text{ and }Y(y)=C\,\cosh(\lambda\, y)+S\,\sinh(\lambda\,y)$$ for some constants $\phi$, $C$, and $S$. We may choose $\phi$ so that $\text{Re}(\phi)\in[0,\pi)$.
As $v(x,0)=-\sin(x)$, we get $\lambda=1$, $\phi=0$, $C=-1$. Because $v(x,\pi)=+\sin(x)$, we obtain $$-\cosh(\pi)+S\,\sinh(\pi)=C\,\cosh(\lambda\,\pi)+S\,\sinh(\lambda\,\pi)=+1\,.$$ That is, $$S=\frac{1+\cosh(\pi)}{\sinh(\pi)}=\text{coth}\left(\frac{\pi}{2}\right)\,.$$ Hence, $$v(x,y)=\sin(x)\,\Biggl(-\cosh(y)+\coth\left(\frac{\pi}{2}\right)\,\sinh(y)\Biggr)\,.$$ Consequently, $$u(x,y)=\sin(x)\,\Biggl(\cos(y)-\cosh(y)+\coth\left(\frac{\pi}{2}\right)\,\sinh(y)\Biggr)\,.$$