1
votes

I want to define a function s[t, n] that returns the value at point t of the FourierTrigSeries of n terms of a given function f(t). For instance, for f(t) = t^2:

s[t_, n_] = FourierTrigSeries[t^2, t, n];  (* 2nd input = indep. variable *)
s[t, 3]
    Out =   Pi^2 / 3 + 4 (-Cos[t] + 1/4 Cos[2 t] - 1/9 Cos[3 t])

which is fine (a function of t). To evaluate it at t = 0:

s[t, 3] /. t -> 0
    Out =   -31 / 9 + Pi^2 / 3

This is what I would like to see as the output of s[0, 3], meaning "evaluate at t=0 the FourierTrigSeries of 3 terms".

However doing s[0,3] makes t = 0 "too early", so the original function f(t)=t^2 becomes identically 0; even the independent variable t (second input to FourierTrigSeries) becomes 0 too, which makes no sense. The result is that Mathematica cannot read my mind and returns as follows:

s[0, 3]
    Out =     FourierTrigSeries[0,0,3]

Summarizing: I would like to define the function s[t, n] such that n is substituted for its value first and, only after that, t is substituted by its value for evaluation.

I have tried doing s[n, t] instead of s[t, n]; also tried := (delayed evaluation) instead of = in different ways, and in combination with things like

ss[tdelay_, n_] = s[t, n] /. t -> tdelay

But nothing seems to work. How can I get the behavior I want?

Thank you in advance!

2

2 Answers

1
votes

Use SetDelayed and a temporary (local) variable.

s[t_, n_] := Module[{t0}, FourierTrigSeries[t0^2, t0, n] /. t0 -> t]

s[0, 3]
-(31/9) + Pi^2/3
0
votes

After seeing Chris's answer I've tried to see why Module was needed and I have found another way which, for some reason, escaped me when I first tried:

f[t_] = t^2;    (* or write t0^2 in line below *)
s[t_, n_] := FourierTrigSeries[f[t0], t0, n] /. t0 -> t
s[0, 3]
    Out =   -(31/9) + Pi^2 / 3

Apparently it can be done without the Module thing...

Thank you anyway!