2
$\begingroup$

It can be useful to generate a smooth random function over $[-L,L]$ with BSplineFunction:

L=10;
SeedRandom[1]
f[x_] = BSplineFunction[RandomReal[{-1, 1}, 20], SplineClosed -> True][x/(2*L)]

It is easy to plot f[x]

Plot[f[x], {x, -L, L}, PlotRange -> {{-L, L}, All}, Axes -> False, Frame -> True, AspectRatio -> 0.5]

enter image description here

However, sometimes we also want to use its derivatives in a numerical computation, for example, D[f[x], {x, 3}]. Before using it, let us observe its curve first, which becomes many steps! Obviously, such a function is NOT the derivative of f[x] and cannot be used in numerical computations.

Plot[Evaluate[D[f[x], {x, 3}]], {x, -L, L}, PlotRange -> {{-L, L}, All}, Axes -> False, Frame -> True, AspectRatio -> 0.5]

enter image description here

I have not figured out the reason. Any suggestion? Thanks in advance!

$\endgroup$
3
$\begingroup$

It's no surprise that you get a step function with 3 derivatives, since the default SplineDegree is 3. If you want to take 3 derivatives, you should up the SplineDegree:

L = 10;
SeedRandom[1]
f[x_] = BSplineFunction[RandomReal[{-1,1}, 20], SplineClosed->True, SplineDegree->5][x/(2*L)];

Then:

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

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.