Plotting symbolic function using symsum

5.8k Views Asked by At

I'm attempting to plot the temperature distribution with respect to time.

I found that the solution to the Heat Equation with initial temperature distribution of $f(x)=|x|$ on $[-\pi,\pi]$ is

$$u(x,t)=\frac{\pi}{2}+\sum\limits_{n=1}^\infty\frac{2}{\pi n^2}((-1)^n-1)e^{-n^2t}\cos(nx)$$

So far I was able to come up with the following Matlab code:

syms t;
r = symsum((2/(pi*n^2))*((-1^n)-1)*cos(n*x)*exp(-(t*n^2)),n,1,1000);
grid on
ezplot(r+(pi/2),[-1,1],[-1,1])

When I try to run this get the following error:

??? Undefined function or variable "X".

Error in ==> ezplot>ezimplicit at 252
[X,Y] = meshgrid(X,Y);

Error in ==> ezplot at 153
    hp = ezimplicit(cax,f{1},vars,labels,args{:});

Error in ==> sym.ezplot at 71
   h = ezplot(char(f),char(y),varargin{2:end});

Error in ==> fp at 26
ezplot(r+(pi/2),-1,1,-1,1)

I know that I have to adjust something small, since I was able to plot the infinite series $\sum\limits_{n=1}^\infty \frac{x^k}{k!}=e^x$ on $[0,1]$. The big difference between this example and my problem is that what I'm trying to plot has two variables (which I noticed Matlab didn't recognize per the error message), but I read online that I can specify the range for it in the argument of ezplot, yet I still came up with an error.

I'm wondering if there's an easier way of approaching this problem. I'm fairly new to Matlab and learn as I go.