Defined function doesn't plot correctly - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T05:07:52Z https://mathematica.stackexchange.com/feeds/question/182762 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/182762 0 Defined function doesn't plot correctly Richard Burke-Ward https://mathematica.stackexchange.com/users/57910 2018-09-28T11:43:08Z 2018-09-28T14:56:02Z <p>I have defined a function that produces a single negative impulse spanning <code>-b</code> to <code>-a</code> and a positive impulse spanning <code>a</code> to <code>b</code>, and gives <code>0</code> for all other values of <code>x</code> (for real <code>x</code>). I then adjust the function so that the area under each single impulse is always equal to <code>-1</code> or <code>1</code>. So:</p> <pre><code>f[x_] := If[x &lt; -b, 0, If[-b &lt;= x &lt;= -a, -((Cos[Pi*((x + (a + b)/2)/((b - a)/2))] + 1)/(b - a)), If[-a &lt; x &lt; a, 0, If[a &lt;= x &lt;= b, (Cos[Pi*((x + (a + b)/2)/((b - a)/2))] + 1)/ (b - a), 0]]]] </code></pre> <p>Here's a plot to show what I'm after:</p> <pre><code>Plot[{f[x] /. a -&gt; 1 /. b -&gt; 2}, {x, -3, 3}, PlotLegends -&gt; "Expressions"] </code></pre> <p><a href="https://i.stack.imgur.com/bqRgd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bqRgd.jpg" alt="enter image description here"></a></p> <p>This pattern should hold true for all values of <code>a&lt;b</code>, no? However, it seems to fail for some values of <code>a</code> and <code>b</code>, and for positive <code>x</code> (but only for positive <code>x</code>, despite the function being so obviously odd). For example</p> <pre><code>Plot[{f[x] /. a -&gt; 0 /. b -&gt; 1, f[x] /. a -&gt; 1 /. b -&gt; 3, f[x] /. a -&gt; 2 /. b -&gt; 5, f[x] /. a -&gt; 3 /. b -&gt; 7, f[x] /. a -&gt; 4 /. b -&gt; 9}, {x, -10, 10}, PlotRange -&gt; Full, PlotLegends -&gt; "Expressions"] </code></pre> <p>produces</p> <p><a href="https://i.stack.imgur.com/n6GEm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n6GEm.jpg" alt="enter image description here"></a></p> <p>What am I doing wrong?</p> https://mathematica.stackexchange.com/questions/182762/-/182763#182763 2 Answer by J. M. will be back soon for Defined function doesn't plot correctly J. M. will be back soon https://mathematica.stackexchange.com/users/50 2018-09-28T11:58:18Z 2018-09-28T14:56:02Z <p>One can judiciously combine <code>Haversine[]</code>, <code>Rescale[]</code>, and <code>Clip[]</code>:</p> <pre><code>twinpulse[x_, {a_, b_}] /; 0 &lt;= a &lt; b := 2 Sign[x] Haversine[Clip[Rescale[Abs[x], {a, b}, {0, 2 π}], {0, 2 π}]]/(b - a) Plot[Table[twinpulse[x, sp], {sp, {{0, 1}, {1, 3}, {2, 5}, {3, 7}, {4, 9}}}] // Evaluate, {x, -10, 10}, PlotRange -&gt; Full] </code></pre> <p><img src="https://i.stack.imgur.com/tLUaW.png" alt="pulses"></p> <hr> <h2>How to invent a function</h2> <p>I'll go over through the thought process I had in coming up with this solution, and along the way point out what was the problem with the OP's solution.</p> <p>Some prerequisites: recall that the <a href="http://mathworld.wolfram.com/Haversine.html" rel="nofollow noreferrer">haversine</a> can be expressed as</p> <p><span class="math-container">$$\operatorname{hav}(x)=\frac{1-\cos x}{2}=\sin^2\frac{x}{2}$$</span></p> <p>It is a <span class="math-container">$2\pi$</span>-periodic function, so one can restrict attention to the interval <span class="math-container">$[0,2\pi]$</span>:</p> <pre><code>Plot[Haversine[x], {x, 0, 2 π}] </code></pre> <p><img src="https://i.stack.imgur.com/39qAy.png" alt="haversine"></p> <p>which is in the right shape for a pulse.</p> <p>The OP gave two additional stipulations for the pulse: it should range over <span class="math-container">$[a,b]$</span>, and it should have unit area. However, the haversine ranges over <span class="math-container">$[0,2\pi]$</span> naturally, so a change of variables is needed.</p> <p><em>Mathematica</em> has a built-in function, <code>Rescale[]</code>, which can remap one interval to another. For this situation, we want a function that remaps the given interval <span class="math-container">$[a,b]$</span> to the haversine's natural interval <span class="math-container">$[0,2\pi]$</span>:</p> <pre><code>Rescale[x, {a, b}, {0, 2 π}] // Together // Factor 2 π (a - x)/(a - b) </code></pre> <p>which can now be composed with the haversine:</p> <pre><code>Haversine[Rescale[x, {a, b}, {0, 2 π}] // Factor] // FunctionExpand (1 - Cos[(2 π (a - x))/(a - b)])/2 </code></pre> <p>At once, we spot a difference between this and the OP's expression: the OP's expression uses <span class="math-container">$1+\cos(\cdot)$</span>, and a different shift <span class="math-container">$(a+b)/2$</span>.</p> <p>From here, we also need to impose the unit area constraint. That means we have to divide by the function by its integral over <span class="math-container">$[a,b]$</span>:</p> <pre><code>Assuming[a &lt; b, Integrate[(1 - Cos[(2 π (a - x))/(a - b)])/2, {x, a, b}]] (-a + b)/2 </code></pre> <p>We're almost there! Now, the OP only wanted a single pulse, while haversine is actually a periodic function. Conveniently, haversine is zero at both ends of the interval, so we just need to add a <code>Clip[]</code> to restrict the result of <code>Rescale[]</code> before it gets passed to the haversine:</p> <pre><code>2 Haversine[Clip[Rescale[x, {a, b}, {0, 2 π}], {0, 2 π}]]/(b - a) </code></pre> <p>This shows only one pulse, so we need one last transformation. We replace <code>x</code> with <code>Abs[x]</code> and multiply by <code>Sign[x]</code>, which enforces the odd function condition. We finally have</p> <pre><code>2 Sign[x] Haversine[Clip[Rescale[Abs[x], {a, b}, {0, 2 π}], {0, 2 π}]]/(b - a) </code></pre> <p>which was the expression I used above.</p> <p>Expressed more conventionally,</p> <pre><code>Assuming[x ∈ Reals &amp;&amp; 0 &lt;= a &lt; b, FullSimplify[FunctionExpand[PiecewiseExpand[ 2 Sign[x] Haversine[Clip[Rescale[Abs[x], {a, b}, {0, 2 π}], {0, 2 π}]]/(b - a)]]]] </code></pre> <p><span class="math-container">$$\begin{cases} -\frac{2 \sin ^2\left(\frac{\pi (a-x)}{a-b}\right)}{a-b} &amp; x&gt;0\land 0\leq \frac{\pi (a-x)}{a-b}\leq \pi \\ \frac{2 \sin ^2\left(\frac{\pi (a+x)}{a-b}\right)}{a-b} &amp; x&lt;0\land 0\leq \frac{\pi (a+x)}{a-b}\leq \pi \end{cases}$$</span></p>