4

I am trying to plot the level set of L1 norm in matlab. But it is not at all working, and I am stuck. Any help ?

x = linspace(-1,1,10);
y = linspace(-1,1,10);
[xm,ym] = meshgrid(x,y);
z = sum(abs(xm-ym));
surfc(x,y,z)
Shew
  • 1,557
  • 1
  • 21
  • 36

1 Answers1

5

Change your fourth line into this, in accordance with the definition of L1 norm:

z = abs(xm)+abs(ym);

You could do it more efficiently using with bsxfun to avoid generating the matrices xm, ym:

x = linspace(-1,1,10);
y = linspace(-1,1,10);
z = bsxfun(@plus, abs(x), abs(y).');
surfc(x,y,z)

Either of the two approaches produces:

enter image description here

For a better picture you should increase sampling, and perhaps remove surface edges:

x = linspace(-1,1,100);
y = linspace(-1,1,100);
z = bsxfun(@plus, abs(x), abs(y).');
surfc(x,y,z,'edgecolor','none')

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147