# Substitute a function in an algebraic expression

I have this algebraic expression

``````    x                       L                                             L
⌠                       ⌠                                             ⌠  s
⎮ s*(L - x) f(s) ds   x ⎮ (L - s) f(s) ds                           Y ⎮ ---- ds
⌡                       ⌡                     L                       ⌡ f(s)
0                       x                     ⌠ ⎛    s⎞               0
1:  ------------------- + ------------------- = X ⎮ ⎜1 - -⎟ * f(s) ds + -----------
L                     L              ⌡ ⎝    L⎠                  L
0
``````

and I would like to substitute different expressions, e.g. `sin(2 s)` or `(L -s ) s / 4`, for `f(s)` and finally evaluate the integrals (it's as simple as pressing `=`) — but I don't know how to apply the requested substitution at once (I know how I could, painstakingly, change all the occurrences of `f(s)` one by one)

• Does the menu item `Calc -> Algebra -> Manipulation -> Make substitution` not work? It just works for me for the algebraic expression `int(s*(L-x)*f(s)/L,s,0,x)+int((L-s)*f(s)/L,s,x,L)` with `sin(s)` substituted for `f(s)`. Jun 1, 2022 at 7:16

See the Calc manual on `calc-substitute`.

Quote:

The ‘a b’ (‘calc-substitute’) [‘subst’] command substitutes all
occurrences of some variable or sub-expression of an expression with a
new sub-expression. For example, substituting ‘sin(x)’ with ‘cos(y)’ in
‘2 sin(x)2 + x sin(x) + sin(2 x)’ produces ‘2 cos(y)2 + x cos(y) +
sin(2 x)’. Note that this is a purely structural substitution; the lone
‘x’ and the ‘sin(2 x)’ stayed the same because they did not look like
‘sin(x)’. *Note Rewrite Rules::, for a more general method for doing
substitutions.

The ‘a b’ command normally prompts for two formulas, the old one and
the new one. If you enter a blank line for the first prompt, all three
arguments are taken from the stack (new, then old, then target
expression). If you type an old formula but then enter a blank line for
the new one, the new formula is taken from top-of-stack and the target
from second-to-top. If you answer both prompts, the target is taken
from top-of-stack as usual.