5
$\begingroup$

I have a function testF22 like this

rndA = RandomReal[{0.1, 0.15}]
rndB = RandomReal[{0.2, 0.25}]


testF22[x_] = 
 If[x <= rndA, Exp[x], If[x >= rndB, Exp[x] - 0.1, 1/zzz]]

zzz - is something unknown. It is a test example. In real program I have similar, but it is difficult to put here because of complexity. unknown variable appears when I use NSolve and it cannot find solution for some input parameters. Let's say I run NSolve many times for different points and it gives me an array of values depend on x, and later I make a function with Interpolation[]. In other words every value of function at point x is a number which is returned by NSolve. I have this gap, if my function is undefined. I put random numbers because I do not know the range of gap, it depends on parameters and can be different.

and if I plot it, it looks like this

Plot[testF22[x], {x, 0, 0.3}]

enter image description here

I would like to fill the gap of my function by last known value like here

enter image description here

I can easily fill it with fixed value like this

testF33[x_] := If[NumericQ[testF22[x]], testF22[x], 0.5]
Plot[testF33[x], {x, 0, 0.3}]

But the problem is, that I do not want to put just fix value lke 0.5 in my exampke, I want to put nearest known defined value, it will be dynamic depends on some parameters or random number in my example. Or maybe there is another way how to fill this gap with something a little bit different?

UPDATE 1

Because I got many answers where it is assumed that I know missing interval, I would like to clarify, that testF22[x_] is an Interpolation of array of data. I do not know what is rndA and rndB. I just put is as an example. I need something like like this:

take existing function testF22[x] with missing data in unknown range and make another one with complete data, without touching testF22 and without knowing it's internal details.

Another example (it is not tested, but just for understanding): In normal case I have something like this

myData = {{0,0},{0.1, 0.5},{0.15, 0.1},{0.16, 0.2},{0.17, 0.3},{0.2, 0.6},{0.25, 0.7},{0.3, 0.8}}

myFunc[x_] = Interpolation[myData][x]

Plot[myFunc[x], {x,0,0.3}]

But in some cases I got something like

myData = {{0,0},{0.1, 0.5},{0.15, zzz},{0.16, zzz},{0.17, zzz},{0.2, 0.6},{0.25, 0.7},{0.3, 0.8}}

myFunc[x_] = Interpolation[myData][x]

Plot[myFunc[x], {x,0,0.3}]

and missing part appears in the graph. And this part {0.15, zzz},{0.16, zzz},{0.17, zzz} I really do not know when it is started and finished. I do not know this numbers 0.15, 0.16, 0.17. It can be different depends on input parameters.

My question is how to make something generic without knowing exact borders of missing data part.

$\endgroup$
  • $\begingroup$ One possibility would be to track the first time the NSolve fails with Check, and then store the last value when it converged (in the example, something like 1.15) and use this value in testF33 instead of 0.5. $\endgroup$ – anderstood Nov 14 '16 at 22:21
  • $\begingroup$ The existing answers work perfectly if the interval of missing data is known, as well as the values and the end of the known parts. My comment above is a suggestion in case you are looking for something more general, which is what I understood. $\endgroup$ – anderstood Nov 15 '16 at 0:07
  • $\begingroup$ @anderstood Yes, I am looking for something more general, because I do not know missing interval. Thanks. I will try your comment. $\endgroup$ – Zlelik Nov 15 '16 at 9:27
3
$\begingroup$

How about

rndA = 0.13339;
rndB = 0.242135;

g[x_] := Piecewise[{{E^x, x <= rndA}, {Exp@Min@{rndA, rndB}, rndA < x < rndB},
         {E^x, x >= rndB}}]

Plot[g[x], {x, 0, 0.3}, Exclusions -> None]

enter image description here


Quite additionally, but might be useful:

f[x_] := Piecewise[{{E^x, x <= rndA}, {1/zzz, rndA < x < rndB}, {E^x, x >= rndB}}]

reg = N @ First @ RegionBounds @ ImplicitRegion[#, x]& @ Reduce[f'[x] == 0, x]

{0.13339, 0.242135}

$\endgroup$
  • $\begingroup$ You did not get it, testF22[x_] is an Interpolation of array of data. I do not know what is rndA and rndB. I just put is as an example. I need something like this: take existing function testF22[x] with missing data and make another one with complete data, without touching testF22 and without knowing it's internal details. $\endgroup$ – Zlelik Nov 15 '16 at 9:07
3
$\begingroup$

You can define your function to plot using Piecewise and a zero-order Interpolation for the missing points, whose positions you can calculate from the values of rndA and rndB. You are still left with two possibilities here though. Zero-order interpolation is essentially connecting the dots with straight lines. There are two ways to do so though: up-then-right, or right-then-up. This behavior (and the opposite behavior by ListPlot) are discussed in this really interesting older question and answers therein: How can the behavior of InterpolationOrder->0 be controlled?

Here you want the "right-then-up" behavior, judging from your plot. Unfortunately, however, Interpolation uses the "other" convention when connecting dots at zero-order. No matter though: $ArgentoSapiens contributed a a clever workaround that achieves just that.

So here's a possible solution then:

rndA = RandomReal[{0.1, 0.15}] (* 0.1285 *)
rndB = RandomReal[{0.2, 0.25}] (* 0.2403 *)

f = Piecewise[{
      {Exp[#], # <= rndA},
      {Exp[#] - 0.1, # >= rndB}
    },
    Interpolation[
      {{rndA, Exp[rndA]}, {rndB, Exp[rndB] - 0.1}}.DiagonalMatrix[{-1, 1}],
      InterpolationOrder -> 0
     ][-#]
   ]&;

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

Mathematica graphics

$\endgroup$
2
$\begingroup$

Just a way to "simulate". I am not sure I understand exactly what is desired and in the following if it may deal with "gap" differently from intended. MeshShading is used to shade the "gap":

i1 = {0.1, 0.15};
i2 = {0.2, 0.25};
f[a_, b_, x_] := 
 Piecewise[{{Exp[x], x <= a}, {Exp[x] - 0.1, x >= b}, {Exp[a], True}}]
Manipulate[
 Plot[f[a, b, x], {x, 0, 0.3}, PlotRange -> {1, 1.2}, 
  MeshFunctions -> (#1 &), Mesh -> {{a, 1.01 b}}, 
  MeshShading -> {Blue, Red, Blue}, Exclusions -> None, 
  Exclusions -> None],
 Evaluate[{a, ##} & @@ i1], Evaluate[{b, ##} & @@ i2]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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