I like to know where my files reside:
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:
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
Or even more values
or, the function represented as pairs {x,f(x)}, which is sometimes useful
And plotting the function
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 “->”
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:
Ok, the above example was on a more complicated side, needing the Exclusions option
Functions like the one above need the Exclusions option as below
Now we can combine the function ff with two variables as follows
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.
Comment
An alternative way to define a piecewise function is to use