title: How to use surf to plot sphere function in matlab
body: <p>I'm trying to plot sphere function below, But I'm getting wrong result</p>
  
  <p><a href="https://i.stack.imgur.com/vMURD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vMURD.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/LDZZe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LDZZe.png" alt="enter image description here"></a></p>
  
  <p>Here is the code I'm using</p>
  
  <pre><code>x1 = [-10:1:10];
  x2 = [-10:1:10];
  y = zeros(1,21);
  
  for i = 1:21
      y(i) = sphere([x1(i) x2(i)]);
  end
  Y = meshgrid(y);
  surf(x1,x2,Y);
  colormap hsv;
  </code></pre>
  
  <p><code>sphere.m</code></p>
  
  <pre><code>function [y] = sphere(x)
  d = length(x);
  sum = 0;
  for i = 1:d
      sum = sum + x(i)^2;
  end
  y = sum;
  end
  </code></pre>