How to replace a variable in a function?

I'm new to Mathematica and am using it for algebraic manipulation.

I'm trying to figure out how to replace a variable in a user-defined function. I defined a derivative function like this:

f'[x_] := 1/h (f[Subscript[x, 0] + h] - f[Subscript[x, 0]]) -h/2f''[Subscript[x, 0]] - h^2/6 f'''[Subscript[x, 0]]


That works fine. However, now I want to substitute 2h for h. I tried:

Replace[f'[x], h->2h]


but that didn't work. Any help would be appreciated.

• f'[x_] /. h -> 2 h ? Also I don't think that you should define function this way as f'[x_] :=. Are you trying to make an equation to be used in DSolve? Apr 12 '16 at 18:43
• @BlacKow Yes! That worked! I'm not really sure what's the best way to define a function. In this case, I'm working with the forward-difference formula and using it to derive an O(h^3) . I know which steps I have to do but I wanted to avoid writing out all the algebra steps. This has reduced my work load significantly. It's seems to be doing what it's suppose to thus far. Is there a better way to define a purely symbolic function? Apr 12 '16 at 18:59
• Consider the following: Collect[f'[x] /. First @ Solve[f[x + h] == Normal[Series[f[x + h], {h, 0, 3}]], f'[x]], h, Simplify] Apr 12 '16 at 23:16

While it really depends on what you are trying to do, your construction may well cause you problems. You are sort of trying to define an approximation to the derivative of f as a function of f and its second and third derivatives. Here's an alternative approach. Say you define the approximation as

fder[x_, h_] := 1/h (f[x + h]-f[x])-h/2 D[f[x], {x, 2}]- h^2/6 D[f[x], {x, 3}];
fder[x0, h]


$-\frac{1}{6} h^2 f^{(3)}(\text{x0})-\frac{1}{2} h f''(\text{x0})+\frac{f(h+\text{x0})-f(\text{x0})}{h}$

It looks like it did before, but now it is fully functional. For example, you can replace h by 2 h straightforwardly by:

    fder[x0, 2 h]


Or you can define f to be a function (like Sin or Exp or something) and then fder will take on the correct values immediately.

• This is great! You're right this is a better approach. Thanks for the help! Apr 13 '16 at 3:26