# Understanding a piecewise function generated by Mathematica

After some simplifications I was eventually get the following piecewise function.

mypdf[x_] =Piecewise[{
{Indeterminate, x == -1.12568412580949},
{(E^(-0.42044483028504587*x - 1.222667146921728*Sqrt[1.1256841258094925 + 1.*x])*(0.04164934621418419 + 0.04164934621418419*E^(2.445334293843456*Sqrt[1.1256841258094925 + 1.*x])))/Sqrt[1.1256841258094925 + 1.*x],
1.*Sqrt[1.1256841258094925 + 1.*x] >= 1.2241479465411274},
{(E^(-1.3335678123173245*x - 1.222667146921728*Sqrt[1.1256841258094925 + 1.*x])*(0.04164934621418419*E^(0.9131229820322787*x) + 0.05004800901350002*E^(0.42044483028504587*x + 2.4453342938612392*Sqrt[1.1256841258094925 + 1.*x])))/Sqrt[1.1256841258094925 + 1.*x],
1.*Sqrt[1.1256841258094925 + 1.*x] >= 1.2241479465050318},
{(E^(-0.42044483028504587*x -1.222667146921728*Sqrt[1.1256841258094925 + 1.*x])*(0.05620334996303362 +0.05620334996303362*E^(2.445334293843456*Sqrt[1.1256841258094925 +1.*x])))/Sqrt[1.1256841258094925 + 1.*x],
1.*Sqrt[1.1256841258094925 + 1.*x] <= 0.9435301862601678},
{(E^(-1.3335678123173245*x - 1.222667146939511*Sqrt[1.1256841258094925 + 1.*x])*(0.05004800901350002*E^(0.42044483028504587*x) + 0.05620334996303362*E^(0.9131229820322787*x + 2.4453342938612392*Sqrt[1.1256841258094925 + 1.*x])))/Sqrt[1.1256841258094925 + 1.*x],
1.*Sqrt[1.1256841258094925 + 1.*x] <= 0.9435301862962627},
{(E^(-0.9131229820322787*x - 1.222667146939511*Sqrt[1.1256841258094925 + 1.*x])*(0.05004800901350002 + 0.05004800901350002*E^(2.445334293879022*Sqrt[1.1256841258094925 + 1.*x])))/Sqrt[1.1256841258094925 + 1.*x],
x >= -1.12568412580949}}, 0]


But I dont understand why Mathematica outputs such a strange form?! Because first of all, the function is defined $2$ times in this range $1. Sqrt[1.12568 + 1. x] >= 1.22415$ and two times in this range $1. Sqrt[1.12568 + 1. x] <= 0.94353$.

Even worse the functions for the same range or condition are not the same! However, I am able to plot this function very nicely. Below is a plot of it

My eventual aim is to transfer this function to Matlab. I know how to do it from here but piecewise function is not recognized by Matlab and I want to define it if possible in terms of UnitSteps. I am also able to do it but as I mentioned the piecewise function above is very strange! the conditions are not defined in terms of for example $0.9<x$ but for example $1. Sqrt[1.12568 + 1. x] <= 0.94353$.

Is it possible to obtain a unser friendly non-ugly function so that I can transer it to Matlab? Why is it two times defined in the same range by Mathematica?

• The "2 times defined" are actually different: 1. Sqrt[1.1256841258094925 + 1. x] >= 1.2241479465411274 and 1. Sqrt[1.1256841258094925 + 1. x] >= 1.2241479465050318. It's a small difference, but a difference nonetheless. And the first one does not preclude the second one from being satisfied. Piecewise "falls" through the cases. The first condition that is true determines the value. – Michael E2 Oct 27 '14 at 3:24
• @MichaelE2 so you are suggesting to take the first condition of both cases. Then I would square both sides of the conditions, simplify and then represent the piecewise function manually in terms of unitsteps. – Seyhmus Güngören Oct 27 '14 at 3:29
• Is there any way to do convertion from piecewise to unitstep form authomatically? – Seyhmus Güngören Oct 27 '14 at 3:39
• 1) I'm suggesting that the original computation might not determine the inequalities sufficiently precisely. 2) I don't know of an automatic way to convert to UnitStep. 3) This will simplify the inequalities: MapAt[Reduce, mypdf[x], {1, All, 2}]. 4) This will remove the overlaps: foo = mypdf[x]; foo[[1, All, 2]] = With[{conds = mypdf[x][[1, All, 2]]}, FoldList[{Or[First@#1, #2], Reduce[#2 && ! Last@#1]} &, {False, Reduce@First[conds]}, Rest[conds]][[All, 2]]]; foo, but I don't think that will be a computational improvement. – Michael E2 Oct 27 '14 at 10:31
• @MichaelE2 this was actually all I needed. Why not posting it as an answer? – Seyhmus Güngören Oct 27 '14 at 21:07

You can use Reduce to simplify each inequality:

MapAt[Reduce, mypdf[x], {1, All, 2}]


Or you can use it to simplify them and remove the overlaps, by folding the preceding ones together and eliminating the complement, as follows. In the code we save the piecewise expression in foo and replace the inequalities with their reduced equivalents (foo[[1, All, 2]] = ...). The Fold arguement has two parts. The first one accumulates all the conditions seen so far (with Or). The second one is the condition of the current rule being processed.

foo = mypdf[x];
foo[[1, All, 2]] =
With[{conds = mypdf[x][[1, All, 2]]},
FoldList[
{Or[First@#1, #2], Reduce[#2 && ! Last@#1]} &,
{False,  Reduce@First[conds]},
Rest[conds]
][[All, 2]]];

Simplify@foo


I don't know Matlab. I would think stringing together a sum of UnitStep functions multiplied by one of the other functions would mean all function parts would be evaluated whenever the whole is evaluated. Perhaps there's a better way (ask on a Matlab site). In any case, it shouldn't be hard to do with the simplified inequalities.