# Mathematica Empty Plot FourierSinSeries function

I'm trying to get a plot of different order Fourier series of the function x-1, why does Mathematica give me an empty plot?

f[x_, N_] := FourierSinSeries[x-1, x, N]
Plot[{f[x, 1], f[x, 5]}, {x, 0, 3}]


Executing f[x,1] and f[x,5] do give me valid functions. A workaround is:

f1=f[x,1]
f5=f[x,5]
Plot[{f1,f5},{x,0,3}]


Why does it only work that way?

• Bad idea to use upper case N as a variable, since that symbol has a built-in meaning. Apr 19 '16 at 14:47

That's because Plot is a numerical function which evaluates its first argument with numerical values of x while FourierSinSeries requires symbolic variable. What happens is that Plot internally makes an assignment like

 x = 0;


and then evaluates

 f[x, 1]

 FourierSinSeries[-1, 0, 1]


The obtained expression isn't numeric, and Plot simply ignores it without warning.

Your workaround works because you preliminarily evaluate FourierSinSeries with unassigned x what results in an expression suitable for plotting:

 x =.;
f[x, 1]


(2 - 4/π) Sin[x]

This behavior (except the absence of a warning message) is documented under "Details and Options" on the Docs page for Plot :

Plot has attribute HoldAll and evaluates f only after assigning specific numerical values to x.

• Another work around is to curry f. Define f[n_] := f[n] = Function[x, Evaluate@FourierSinSeries[x - 1, x, n]]. Then Plot[{f[x], f[x]}, {x,0,3}]. Mar 16 '17 at 7:09
• @evanb Good demonstration of currying with memoization. But for safety it is better to use \[FormalX] instead of x in the definition for f. Mar 16 '17 at 7:16
• Can you demonstrate? I frankly never understood the use of the Formal parameters. Maybe this deserves its own question? Mar 16 '17 at 7:40
• @evanb In your function definition your Evaluate breaks the scope of Function and if x has a value, it will be substituted into the FourierSinSeries[x - 1, x, n] expression before evaluation of FourierSinSeries. The safety of formal parameters comes from the fact that they are Protected and you can't assign values to them. More lengthy explanation can be found in this answer. Mar 16 '17 at 7:47
• @evanb A detailed explanation of the variable renaming mechanics can be found in this answer (and after reading it I realized that my formulation above wasn't quite correct: actually it is namely RuleDelayed who renames x to x\$ inside of Function, because the latter is inert on that stage). Mar 16 '17 at 9:48

## Expressions don't get automatically evaluated inside the Plot function.

This is because Plot is HoldAll

## Workaround: use Evaluate

Including an Evaluate function:

f[x_, n_] := FourierSinSeries[x - 1, x, n];

Plot[{Evaluate@f[x, 1], Evaluate@f[x, 5]}, {x, 0, 3}]

• That's on account of Plot[]'s HoldAll attribute. Apr 21 '16 at 5:37