I have a function:

$$ u_1(\mathbf{x})= \begin{cases} 1, & \text{if} \;\;|x_1|\le r_1, |x_2| \le r_2 \\ 0, & \text{otherwise} \end{cases} ,\mathbf{x}=[x_1,x_2]^T \in \mathbb{R}^2 $$

And I'm told that if $r_{1}=r_{2}=5$, then plotting it in Matlab with stem3 should look like this:

enter image description here

Maybe I'm just too exhausted at this point, but I don't see how this is the same function. It looks like it's equal to 1 between 10 and 20 rather than between -5 and 5? Can someone please explain what's going on here?


2 Answers 2


Formally, you are correct. But the question says "should look like this". And not "should be equal too". Apparently, the graph drawn is just a hint of the solution, and one should correctly set the $x$- and $y$-axis markers to get the actual solution, only a shift away with the vector $(-15,-15)$, that could replace this graph based on a $[0,30]^2$ grid. Try:

x = [-15:1:15];
y = x;
[X,Y] = meshgrid(x,y);
z = (abs(X)<=5) .* (abs(Y)<=5);
stem3(z);axis tight
stem3(x,y,z);axis tight

enter image description here


The axes in your plot seem to be indexes into an array, as if you were using stem3(Z) instead of stem3(X,Y,Z).

Check the example in the documentation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.