Let $f:\mathbb{R}^2 \to \mathbb{R}_+$ be the function defined piecewise by $$ f(x,y) := \begin{cases} \sqrt{1 - x^2 - y^2} &\text{, if } x^2+y^2 \leq 1 \text{ and } y \geq 0, \\ 0 &\text{, otherwise.} \end{cases} $$
I would like to plot this function in MATLAB. I tried the following code, but only recieve error notifications... (I suspect mistakes in the definition of f and/or the line with Z);
f = @(x,y) piecewise(x^2+y^2<=1, piecewise(y>=0,sqrt(1-x^2-y^2),y<0,0), x^2+y^2>1, 0);
xarray = linspace(-5,5,51);
yarray = linspace(-5,5,51);
[X,Y] = meshgrid(xarray,yarray);
Z = f(xarray,yarray);
% Plot the function %
figure
surfc(X,Y,Z)
xlabel('x')
ylabel('y')
zlabel('f(x,y)')
colorbar
Thanks in advance for any help! :)
Two different naive approaches (they are naive because they don't rigorously treat discontinuities):
.