I like to know where my files reside:

NotebookDirectory[]

NotebookFileName[]

Directory[]

SetDirectory[NotebookDirectory[]]

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 need to be executed by pressing Shift+Enter while the cursor is in the cell.

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:

ff[x_]:=Piecewise[{{x^2,x<0},{x,x≥0}}]

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

ff[3]

ff[-2]

Or even more values

Table[ff[x],{x,-4,4,1/4}]

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

Table[{x,ff[x]},{x,-4,4,1/4}]

And plotting the function

Plot[ff[x],{x,-3,3}]

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  “->”

Plot[ff[x],{x,-2,2},AspectRatio->Automatic]

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:

fg[x_,y_]:=Piecewise[{{x+y,x^2+y^2<1},{x-y,x^2+y^2≥1}}]

Plot3D[fg[x,y],{x,-2,2},{y,-2,2},Exclusions->And[x^2+y^2==1],BoxRatios->{1, 1, 1}]

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

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

Plot3D[fh[x,y],{x,-2,2},{y,-2,2},BoxRatios->{1, 1, 1}]

Functions like the one above need the  Exclusions option as below

Plot3D[fh[x,y],{x,-2,2},{y,-2,2},Exclusions->None, BoxRatios->{1, 1, 1}]

Now I will call this plot

myPlotfh=Plot3D[fh[x,y],{x,-2,2},{y,-2,2},Exclusions->None, BoxRatios->{1, 1, 1}]

myPlotfh

Export["myPlotfh.png",myPlotfh]

Export["myPlotfh.pdf",myPlotfh]

Export["myPlotfh.jpg",myPlotfh]

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

Plot3D[ff[x+y],{x,-2,2},{y,-2,2},Exclusions->None, BoxRatios->{1, 1, 1}]

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.

Plot3D[ff[x+y],{x,-2,2},{y,-2,2},Exclusions->None,ClippingStyle->None, BoxRatios->{1, 1, 1}]

Comment

An alternative way to define a piecewise function is to use

If[x<0, x^2, x] (* the logic of this comand is, given a condition, if it is True, then the second component evaluates, if the condition is false, then the third component evaluates; compare to the definition of ff above *)