3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Nov 16, 2023 at 21:40
  • $\begingroup$ @lericr oops, h0 is supposed to be h. Corrected! $\endgroup$
    – kichapps
    Nov 16, 2023 at 21:45

2 Answers 2

5
$\begingroup$

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)$

$\endgroup$
3
  • $\begingroup$ 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]? $\endgroup$
    – kichapps
    Nov 16, 2023 at 22:52
  • $\begingroup$ Try definition S[sign_, n_ : 4] = Function[h, Normal[Series[f[x + sign h], {h, 0, n}]]] and use it S[1 ][h] $\endgroup$ Nov 17, 2023 at 8:24
  • $\begingroup$ 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. $\endgroup$
    – kichapps
    Nov 20, 2023 at 20:40
2
$\begingroup$

Or

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

enter image description here

$\endgroup$
3
  • $\begingroup$ So, Normal[] makes the Series[...] get evaluated before the ReplaceAll, /.. Am I understanding this correctly? $\endgroup$
    – kichapps
    Nov 16, 2023 at 22:57
  • $\begingroup$ @kichapps Normal remove the little O , O[y - x]^5. $\endgroup$
    – cvgmt
    Nov 17, 2023 at 1:55
  • $\begingroup$ 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. $\endgroup$
    – kichapps
    Nov 20, 2023 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.