# Tag Info

53

Update 2015-7-13: PlotPiecewise now handles discontinuities of non-Piecewise functions. (It basically did this before, but required the head to be Piecewise or contain a built-in discontinuous function, like Sign. All I did was brainlessly wrap a function in Piecewise. I also had to rewrite some of the Reduce/PiecewiseExpand code. One of the more ...

29

Here is the example from the documentation adapted for the OP's data: data = MapIndexed[ Flatten[{#2, #1}] &, {2, 5, 9, 15, 22, 33, 50, 70, 100, 145, 200, 280, 375, 495, 635, 800, 1000, 1300, 1600, 2000, 2450, 3050, 3750, 4600, 5650, 6950}]; f = Interpolation@data (* InterpolatingFunction[{{1, 26}}, <>] *) pwf = Piecewise[ Map[{...

22

There is no documented built-in way to convert the InterpolatingFunction object into explicit Piecewise form (thanks to @MichaelE2 for the link!). So the only possibility to get an explicit interpolating function is to re-implement the built-in Interpolation in the high-level Mathematica language. I have already done this for the built-in "Spline" method ...

19

Mathematica even leaves a gap when the expressions in Piecewise are equal, as long as Mathematica doesn't see the equality. Very simple example test[x_] := Piecewise[{{x, x >= 1}, {Sqrt[x^2], x < 1}}] Plot[test[x], {x, 0, 2}, PlotStyle -> Thick] When you replace Sqrt[x^2] by x, no gap. What you have to understand is that the cracks are ...

19

Important Update Through a copy-paste mistake, I dropped the temperature dependence on the effective heat capacity, which was the point of the exercise. I have modified the post to correct that mistake. Update #2 The difference between liquid and solid thermal conductivity can be quite large (4x for water), so I added support for the temperature dependent ...

17

That $F$ should really be something like $F_n$, defining a family of functions and then your total $F$ would really be the union of the $\{F_n\}$ over the domains where they are non-zero. Then unless I am much mistaken each of those domains will have length $2$ and so we can define your family of functions as you had above and then have a dispatcher ...

17

"... I need it only as a function of x" ClearAll[f] f[x_] := Min[x - Quotient[x, 2], 1 + Quotient[x, 2]] Plot[f[x], {x, 0, 10}] Plot[f[x], {x, -5, 10}] Plot[f[x], {x, 0, 50}]

16

l = {0, 2, 5, 9, 14}; a0 = Total[UnitStep[t - l]] - 1; Plot[a0, {t, First[l], Last[l]}, Exclusions -> None, Frame -> True, PlotStyle -> {Directive[Red, Thick]}]

15

In M11+ you can use the "GetPolynomial" method of an interpolating function to obtain the corresponding piecewise expression (but only when using the default "Hermite" method): InterpolationToPiecewise[if_, x_] := Module[{main, default, grid}, grid = if["Grid"]; Piecewise[ {if @ "GetPolynomial"[#, x-#], x < First @ #}& /@ grid[[2 ;; -...

14

You can force NDSolve to use the finite element method: uif = NDSolveValue[{-u''[x] == UnitBox[(x - 0.5) 0.5/0.2], u[0] == 0, u'[1] == 0}, u, {x, 0, 1}, Method -> {"PDEDiscretization" -> "FiniteElement"}]; Possibly, this might be the default. In any case if you compare to the analytical solution: aif = DSolveValue[{Rationalize[-u''[x] == ...

14

Let us look at what happens a bit and start with your first question (title): "Piecewise imposes internal boundaries in NDSolve - is this expected" Yes, that is expected if the function introduces a discontinuity in one of the coefficients and the spatial discretization method is the finite element method (FEM). The FEM will produce better results if the ...

13

Update: To overcome the issue I mention in the comment, also to make it more general, here is an upgraded version, which can deal with clipping and pointwise exclusion, and preserve styles specified in the original plot. The main function is this discontinuousHighlighter: Clear[discontinuousHighlighter] discontinuousHighlighter[origplot_, {excluMarker_, ...

13

A drawback of the Which command is that it evaluates x repeatedly until one of the conditions of Which is satisfied. This is time consuming when x is a complicated expression or/and when the set of conditions is large. This is not true in Compile as x will always be a number or an array. Multiple uses of x won't slow anything down. It isn't even true ...

12

I have been unsuccessful at getting the automatic discontinuity handling to work. (I get the errors "Failure to project onto the discontinuity surface when computing Filippov continuation".) But manually handling it with WhenEvent works, although it complains about slow convergence to the event locations. Perhaps the discontinuity conditions are ...

12

The undocumented function InternalFromPiecewise seems to take a Piecewise function in the standard form returned by PiecewiseExpand and return a list consisting of a partition of the real numbers into intervals and a list of corresponding values. try = {3 (1 - q), 2 (1 - q) + q, 1 + 1.5 q}; InternalFromPiecewise@ PiecewiseExpand@ Min[try] (* {{0.4 <= ...

12

There is a solution to the problem when the value of the function P1[x,t] on the border does not fall to zero, but to a critical value Pxs. Then the second moving boundary is not needed, and the solution can be obtained using NDSolve (my algorithm) Nmax = 500; xR = 10; t0 = 1/3; tm = 100; Pxs = 1/2; s0 = 1/10; u0 = 2/100; S1[0] = s0; S1[-1] = S1[0]; f[t_]...

11

You could use Series. What's necessary is to know which abscissa values were used for the interpolation. Let's generate some fake data. xVals = RandomReal[{0, 100}, 35] // Sort; yVals = Sin[xVals/30] + RandomReal[.1, 35]; ListPlot[{xVals, yVals}\[Transpose]] Create the interpolation: iPol = Interpolation[{xVals, yVals}\[Transpose]] Get the Series ...

11

Edit: Using a helper function fh will result in no messages and no need to set extra options. σ[t_] := 40000 Sin[0.02 t] C1 = 80000; C2 = 20000; σgr = 15000; fh[t_?NumericQ, x_, y_] := Piecewise[{{C1/(C1 + C2)*y, (σ[t]*y > 0) && ((σ[t] - C2*x >= σgr) || (σ[t] - C2*x <= -σgr))}}, 0] sol = NDSolve[{s[t] == σ[t]/C1 + sep[t], sep'[t] == fh[...

11

This particular ODE can be integrated by the somewhat cumbersome means, s1 = Simplify@ExpToTrig@DSolve[{u1''[r] + k^2 u1[r] + v0 u1[r] == 0, u1[0] == 0}, u1[r], r, Assumptions -> k^2 + v0 > 0][[1, 1]] /. C[1] -> -I c/2 s2 = First@FullSimplify@First@DSolve[{u2''[r] + k^2 u2[r] == 0, u2[r0] == u1[r] /. s1 /. r -> r0, u2'[r0] == D[u1[r] /....

11

You can use the functions SimplifyPWToUnitStep and SimplifySimplifyUnitStep to convert a Piecewise function to one using UnitStep: s[t_] := Piecewise[{{1, t < 300.}, {0, 300. <= t < 1500.}, {1, t <= 3600}}]; s2[t_] := SimplifySimplifyUnitStep[SimplifyPWToUnitStep[s@t]]; s2[t] 1 - UnitStep[-300. + t] + UnitStep[3600 - t, -1500. + t] To ...

11

I think this is a bug, because if we transform the Piecewise function into a combination of UnitStep (which is mathematically equivalent to the original function of course), Integrate integrates without difficulty: um = -(2/3) - 2/(3 (-1 + u)) - (2 u)/3 + u^2/3; up = -(10/3) - 2/(3 (-1 + u)) + (8 u)/3 - u^2/3; sv = SimplifyPWToUnitStep@Piecewise[{{um, u &...

11

It does seem to be a bug. As a temporary workaround, you can re-express your function in terms of either UnitStep[] or Boole[], like so: f[x_] = Dot @@ MapAt[Boole, InternalFromPiecewise[ Piecewise[{{2 x - 3, x > 1}, {-x, -1 <= x <= 1}, {2 x + 3, x < -1}}]], 1] StreamPlot[{y - f[x], -x}, {x, -5, 5}, {y, -5, 5}] Note the use of the ...

11

One more way is as follows. f[x_] := Sum[ Piecewise[{{x - n, 2 n <= x <= 2 n + 1}, {n + 1, 2 n + 1 <= x <= 2 n + 2}}], {n, -Infinity, Infinity}]; Plot[f[x], {x, -4, 5}]

11

Summary: To perform the 3D Fourier Transform of a spherically symmetric function $f(r)$ in Mathematica, use the command (4 Pi)/k FourierSinTransform[f[r] r, r, k] (note the additional factor of $r$ in the function being transformed.) This is a standard problem in scattering theory; one needs to find the transform of the electric potential of a ball of ...

10

Try using MeshShading: Plot3D[ -(x - 12.5)^3, {x, 0, 25}, {y, 0, 20}, MeshShading -> {Table[Hue[x], {x, 0, 1, 1/16}]} ] The Table part generates sixteen different colors from the Hue color function. I did this because the image in this case had sixteen rows. If there are more rows than colors the colors will be reused cyclically.

10

From the documentation of Piecewise: The $\text{cond}_i$ are evaluated in turn, until one of them is found to yield True. The last condition is always True, so that Piecewise can return a value even when all the preceding conditions evaluated to False. In a math textbook, this last case would be written as "otherwise". When you write math notation for ...

9

Pretty cool example! I'm sure the following could be sped up a bit. h[x_] = Piecewise[{{Exp[-1/(1 - (1 - 2 x)^2)], 0 < x < 1}}]; g[x_] := With[{n = Floor[x]}, h[n^2 (x - n)]]; f[x_?NumericQ] := NIntegrate[g[t], {t, 0, x}]; Plot[f[x], {x, 0, 8}] A plot of $g(x)$ helps illustrate what's going on here as well. Plot[g[x], {x, 0, 9.1}, PlotPoints ->...

9

This is just an extended comment. I'm not quite certain what's going on as we can easily implement a shooting method manually here. (Shooting method, in short: parametrize in terms of u'[0] and very the parameter until u'[1] has the desired value.) fun = ParametricNDSolveValue[{-u''[x] == UnitBox[(x - 0.5) 0.5/0.2], u[0] == 0, u'[0] == ud}, u, {x, 0, 1}, ...

9

Here is a (mostly) general routine that (tries to) convert a one-dimensional InterpolatingFunction[] into an equivalent Piecewise[] function: convertToPiecewise::umet = "Unknown interpolation method 1."; SetAttributes[convertToPiecewise, Listable]; convertToPiecewise[iF_InterpolatingFunction, x_, OptionsPattern[{"...

Only top voted, non community-wiki answers of a minimum length are eligible