$$ u=v\left( x,t\right) +r\left( x\right) $$ Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogenous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the nonhomogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$ Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDE\ $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives \begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation} PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows. For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important. $$ \Phi_{0}\left( x\right) =1 $$ And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*} with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can eigenfunction expansion and assume the solution to (1)\ is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation} Plugging this into the PDE (1) gives $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$ But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$ Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes \begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align} To find $C_{n}$ $$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$ For $n=0$ $$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$ But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$ All other $C_{m}\,\ $\ for $m>0$ are zero. Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$) \begin{align*} A_{0}^{\prime}\left( t\right) & =-\frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*} Where $B_{0}$ is integration constant. For $n>0$ (3) becomes $$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$ This has the solution $$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$ Where $B_{n}$ is constant of integration. Hence from (2) \begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*} Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes \begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation} At $t=0$ \begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation} case $n=0$ $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$ But $\lambda_{0}=0$ hence \begin{align*} \int_{0}^{\pi}\sin\left( x\right) dx & =\int_{0}^{\pi}\left( x-\frac {1}{\pi}x^{2}\right) dx+\int_{0}^{\pi}B_{0}dx\\ 2 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{2}{\pi}-\frac{\pi}{6} \end{align*} For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$ For $m=1$ \begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =0 \end{align*} For $m>1$ \begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{m}\\ B_{m} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*} Hence solution (4) becomes \begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
$$ u=v\left( x,t\right) +r\left( x\right) $$ Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogenous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the nonhomogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$ Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDE\ $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives \begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation} PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows. For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important. $$ \Phi_{0}\left( x\right) =1 $$ And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*} with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can eigenfunction expansion and assume the solution to (1)\ is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation} Plugging this into the PDE (1) gives $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$ But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$ Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes \begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align} To find $C_{n}$ $$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$ For $n=0$ $$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$ But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$ All other $C_{m}\,\ $\ for $m>0$ are zero. Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$) \begin{align*} A_{0}^{\prime}\left( t\right) & =-\frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*} Where $B_{0}$ is integration constant. For $n>0$ (3) becomes $$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$ This has the solution $$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$ Where $B_{n}$ is constant of integration. Hence from (2) \begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*} Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes \begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation} At $t=0$ \begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation} case $n=0$ $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$ But $\lambda_{0}=0$ hence \begin{align*} \int_{0}^{\pi}\sin\left( x\right) dx & =\int_{0}^{\pi}\left( x-\frac {1}{\pi}x^{2}\right) dx+\int_{0}^{\pi}B_{0}dx\\ 2 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{2}{\pi}-\frac{\pi}{6} \end{align*} For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$ For $m=1$ \begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =0 \end{align*} For $m>1$ \begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{m}\\ B_{m} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*} Hence solution (4) becomes \begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
$$ u=v\left( x,t\right) +r\left( x\right) $$ Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogenous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the nonhomogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$ Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDE\ $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives \begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation} PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows. For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important. $$ \Phi_{0}\left( x\right) =1 $$ And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*} with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can eigenfunction expansion and assume the solution to (1)\ is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation} Plugging this into the PDE (1) gives $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$ But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$ Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes \begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align} To find $C_{n}$ $$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$ For $n=0$ $$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$ But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$ All other $C_{m}\,\ $\ for $m>0$ are zero. Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$) \begin{align*} A_{0}^{\prime}\left( t\right) & =-\frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*} Where $B_{0}$ is integration constant. For $n>0$ (3) becomes $$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$ This has the solution $$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$ Where $B_{n}$ is constant of integration. Hence from (2) \begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*} Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes \begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation} At $t=0$ \begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation} case $n=0$ $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$ But $\lambda_{0}=0$ hence \begin{align*} \int_{0}^{\pi}\sin\left( x\right) dx & =\int_{0}^{\pi}\left( x-\frac {1}{\pi}x^{2}\right) dx+\int_{0}^{\pi}B_{0}dx\\ 2 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{2}{\pi}-\frac{\pi}{6} \end{align*} For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$ For $m=1$ \begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =0 \end{align*} For $m>1$ \begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{m}\\ B_{m} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*} Hence solution (4) becomes \begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
Solve $u_{t}=u_{xx}$ with initial conditions $u\left( x,0\right) =\sin\left( x\right) $ and B.C. $u_{x}\left( 0,t\right) =1,u_{x}\left( \pi,t\right) =-1$. For $t>0,0<x<\pi$.
Since the boundary conditions are not homogeneous, we can't use separation of variables. Let the solution be be
$$ u=v\left( x,t\right) +r\left( x\right) $$
Where Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogeneoushomogenous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the non-homogeneousnonhomogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$
Substituting Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDEPDE\ $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives
\begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation}
PDE PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows (ref 1). For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important.
$$ \Phi_{0}\left( x\right) =1 $$
And And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*}
with\begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*} with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can use eigenfunction expansion and assume the solution to (1)\ is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation}
Plugging Plugging this into the PDE (1) gives
$$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$
But But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to
$$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$
Since Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes
\begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align}
To \begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align} To find $C_{n}$
$$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$
For For $n=0$
$$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$
But But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$
All All other $C_{m}$$C_{m}\,\ $\ for $m>0$ are zero.
Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$)
\begin{align*} A_{0}^{\prime}\left( t\right) & =- \frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*}
Where \begin{align*} A_{0}^{\prime}\left( t\right) & =-\frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*} Where $B_{0}$ is integration constant. For $n>0$ (3) becomes
$$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$
This This has the solution
$$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$
Where Where $B_{n}$ is constant of integration. Hence from (2)
\begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*}
Since \begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*} Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes
\begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation}
At At $t=0$
\begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation}
case case $n=0$
$$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$
But But $\lambda_{0}=0$ hence
\begin{align*} 0 & =\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) dx+\int_{0}^{\pi }B_{0}dx\\ 0 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{-\pi}{6} \end{align*}
For \begin{align*} \int_{0}^{\pi}\sin\left( x\right) dx & =\int_{0}^{\pi}\left( x-\frac {1}{\pi}x^{2}\right) dx+\int_{0}^{\pi}B_{0}dx\\ 2 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{2}{\pi}-\frac{\pi}{6} \end{align*} For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating
integrating $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$
For For $m=1$
\begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =\frac{2}{\pi} \end{align*}
For \begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =0 \end{align*} For $m>1$
\begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{n}\\ B_{n} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*}
Hence \begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{m}\\ B_{m} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*} Hence solution (4) becomes
\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*} \begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
Maple givessolution verified:
Here is verificationVerification of hand solution (used 10 terms in sum, good enough):
mysol=(x-1/Pi x^2)-2/Pi t-Pi/6+2/Pi-2/Pi
2/Pi Inactivate[Sum[ Sum[((-1)^n+1)/(n^2(n^2-1)) Cos[n x]Exp[-n^2 t],{n,2,Infinity10}]];
];
D[mysol/.Infinity->20//Activate,x]/.t->0/.x->0
(*1*)
D[mysol/.Infinity->20//Activate,x]/.t->0/.x->Pi
(*-1*)
Plot[{ic,Sin[x]mysol/.t->0},{x,0,Pi},AxesOrigin->{0,0}]
Animation of hand solution
Manipulate[
Plot[Evaluate[mysol /. t -> time], {x, 0, Pi},
PlotRange -> {{0, Pi}, {-0.5, 1}}],
{{time, 0, "time"}, 0, 1, .1},
TrackedSymbols :> {time},
Initialization :> {mysol = (x - 1/Pi x^2) - 2/Pi t - Pi/6 + 2/Pi -
2/Pi Sum[((-1)^n + 1)/(n^2 (n^2 - 1)) Cos[n x] Exp[-n^2 t], {n, 2,
10}]}
]
Solve $u_{t}=u_{xx}$ with initial conditions $u\left( x,0\right) =\sin\left( x\right) $ and B.C. $u_{x}\left( 0,t\right) =1,u_{x}\left( \pi,t\right) =-1$. For $t>0,0<x<\pi$.
Since the boundary conditions are not homogeneous, we can't use separation of variables. Let the solution be
$$ u=v\left( x,t\right) +r\left( x\right) $$
Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogeneous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the non-homogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$
Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDE $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives
\begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation}
PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows (ref 1). For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important.
$$ \Phi_{0}\left( x\right) =1 $$
And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*}
with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can use eigenfunction expansion and assume the solution to (1) is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation}
Plugging this into the PDE (1) gives
$$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$
But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to
$$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$
Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes
\begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align}
To find $C_{n}$
$$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$
For $n=0$
$$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$
But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$
All other $C_{m}$ for $m>0$ are zero.
Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$)
\begin{align*} A_{0}^{\prime}\left( t\right) & =- \frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*}
Where $B_{0}$ is integration constant. For $n>0$ (3) becomes
$$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$
This has the solution
$$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$
Where $B_{n}$ is constant of integration. Hence from (2)
\begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*}
Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes
\begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation}
At $t=0$
\begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation}
case $n=0$
$$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$
But $\lambda_{0}=0$ hence
\begin{align*} 0 & =\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) dx+\int_{0}^{\pi }B_{0}dx\\ 0 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{-\pi}{6} \end{align*}
For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating
$$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$
For $m=1$
\begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =\frac{2}{\pi} \end{align*}
For $m>1$
\begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{n}\\ B_{n} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*}
Hence solution (4) becomes
\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
Maple gives
Here is verification
mysol=(x-1/Pi x^2)-2/Pi t-Pi/6+2/Pi-
2/Pi Inactivate[Sum[((-1)^n+1)/(n^2(n^2-1)) Cos[n x]Exp[-n^2 t],{n,2,Infinity}]];
D[mysol/.Infinity->20//Activate,x]/.t->0/.x->0
(*1*)
D[mysol/.Infinity->20//Activate,x]/.t->0/.x->Pi
(*-1*)
Plot[{ic,Sin[x]},{x,0,Pi},AxesOrigin->{0,0}]
Since the boundary conditions are not homogeneous, we can't use separation of variables. Let the solution be
$$ u=v\left( x,t\right) +r\left( x\right) $$ Where $v\left( x,t\right) $ is the solution to $v_{t}=v_{xx}$ and homogenous B.C. $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$ and $r\left( x\right) $ is any reference solution which only needs to satisfy the nonhomogeneous boundary conditions: $r^{\prime}\left( 0\right) =1,r^{\prime }\left( \pi\right) =-1$. By guessing, let $r\left( x\right) =Ax+Bx^{2}$. Let see if this satisfies the boundary conditions. $r^{\prime}=A+2Bx$. At $x=0$ this implies $1=A$. Hence $r=x+Bx^{2}$. Now $r^{\prime}=1+2Bx$. At $x=\pi$ this gives $-1=1+2B\pi$ or $B=-\frac{1}{\pi}$. Therefore $$ r\left( x\right) =x-\frac{1}{\pi}x^{2} $$ Substituting $u=v\left( x,t\right) +r\left( x\right) $ into the PDE\ $u_{t}=u_{xx}$ and noting that $r^{\prime\prime}\left( x\right) =-\frac{2}{\pi}$ gives \begin{equation} v_{t}=v_{xx}-\frac{2}{\pi}\tag{1} \end{equation} PDE (1) is now solved using eigenfunction expansion. We need to find eigenfunctions and eigenvalues of $v_{t}=v_{xx}$ with $v_{x}\left( 0,t\right) =0,v_{x}\left( \pi,t\right) =0$. This is known PDE and have eigenfunctions and eigenvalues as follows. For zero eigenvalue, the eigenfunction is an arbitrary constant. Say $\beta$. let $\beta=1$ since scale is not important. $$ \Phi_{0}\left( x\right) =1 $$ And for $n=1,2,3,\cdots$ \begin{align*} \Phi_{n}\left( x\right) & =\cos\left( \sqrt{\lambda_{n}}x\right) \\ & =\cos\left( nx\right) \end{align*} with eigenvalues $\lambda_{n}=n^{2}$ for $n=1,2,3,\cdots$. Now we can eigenfunction expansion and assume the solution to (1)\ is \begin{equation} v\left( x,t\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \tag{2} \end{equation} Plugging this into the PDE (1) gives $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi_{n}^{\prime\prime}\left( x\right) -\frac{2}{\pi} $$ But $\Phi_{n}^{\prime\prime}\left( x\right) =-\lambda_{n}\Phi_{n}\left( x\right) $ and the above simplifies to $$ \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\frac{2}{\pi} $$ Since eigenfunctions are complete, we can expand $\frac{2}{\pi}$ using them and the above becomes \begin{align} \sum_{n=0}^{\infty}A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) & =-\sum_{n=0}^{\infty}A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) -\sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) \Phi_{n}\left( x\right) +A_{n}\left( t\right) \lambda_{n}\Phi_{n}\left( x\right) & =-C_{n}\Phi_{n}\left( x\right) \nonumber\\ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) \lambda_{n} & =-C_{n}\tag{3} \end{align} To find $C_{n}$ $$ \sum_{n=0}^{\infty}C_{n}\Phi_{n}\left( x\right) =\frac{2}{\pi} $$ For $n=0$ $$ C_{0}\Phi_{0}\left( x\right) =\frac{2}{\pi} $$ But $\Phi_{0}\left( x\right) =1$, hence $$ C_{0}=\frac{2}{\pi} $$ All other $C_{m}\,\ $\ for $m>0$ are zero. Hence (3) becomes, for $n=0$ (since $\lambda_{0}=0$) \begin{align*} A_{0}^{\prime}\left( t\right) & =-\frac{2}{\pi}\\ A_{0}\left( t\right) & =-\frac{2}{\pi}t+B_{0} \end{align*} Where $B_{0}$ is integration constant. For $n>0$ (3) becomes $$ A_{n}^{\prime}\left( t\right) +A_{n}\left( t\right) n^{2}=0 $$ This has the solution $$ A_{n}\left( t\right) =B_{n}e^{-n^{2}t} $$ Where $B_{n}$ is constant of integration. Hence from (2) \begin{align*} v\left( x,t\right) & =\sum_{n=0}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =A_{0}\left( t\right) +\sum_{n=1}^{\infty}A_{n}\left( t\right) \Phi _{n}\left( x\right) \\ & =-\frac{2}{\pi}t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \end{align*} Since $u=v\left( x,t\right) +r\left( x\right) $ then the solution becomes \begin{equation} u\left( x,t\right) =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t+B_{0}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \tag{4} \end{equation} At $t=0$ \begin{equation} \sin\left( x\right) =\left( x-\frac{1}{\pi}x^{2}\right) +B_{0}+\sum _{n=1}^{\infty}B_{n}\cos\left( nx\right) \tag{5} \end{equation} case $n=0$ $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx=\int_{0}^{\pi}\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( \sqrt{\lambda_{0}}x\right) dx+\int_{0}^{\pi}B_{0}\cos\left( \sqrt {\lambda_{0}}x\right) dx $$ But $\lambda_{0}=0$ hence \begin{align*} \int_{0}^{\pi}\sin\left( x\right) dx & =\int_{0}^{\pi}\left( x-\frac {1}{\pi}x^{2}\right) dx+\int_{0}^{\pi}B_{0}dx\\ 2 & =\frac{\pi^{2}}{6}+B_{0}\pi\\ B_{0} & =\frac{2}{\pi}-\frac{\pi}{6} \end{align*} For $n>0$, Multiplying both sides of (5) by $\cos\left( mx\right) $ and integrating $$ \int_{0}^{\pi}\sin\left( x\right) \cos\left( mx\right) dx=\int_{0}^{\pi }\left( x-\frac{1}{\pi}x^{2}\right) \cos\left( mx\right) dx+\sum _{n=1}^{\infty}B_{n}\int_{0}^{\pi}\cos\left( nx\right) \cos\left( mx\right) dx $$ For $m=1$ \begin{align*} 0 & =0+B_{1}\frac{\pi}{2}\\ B_{1} & =0 \end{align*} For $m>1$ \begin{align*} -\frac{1+\left( -1\right) ^{m}}{m^{2}\left( -1+m^{2}\right) } & =\frac{\pi}{2}B_{m}\\ B_{m} & =\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) \end{align*} Hence solution (4) becomes \begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
Maple solution verified:
Verification of hand solution (used 10 terms in sum, good enough):
mysol=(x-1/Pi x^2)-2/Pi t-Pi/6+2/Pi-2/Pi
Sum[((-1)^n+1)/(n^2(n^2-1)) Cos[n x]Exp[-n^2 t],{n,2,10}];
D[mysol,x]/.t->0/.x->0
(*1*)
D[mysol,x]/.t->0/.x->Pi
(*-1*)
Plot[{mysol/.t->0},{x,0,Pi},AxesOrigin->{0,0}]
Animation of hand solution
Manipulate[
Plot[Evaluate[mysol /. t -> time], {x, 0, Pi},
PlotRange -> {{0, Pi}, {-0.5, 1}}],
{{time, 0, "time"}, 0, 1, .1},
TrackedSymbols :> {time},
Initialization :> {mysol = (x - 1/Pi x^2) - 2/Pi t - Pi/6 + 2/Pi -
2/Pi Sum[((-1)^n + 1)/(n^2 (n^2 - 1)) Cos[n x] Exp[-n^2 t], {n, 2,
10}]}
]
\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{m^{2}}\frac{\left( -1\right) ^{m}+1}{m^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}
\begin{align*} u\left( x,t\right) & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=1}^{\infty}B_{n}e^{-n^{2}t}\cos\left( nx\right) \\ & =\left( x-\frac{1}{\pi}x^{2}\right) -\frac{2}{\pi }t-\frac{\pi}{6}+\frac{2}{\pi}+\sum_{n=2}^{\infty}\frac{-2}{\pi}\left( \frac{1}{n^{2}}\frac{\left( -1\right) ^{n}+1}{n^{2}-1}\right) e^{-n^{2} t}\cos\left( nx\right) \end{align*}