In Mathematica, can I give it an integral and a few substitution rules and have it rewrite the integral in terms of those variables?
7 Answers
Here is a naive start. It will probably work on most calculus course material, but Solve is not guaranteed to invert every possible substitution. (For instance, it does not check the domain of integration in substitution of trigonometric functions, so it should not be hard to come up with an example where it does not work.) I have always wished there were something built into Mathematica for doing this -- that is to say, a user-interface for such transformations.
There are two ways to do a substitution, let $x = g(u)$ or $u = h(x)$. And there are two kinds of integrals, definite and indefinite. Hence four somewhat similar functions.
ClearAll[substitute];
SetAttributes[substitute, HoldAll];
substitute[Integrate[f_, x_], u_ -> gx_] :=
With[{sub = First@Solve[u == gx, x] /. _C -> 0},
With[{integrand = (f /. sub) D[x /. sub, u]},
Defer@Integrate[integrand, u]]
];
substitute[Integrate[f_, x_], {u_, sub : (x_ -> gu_)}] :=
With[{integrand = (f /. sub) D[gu, u]},
Defer@Integrate[integrand, u]
];
substitute[Integrate[f_, {x_, x1_, x2_}], u_ -> gx_] :=
With[{sub = First@Solve[u == gx, x] /. _C -> 0},
With[{integrand = (f /. sub) D[x /. sub, u],
u1 = gx /. x -> x1,
u2 = gx /. x -> x2},
Defer@Integrate[integrand, {u, u1, u2}]
]];
substitute[Integrate[f_, {x_, x1_, x2_}], {u_, sub : (x_ -> gu_)}] :=
With[{integrand = (f /. sub) D[gu, u],
u1 = u /. First@Solve[gu == x1, u],
u2 = u /. First@Solve[gu == x2, u]},
Defer@Integrate[integrand, {u, u1, u2}]
]
Some simple examples:
substitute[Integrate[x Exp[x^2], x], u -> x^2]
(* Integrate[E^u/2, u] *)
substitute[Integrate[Sin[x] Exp[Cos[x]], x], u -> Cos[x]]
(* Integrate[-E^u, u] *)
substitute[Integrate[Sin[x] Exp[Cos[x]], x], {u, x -> ArcCos[u]}]
(* Integrate[-E^u, u] *)
substitute[Integrate[Sin[x] Exp[Cos[x]], {x, 0, Pi/2}], u -> Cos[x]]
(* Integrate[-E^u, {u, 1, 0}] *)
substitute[Integrate[Sin[x] Exp[Cos[x]], {x, 0, Pi/2}], {u, x -> ArcCos[u]}]
(* Integrate[-E^u, {u, 1, 0}] *)
- 235,386
- 17
- 334
- 747
2022 Update: Included in Mathematica 13.1
As briefly stated in xzczd answer, after eight years, this function was explicitly added to Mathematica v13.1 with the new function IntegrateChangeVariables.
Here is how to perform the same five integral substitutions as in michael-e2 answer with the new Mathematica function:
int1 = Inactive[Integrate][x Exp[x^2], x];
IntegrateChangeVariables[int1, u, u == x^2]
(* Integrate[E^u/2, u] *)
int2 = Inactive[Integrate][Sin[x] Exp[Cos[x]], x];
IntegrateChangeVariables[int2, u, u == Cos[x]]
(* Integrate[-E^u, u, Assumptions -> -1 <= u <= 1 && 0 <= x <= Pi *)
IntegrateChangeVariables[int2, u, x == ArcCos[u]]
(* Integrate[-E^u, u] *)
int3 = Inactive[Integrate][Sin[x] Exp[Cos[x]], {x, 0, Pi/2}];
IntegrateChangeVariables[int3, u, u == Cos[x]]
(* Integrate[-E^u, {u, 0, 1}] *)
IntegrateChangeVariables[int3, u, x == ArcCos[u]]
(* Integrate[-E^u, {u, 0, 1}] *)
- 616
- 5
- 12
-
1This seems like it would be a good addition to the existing community wiki answer. – Michael Seifert Jul 01 '22 at 11:53
I have extended @Michael E2 's answer so that it should (hopefully) play nice with some trig substitutions, namely those involving a Weierstrass substitution. The idea is that I get rid of the annoying trig functions in my integrals by replacing with the usual $t=\tan\frac{x}{2}$ substitution first then let the user define their own custom function of $t$ which then uses the code above.
using this and this post as a guide
$TrigFns = {Sin, Cos, Tan, Csc, Sec, Cot};
tansubs = Replace[$TrigFns, x_ -> x[_], 2] -> (Through[$TrigFns[x]] /. x -> 2 ArcTan[t] // TrigExpand // Together) // Thread
$Assumptions = _ \[Element] Reals;
ClearAll[wsSubstitute,trigSubstitute,t];
SetAttributes[{wsSubstitute,trigSubstitute}, HoldAll];
wsSubstitute[Integrate[f_, x_], t_] := (f //. tansubs) 2/(1 + t^2) // Simplify;
trigSubstitute[Integrate[f_, x_], u_ -> gt_] := With[{integrand = wsSubstitute[Integrate[f, x], t]}, substitute[Integrate[integrand, t], u -> gt]];
trigSubstitute[Integrate[f_, x_], {u_, sub : (t_ -> gu_)}] := With[{integrand = wsSubstitute[Integrate[f, x], t]}, substitute[Integrate[integrand, t], {u, sub}]];
As a couple examples of its usage, I will first simply do a couple Weierstrass substitutions from Wikipedia:
trigSubstitute[Integrate[1/(Cos[x] + 2), x], t -> t]
trigSubstitute[Integrate[1/(a Cos[x] + b Sin[x] + c), x], t -> t]
$$\int \frac{2}{t^2+3} \, dt$$ $$\int \frac{2}{-a t^2+a+2 b t+c t^2+c} \, dt$$
And as a slightly harder example:
trigSubstitute[Integrate[1/(Sqrt[Cos[x] - a] Sqrt[Cos[x] - b]), x], {u, t -> Sqrt[(1 - a)/(1 + a)] Sin[u]}]
$$\int \frac{2 \sqrt{\frac{1-a}{a+1}} \cos (u)}{\sqrt{-\frac{(1-a) \sin ^2(u)}{a+1}-a \left(\frac{(1-a) \sin ^2(u)}{a+1}+1\right)+1} \sqrt{-b \left(\frac{(1-a) \sin ^2(u)}{a+1}+1\right)-\frac{(1-a) \sin ^2(u)}{a+1}+1}} \, du$$
EDIT: Add a //Simplify in the expressions for the integrand in the functions substitute to make the output nicer.
- 765
- 3
- 10
David Parks's Presentations add-on application includes a number of functions for doing indefinite integration step-by-step. For example:
<< Presentations`
integrate[Sin[x] Exp[Cos[x]], x]
(* output displays as the original intregral *)
% // ChangeIntegralVariable[subrule = u -> Cos[x], x]
(* output displays as integral of -e^u du *)
% /. IntegralConstantRule
(* output displays as - (integral of e^u du) *)
% + C // UseIntegralTable[BasicIntegralTable]
(* C - E^u *)
% /. subrule
(* C - E^Cos[x] *)
- 11,888
- 2
- 26
- 50
-
-
A free version, "Free Presentations", lacking the documentation, is downloadable at http://web.archive.org/web/20151002151440/http://home.comcast.net/~djmpark/DrawGraphicsPage.html. There is also a link there to the full version, including documentation, but I don't know whether the author still responds. – murray May 02 '20 at 15:52
One way is to use the Mathematica substitution operator: $f(x)/.\{x\rightarrow u\}$ returns $f(u)$.
Suppose you want to substitute $\cos x = u$, you can write out your integrand on one line then put $/.\{x\rightarrow \textrm{ArcCos}[u]\}$ after it. Bear in mind you will also need to fix the differential factor: $\sin x dx \rightarrow du$
- 268
- 1
- 7
For example, ReleaseHold[x^2 HoldForm[D[x,u]]/.x->ArcCos[u]] gives $$-\frac{ArcCos[u]^2}{\sqrt{1-u^2}}.$$
- 4,209
- 3
- 27
- 42
-
-
If I surround all of this with Integrate[...,u], it works for me. You should really be asking this on Mathematica.se – rogerl Sep 15 '14 at 16:05
-
I don't understand your example? It just shows x^2 and arccos(u) etc? Can you use a simpler example? – Sep 15 '14 at 16:08
-
How much simpler could it be? If you want to compute $\int x^2,dx$ using the substitution $u = \cos x$ (I realize you wouldn't do this), write in Mathematica Integrate[ReleaseHold[x^2 HoldForm[D[x,u]]/.x->ArcCos[u]],u] and then resubstitute for $u$. – rogerl Sep 15 '14 at 16:16
-
Well, I don't see why you can't adapt what I did to that case, using w-x->u. – rogerl Sep 15 '14 at 16:21
F[w_] := 5+3*(w-x)and then calling it asF[u+x]– Sep 15 '14 at 14:29