So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate.

In order to do that, I need to calculate the inertial force caused by the sawtooth waveform. And to find the inertial force, I need to calculate second derivative of sawtooth waveform. But because, you know, sawtooth waveform is not differentiable everywhere, I think make an approximation by using a smooth sawtooth waveform is enough. 'Smooth' here, I mean substitute upper tip and bottom tip of the sawtooth waveform into a circular curve of a certain radius.

So in short, my question is: How to make sawtooth wavefrom with a dull tip with Mathematica? Here, although I said sawtooth, it would be better if applicable to any triangle waveform.


What if my triangle waveform is made from a completely arbitrary piecewise function such as below

f = Piecewise[{{10 Mod[x, 1], 0 <= Mod[x, 1] < 0.1}, 
 {-(10/9) Mod[x, 1] + (10/9), 0.1 <= Mod[x, 1] < 1}}];
Plot[Evaluate[f], {x, -3, 3}]

enter image description here

Is it still possible?

  • 2
    $\begingroup$ there is a formula for a fourier series approximation here..demonstrations.wolfram.com/… $\endgroup$
    – george2079
    Commented Dec 5, 2013 at 13:49
  • 3
    $\begingroup$ @george2079 Unfortunately there are Gibbs oscillations. It is very bad especially for derivatives. You need to introduce a smooth cut-off of the Fourier series with one of the window function. It is more difficult task then the Gaussian smoothing. $\endgroup$
    – ybeltukov
    Commented Dec 5, 2013 at 14:11
  • $\begingroup$ Of course.. I answered the title question w/o thinking about what he wanted to do with it. $\endgroup$
    – george2079
    Commented Dec 5, 2013 at 16:16

5 Answers 5


There are nice trigonometric formulas

δ = 0.01;

trg[x_] := 1 - 2 ArcCos[(1 - δ) Sin[2 π x]]/π;
sqr[x_] := 2 ArcTan[Sin[2 π x]/δ]/π;
swt[x_] := (1 + trg[(2 x - 1)/4] sqr[x/2])/2;

Plot[{TriangleWave[x], trg[x]}, {x, -2, 2}, PlotRange -> All]
Plot[{SquareWave[x], sqr[x]}, {x, -2, 2}, PlotRange -> All, Exclusions -> None]
Plot[{SawtoothWave[x], swt[x]}, {x, -2, 2}, PlotRange -> All, Exclusions -> None]

enter image description here enter image description here enter image description here

Previous answer

You can use Gaussian smoothing and interpolation:

n = 1000;
δ = 0.02;

f = Interpolation[Prepend[#, {0.0, #[[-1, 2]]}], PeriodicInterpolation -> True] &@
     Transpose@{#, GaussianFilter[SawtoothWave[#], δ n {5, 1}, 
        Padding -> "Periodic"]} &@Range[1/n, 1.0, 1/n];

Plot[{SawtoothWave[x], f[x]}, {x, -1.2, 1.2}]

enter image description here

Here n is the number of interpolation points and δ is the standard deviation of the smoothing.

The second derivative:

Plot[f''[x], {x, -0.1, 0.1}]

enter image description here

  • $\begingroup$ congratulations on your 10k! :) $\endgroup$
    – rm -rf
    Commented Dec 11, 2013 at 18:14
  • $\begingroup$ @rm-rf Thanks! I learned a lot of interesting things when I was earning this reputation :) $\endgroup$
    – ybeltukov
    Commented Dec 11, 2013 at 18:34
  • $\begingroup$ How to make a square wave function with 2 different width? $\endgroup$
    – Nam Nguyen
    Commented Nov 4, 2020 at 11:28

One approach is to convolve the sawtooth wave directly with a Gaussian kernel. Since this can be done analytically, it is possible to return a function that is in closed form and hence can be differentiated without interpolation.

f = Integrate[SawtoothWave[t/10] Exp[-3 (t - x)^2] , {t, 0, 50}]
Plot[f, {x, 0, 50}]

enter image description here

You can control the amount of "rounding" at the top and bottom by the width of the Exp function. Defining the derivative:

df[x_] := D[f, x];

gives a closed form (though it is not exactly "simple"). The second derivative D[df[x], x] is also possible analytically. The method also works fine with TriangleWave instead of SawtoothWave.

  • $\begingroup$ It is exactly my fist idea! But I tried to integrate from -∞ to ∞ with no luck. $\endgroup$
    – ybeltukov
    Commented Dec 5, 2013 at 23:14
  • $\begingroup$ @ybeltukov -- I think this is why the built-in Convolve function doesn't work. But really, the problem doesn't require the full integral, only a portion is needed. $\endgroup$
    – bill s
    Commented Dec 5, 2013 at 23:26

The Fourier series of the sawtooth is differentiable, being made up of sines. However, as ybeltukov pointed out in a comment I did not read until he made me aware of it, Fourier series of piecewise continuously differentiable functions tend to overshoot a jump discontinuities, something which is called Gibbs phenomenon. For that reason a Fourier series may not work very well for derivatives.

Plot[(2/Pi) Sum[(-1)^k Sin[2 Pi  k t]/k, {k, 10000}], {t, 0, 3}]


Be sure to read ybeltukov's comments below! I'm lifting the content into the answer since its so useful it deserves to be more visible. He suggests this solution:

(2/Pi) N@ Sum[(-1)^k Sin[2 Pi k t]/k BlackmanNuttallWindow[k/2/200], {k, 200}]

He plotted the second derivative and got this good looking graph:


  • $\begingroup$ Gibbs oscillations not die out as the frequency increases. See my comment under OP's question. The second derivative always has heavy oscillating tails. I think it is not good from the physical point of view. $\endgroup$
    – ybeltukov
    Commented Dec 6, 2013 at 0:24
  • $\begingroup$ @ybeltukov I did not see that comment, thank you. I had never heard of Gibbs phenomenon before, however I observed it when I plotted the function for a smaller range of k, but I thought it died out with higher ones. I will update the post accordingly. $\endgroup$
    – C. E.
    Commented Dec 6, 2013 at 0:31
  • $\begingroup$ Try something like (2/Pi) N@ Sum[(-1)^k Sin[2 Pi k t]/k BlackmanNuttallWindow[k/2/200], {k, 200}]. It produces nice shape of the second derivative. $\endgroup$
    – ybeltukov
    Commented Dec 6, 2013 at 0:37
  • 1
    $\begingroup$ Gibbs phenomenon only occurs at jump discontinuities. The Sawtooth wave is a continuous function, so there will be no Gibbs phenomenon for the function, but it will be present in the derivate (which has jump discontinuities). $\endgroup$
    – Aditya
    Commented Dec 6, 2013 at 5:33
  • 2
    $\begingroup$ @Aditya why do you claim the sawtooth wave is continuous function? $\endgroup$
    – Kuba
    Commented Dec 6, 2013 at 7:25

From an answer I saw on Mathematics stackexchange:

Plot[0*x - ArcTan[Tan[Pi*(x - 1/2)]]/Pi - 1/2, {x, 0, 10}]

This paper by Neuwirth describes a function that looks like a smoothened sawtooth. In Mathematica form, the function looks like this:

neuwirth[q_, x_] := Sin[x]/(1 + q^2 - 2 q Cos[x])

The paper considers the case q = 1/2, but it can be adjusted as seen fit, and the function can of course be rescaled and translated as well.

(I will have to defer a plot for the next edit, as I am on gedanken Mathematica currently.)


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.