1
$\begingroup$

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

enter image description here

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?

$\endgroup$
5
  • 2
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Oct 27, 2014 at 3:24
  • $\begingroup$ @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. $\endgroup$ Commented Oct 27, 2014 at 3:29
  • $\begingroup$ Is there any way to do convertion from piecewise to unitstep form authomatically? $\endgroup$ Commented Oct 27, 2014 at 3:39
  • $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Oct 27, 2014 at 10:31
  • $\begingroup$ @MichaelE2 this was actually all I needed. Why not posting it as an answer? $\endgroup$ Commented Oct 27, 2014 at 21:07

1 Answer 1

2
$\begingroup$

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

Mathematica graphics

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.