1
$\begingroup$

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?

$\endgroup$
  • 4
    $\begingroup$ Bad idea to use upper case N as a variable, since that symbol has a built-in meaning. $\endgroup$ – murray Apr 19 '16 at 14:47
2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Another work around is to curry f. Define f[n_] := f[n] = Function[x, Evaluate@FourierSinSeries[x - 1, x, n]]. Then Plot[{f[1][x], f[5][x]}, {x,0,3}]. $\endgroup$ – evanb Mar 16 '17 at 7:09
  • $\begingroup$ @evanb Good demonstration of currying with memoization. But for safety it is better to use \[FormalX] instead of x in the definition for f. $\endgroup$ – Alexey Popkov Mar 16 '17 at 7:16
  • $\begingroup$ Can you demonstrate? I frankly never understood the use of the Formal parameters. Maybe this deserves its own question? $\endgroup$ – evanb Mar 16 '17 at 7:40
  • $\begingroup$ @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. $\endgroup$ – Alexey Popkov Mar 16 '17 at 7:47
  • 1
    $\begingroup$ @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). $\endgroup$ – Alexey Popkov Mar 16 '17 at 9:48
0
$\begingroup$

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}]
$\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.