Using Piecewise-defined Functions in Mathematica

Introduction

Piecewise-defined functions are functions that have different definitions for different parts of their domain. In Mathematica, the Piecewise function is used to define such functions.

Defining a Piecewise Function

To define a piecewise function in Mathematica, use the Piecewise function. The general syntax is:
Piecewise[{{expr1, cond1}, {expr2, cond2}, ...}]

For example, the function:
f(x) = 
x^2 for x < 0
x   for x >= 0
is defined in Mathematica as:

f[x_]:=({{\[Piecewise], ({{(x^2), x<0}, {x, x≥0}})}})

Piecewise-defined Functions of Two Variables

For functions of two variables, the process is similar. Consider the function:
g(x, y) = 
x + y for x^2 + y^2 < 1
x - y for x^2 + y^2 >= 1
This can be defined in Mathematica as:

g[x_,y_]:=({{\[Piecewise], ({{x+y, (x^2)+(y^2)<1}, {x-y, (x^2)+(y^2)≥1}})}})

Plotting a Piecewise-defined Function with Plot3D

To plot a piecewise-defined function of two variables, use the Plot3D function. For the function g(x, y) defined above:

Plot3D[g[x,y],{x,-2,2},{y,-2,2}]

Conclusion

Piecewise-defined functions offer a flexible way to define functions that have different behaviors over different parts of their domain. With Mathematica's Piecewise function and plotting capabilities, it's easy to work with and visualize these functions.