'How can I plot these 3 functions within the range(-4,4) with 0.01 stepsize?

function[]=PieceWise1(x)

if x<=0 then
    root=sqrt(4-(x+2)^2)
    y1=2+root
    disp(y1)
elseif x>0
    y1=-0.5*x
    disp(y1)
end

endfunction

function[]=PieceWise2(x)

if x<=0 then
    root=sqrt(4-(x+2)^2)
    y2=2-root
    disp(y2)
elseif x>0
    y2=0.5*x-4
    disp(y2)
end

endfunction

function[]=PieceWise3(x)

if x<-0.12 then
    y3=-4-(x^-1)/2
    disp(y3)
elseif x>=0
    z=x-2*int(x/2)
    y3=4*(1-z)^2
    disp(y3)
else
    disp(0)
end

endfunction



Solution 1:[1]

   function y1=PieceWise1(x)
     if x<=0 then
        r=sqrt(4-(x+2)^2)
        y1=2+r
      else x>0
       y1=-0.5*x
      end
   endfunction

then you evaluate these functions using the feval function

  Y1=feval(linspace(-4,4,100),PieceWise1)

Solution 2:[2]

function r = PieceWise1(x)
    r = -0.5*x
    s = x<=0
    if or(s), r(s) = 2 + sqrt(4-(x(s)+2).^2), end // .^ instead of ^
endfunction

function r = PieceWise2(x)
    r = 0.5*x - 4
    s = x<=0
    if or(s), r(s) = 2 - sqrt(4-(x(s)+2).^2), end
endfunction

function r = PieceWise3(x)
    r = zeros(x)
    s = x<-0.12
    if or(s), r(s) = -4 - 1./x(s)/2, end  // ./ instead of /
    s = x>=0
    if or(s), r(s) = 4*(1- x(s) + 2*int(x(s)/2)).^2, end
endfunction

x = (-4:0.01:4)';
clf
plot(x,PieceWise1, x,PieceWise2, x,PieceWise3)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge Steer
Solution 2 S. Gougeon