# First argument -h is not a valid variable

I am trying to simpligy my life by defining a function that gets the series expansion of $$f(x+h)$$ and $$f(x-h)$$ (at $$x=0$$) by the following code,

S[h_, n_ : 4] := Series[f[x + h], {h, 0, n}]


The intend was to use S as S[h]=$$f(x+h)$$ and S[-h]=$$f(x-h)$$. However, I keep encountering "First argument -h is not a valid variable." in the latter invocation. I have tried Module, Block and /. with no resolve. Please help me out!

I'd also like to keep n to be SetDelayed because I'd want to have series expansions of different error order.

• I don't think you've accurately provided your code, because I don't see h at all on the right hand side of your definition and so I don't understand how it could be causing that error. Nov 16, 2023 at 21:40
• @lericr oops, h0 is supposed to be h. Corrected! Nov 16, 2023 at 21:45

h is a local to Series, you cannot use it as a functionargument.

Try

S[sign_, n_ : 4] := Series[f[x + sign h], {h, 0, n}]

S[+1]


$$f(x)+h f'(x)+\frac{1}{2} h^2 f''(x)+\frac{1}{6} h^3 f^{(3)}(x)+\frac{1}{24} h^4f^{(4)}(x)+O\left(h^5\right)$$

S[-1]


$$f(x)-h f'(x)+\frac{1}{2} h^2 f''(x)-\frac{1}{6} h^3 f^{(3)}(x)+\frac{1}{24} h^4f^{(4)}(x)+O\left(h^5\right)$$

• Thanks this helps for my current need! But can you also suggest what I'd need to do to have h as a variable so that I can, say, use it like S[m]? Nov 16, 2023 at 22:52
• Try definition S[sign_, n_ : 4] = Function[h, Normal[Series[f[x + sign h], {h, 0, n}]]] and use it S[1 ][h] Nov 17, 2023 at 8:24
• Thanks, that works but demands me to use Simplify which is not the case when I use your original solutions. I guess I cannot get the best of both worlds. Nov 20, 2023 at 20:40

Or

s[h_, n_ : 4] := Normal[f[t] + O[t, x]^(n + 1)] /. t -> x + h
s[h] + s[-h]


• So, Normal[] makes the Series[...] get evaluated before the ReplaceAll, /.. Am I understanding this correctly? Nov 16, 2023 at 22:57
• @kichapps Normal remove the little O , O[y - x]^5. Nov 17, 2023 at 1:55
• Got it. However, the Normal is forcing me to use Simplify with (s[h]-s[-h])/h and I am not sure why. Simplify is also factorizing h outside the bracket which is quite annoying. Nov 20, 2023 at 20:38