I like to know where my files reside:

PiecewiseFunctions3D_1.png

PiecewiseFunctions3D_2.png

PiecewiseFunctions3D_3.png

PiecewiseFunctions3D_4.png

Piecewise defined functions in Mathematica

General comments

All operating system related commands, pertain to Windows operating system.

This notebook is partly written by ChatGPT. I made some changes, adding things that I like. I will post the original ChatGPT written notebook separately.

Remember, the input cells below, recognized by Consolas font used in them.    

If you need any help with Mathematica try the menu item Help, or google what you need mentioning Mathematica in your search query, or ask ChatGPT, or post a question in our class Discussions on Canvas.

How to use a piecewise defined function in Mathematica?

How to use a piecewise defined function in Mathematica?

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 and x for x ≥ 0

is defined in Mathematica as:

PiecewiseFunctions3D_5.png

Here I want to point out that I like using “strange” names for mathematical objects. In Mathematica, I never call a function f, I always call it something like ff, like an abbreviation for “function f”.

To test this definition in Mathematica I evaluate  

PiecewiseFunctions3D_6.png

PiecewiseFunctions3D_7.png

PiecewiseFunctions3D_8.png

PiecewiseFunctions3D_9.png

Or even more values

PiecewiseFunctions3D_10.png

PiecewiseFunctions3D_11.png

or, the function represented as pairs {x,f(x)}, which is sometimes useful

PiecewiseFunctions3D_12.png

PiecewiseFunctions3D_13.png

And plotting the function

PiecewiseFunctions3D_14.png

PiecewiseFunctions3D_15.gif

Sometimes I like to control the AspectRatio in a plot. AspectRatio->Automatic makes the horizontal units identical in length to the vertical units. To get the arrow as in the code below, one just types  “->”   

PiecewiseFunctions3D_16.png

PiecewiseFunctions3D_17.gif

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 and x-y for x^2+y^2≥1

This can be defined in Mathematica as:

PiecewiseFunctions3D_18.png

PiecewiseFunctions3D_19.png

PiecewiseFunctions3D_20.gif

Ok, the above example was on a more complicated side, needing the Exclusions option

PiecewiseFunctions3D_21.png

PiecewiseFunctions3D_22.png

PiecewiseFunctions3D_23.gif

Functions like the one above need the  Exclusions option as below  

PiecewiseFunctions3D_24.png

PiecewiseFunctions3D_25.gif

Now we can combine the function ff with two variables as follows

PiecewiseFunctions3D_26.png

PiecewiseFunctions3D_27.gif

Sometimes I do not like how Mathematica clips three-dimensional plot. So, I have to use Plot3D options to get plots how I like them. In this case it is  ClippingStyle → None.

PiecewiseFunctions3D_28.png

PiecewiseFunctions3D_29.gif

Comment

An alternative way to define a piecewise function is to use

PiecewiseFunctions3D_30.png

Created with the Wolfram Language