Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth connection between piecewise parts

Example piecewise wise function:

f[x_]:=Piecewise[{{x^2, 0<x<1-epsilon},{x,1<x<2-epsilon},{2,x>2}}]

Is there a way to connect these parts in interval epsilon, so I get a smooth function?

EDIT:
By smooth, I don't mean it needs to be derivable in point of connection, just that in some numerical work it looks like a "natural" connection.

EDIT2:
Two black circles represent the points where lies the problem. I'd like it to look like a derivable function (although it doesn't need to be in rigor mathematical sense, but I don't want these two spikes). Red circle represents the part where it looks good.
picture
What I could do is do this by nonlinear fitting the [x-epsilon, x+epsilon], but I was hoping that there was an easier way with piecewise function.

like image 223
enedene Avatar asked Dec 17 '11 16:12

enedene


2 Answers

At first, given a function we should define it precisely on the whole range {x,0,2}, ie. its values on ranges 1-epsilon <= x < 1 and 2 - epsilon <= x < 2. enter image description here

The easiest way is to define f1[x] piecewise linear on the both ranges, however the resulting function wouldn't be differentiable on the gluing points, and it would involve spikes.

To prevent such a situation we should choose (in this case) at least third order polynomials there:

P[x_] := a x^3 + b x^2 + c x + d

and glue them together with f[x] assuming "gluing conditions" (equality of functions at given points as well as of their first derivatives) ie. solve resulting equations :

W[x_, eps_]:= P[x]//. Flatten@Solve[{#^2 == P[#],
                                     1   == P[1], 
                                     2#  == 3a#^2 +2b# +c, 
                                     1   == 3a +2b +c},   {a, b, c, d}]&@(1-eps)

Z[x_, eps_]:= P[x]//. Flatten@Solve[{#  == P[#],
                                     2  == P[2], 
                                     1  == 3a#^2 +2b# +c, 
                                     0  == 12a +4b +c},  {a, b, c, d}]&@(2-eps)  

To visualise the resuls we can take advantege of Manipulate :

f1[x_, eps_]:=  Piecewise[{{x^2, 0 <  x < 1 -eps}, {W[x, eps], 1 -eps <= x < 1},
                           { x , 1 <= x < 2 -eps}, {Z[x, eps], 2 -eps <= x < 2},
                                                   {    2    ,           x >=2}}]; 
Manipulate[ Plot[f1[x, eps],  {x, 0, 2.3}, 
                 PlotRange ->    {0, 2.3}, ImageSize->{650,650}]
                                                        //Quiet, {eps, 0, 1}]

Depending on epsilon > 0 we get differentiable functions f1, while for epsilon = 0 f1 is not differentiable at two points.

Plot[f1[x, eps]/. eps -> .4, {x, 0, 2.3}, PlotRange -> {0, 2.3}, 
                             ImageSize -> {500, 500}, PlotStyle -> {Blue, Thick}]

enter image description here

If we wanted f1 to be a smooth function (infinitely differentiable) we should play around defining f1 in range [1 - epsilon <= x < 1) with a transcendental function, something like for example Exp[1/(x-1)] etc.

like image 80
Artes Avatar answered Sep 24 '22 14:09

Artes


You could do a gradually change between the functions that define the begin and end point of the interval. Below I do this by shifting the weight in the weighted sum of these functions depending on the position in the interval:

ClearAll[f]
epsilon = 0.1;
f[x_] :=
 Piecewise[
  {
   {x^2, 0 < x < 1 - epsilon},
   {Rescale[x, {1 - epsilon, 1}, {1, 0}] x^2 + Rescale[x, {1 - epsilon, 1}, {0, 1}] x, 
      1 - epsilon <= x <= 1}, 
   {x, 1 < x < 2 - epsilon},
   {Rescale[x, {2 - epsilon, 2}, {1, 0}] x + Rescale[x, {2 - epsilon, 2}, {0, 1}] 2, 
      2 - epsilon <= x <= 2},
   {2, x > 2}
   }
  ]

Plot[f[x], {x, 0, 2.5}]

enter image description here

like image 25
Sjoerd C. de Vries Avatar answered Sep 22 '22 14:09

Sjoerd C. de Vries