9
$\begingroup$

In Mathematica, can I give it an integral and a few substitution rules and have it rewrite the integral in terms of those variables?

$\endgroup$

migrated from math.stackexchange.com Sep 16 '14 at 11:49

This question came from our site for people studying math at any level and professionals in related fields.

  • $\begingroup$ Are you looking for something like this $\endgroup$ – Darksonn Sep 15 '14 at 14:19
  • $\begingroup$ @MarkMcClure I tried but it wouldn't let me make an account there. Also I don't want to post my integral because it's homework-related and don't want people to solve it for me. I just want to know how to verify my work with u-sub so I know I'm doing it properly manually. $\endgroup$ – user176201 Sep 15 '14 at 14:22
  • $\begingroup$ @Darksonn I tried it but it didn't work. I did Replace[Integrate[my integral stuff here], {my rules here}] $\endgroup$ – user176201 Sep 15 '14 at 14:23
  • $\begingroup$ @user176201 What about defining the things you're replacing as variables? reference.wolfram.com/language/guide/VariablesAndFunctions.html $\endgroup$ – Darksonn Sep 15 '14 at 14:25
  • $\begingroup$ @Darksonn My rules look like {w-x -> u}. Right now I am using Wolfram online so maybe it isn't the same as Mathematica. $\endgroup$ – user176201 Sep 15 '14 at 14:26
6
$\begingroup$

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}] *)
$\endgroup$
1
$\begingroup$

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$

$\endgroup$
1
$\begingroup$

For example, ReleaseHold[x^2 HoldForm[D[x,u]]/.x->ArcCos[u]] gives $$-\frac{ArcCos[u]^2}{\sqrt{1-u^2}}.$$

$\endgroup$
  • $\begingroup$ This doesn't seem to work with integrals $\endgroup$ – user176201 Sep 15 '14 at 15:48
  • $\begingroup$ If I surround all of this with Integrate[...,u], it works for me. You should really be asking this on Mathematica.se $\endgroup$ – rogerl Sep 15 '14 at 16:05
  • $\begingroup$ I don't understand your example? It just shows x^2 and arccos(u) etc? Can you use a simpler example? $\endgroup$ – user176201 Sep 15 '14 at 16:08
  • $\begingroup$ 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$. $\endgroup$ – rogerl Sep 15 '14 at 16:16
  • $\begingroup$ Well, I don't see why you can't adapt what I did to that case, using w-x->u. $\endgroup$ – rogerl Sep 15 '14 at 16:21
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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] *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy