I'd like to plot a mesh for both f and g functions below in MATLAB:

I'd tried this for f and g:
%% plot f
[x,y] = meshgrid(linspace(-pi,pi,50));
f = x.*y ;
subplot(1,2,1)
mesh(f)
title('f')
%% plot g
syms m n
A = 4*(-1)^(m+n)*(sin(m*x)*sin(n*y))/(m*n);
g = symsum(symsum(A,n,1,inf),m,1,inf);
subplot(1,2,2)
mesh(g)
title('g')
The result of mesh is:

The section plotting f is running without any error. The other section plotting g show nothing in the figure. How can I plot g?
If you're going to work with symbolic math, it's a good idea to get comfortable with assumptions, especially when dealing with periodic functions and functions with discontinuities. You may also want to use fmesh (or ezmesh in older versions) to plot meshes of symbolic expressions:
syms m n x y
assume(in(m,'integer') & m>=1);
assume(in(n,'integer') & n>=1);
assume(x>-pi & x<pi);
assume(y>-pi & y<pi);
A = 4*(-1)^(m+n)*(sin(m*x)*sin(n*y))/(m*n);
g = symsum(symsum(A,n,1,Inf),m,1,Inf);
fmesh(g,5*[-pi pi -pi pi],'MeshDensity',1e2); % or ezmesh(g,5*[-pi pi -pi pi]);
This creates a plot like this:

Another option is to evaluate g numerically using subs and double and then use mesh to plot:
[X,Y] = meshgrid(linspace(-5*pi,5*pi,100));
g2 = real(double(subs(g,{x,y},{X,Y})));
mesh(g2);
or use matlabFunction to create a numeric function:
g2 = matlabFunction(g);
[X,Y] = meshgrid(linspace(-5*pi,5*pi,100));
mesh(real(g2(X,Y)));
In both these latter cases, real must be used to clip the insignificant imaginary parts due to numerical imprecision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With