CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/142-Labs/Lab 03 - The u-Substitution Method.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 03 - The u-Substitution Method

Overview

In this lab, we will use SageMath to perform u-substitutions for both indefinite and definite integrals.

Important SageMath Commands Introduced in this Lab

ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 32: …|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…
Section 5.5

Example 1

We will calculate the indefinite integral x2ex3 dx\displaystyle \int x^2e^{x^3} \ dx by using u-substitution. First, we must decide what substitution to make. Since x3x^3 is the inside function and its derivate 3x23x^2 appears in the integrand, up to a constant multiple, we should let u=x3.u = x^3. We can use SageMath to handle the u-substitution for us. We start by defining f(x)f(x) as our integrand and uu as x3x^3 and then calculating dudu.

u = var('u') ## This is to define u as a variable assume(u>0) ## This is to avoid any problems with the solve command def f(x): return x^2 * e^(x^3) sub = u == x^3 ## We write u == x^3 in order to tell SageMath that this is an equation du = diff(sub.rhs(),x) ## This defines du as the derivative of the right hand side of the equation, u = x^3, with respect to x

Now, we need to substitute both uu and dudu into our original integral. In order to do this, we first need to solve for uu in terms of xx. In this example, it can easily be done by hand to obtain x=u1/3.x = u^{1/3}. We can also use SageMath to obtain the same result using the solve\textbf{solve} command.

solve(sub, x)

Note that SageMath returns both real and complex solutions to the equation u=x3u = x^3 when solving for xx. The function ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{solve_̲for_x} in the package ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲calculus_labs} will return the expected solution when solving the equation for xx. To access this function, we must import it from the package by using the command from package import function\textbf{from} \textit{ package } \textbf{import} \textit{ function}.

from uofsc_calculus_labs import solve_for_x

If SageMath returns the error stating that there is no module named ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲caluculus_labs}, then you will first need to install the package using pip\textbf{pip}. This can be done in SageMath by running the command below.

Note: \textbf{Note: } If you are using a lab computer or the Binder server, then you will not be able to use ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{%pip}; however, ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲calculus_labs} should already be installed.

%pip install uofsc_calculus_labs
from uofsc_calculus_labs import solve_for_x

Once we have imported the function ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{solve_̲for_x}, we use it to solve the equation for xx.

solve_for_x(sub,x)

SageMath returns x=u1/3x = u^{1/3} as desired. Now we can use SageMath to substitute both uu and dudu into our original integral.

g(u) = (f(x)/du).substitute(solve_for_x(sub,x)) g(u)

Therfore, our new integrand is 13eu\dfrac{1}{3}e^u. We can have SageMath display the new integral by using the integrate\textbf{integrate} command along with the parameter hold = true\textbf{hold = true} which will keep SageMath from evaluating the integral.

show(integrate(g(u),u,hold = true))

It follows that under the u-substitution u=x3u = x^3, we have x2ex3 dx=13eu du.\displaystyle \int x^2 e^{x^3} \ dx = \int \dfrac{1}{3} e^u \ du. Now, we can integrate the right integral easily by hand. We see that 13eu du=13eu+C.\displaystyle \int \dfrac{1}{3} e^u \ du = \dfrac{1}{3} e^u + C. Finally, we write our final answer back in terms of xx since that was our starting variable. Hence, we have that x2ex3 dx=13ex3+C.\displaystyle \int x^2 e^{x^3} \ dx = \dfrac{1}{3} e^{x^3} + C.

Example 2

Let us use u-substitition to evalaute 2x(x2+5)4 dx\displaystyle \int 2x(x^2 + 5)^{-4} \ dx. We can again use SageMath to perform the u-substitution by repeating the steps in Example 1 with a new integrand f(x)f(x) and new uu. In order to save time for future examples, we can package all of the steps together into a function which we will call ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub(f, sub)}.

def u_sub(f, sub): u = var('u') assume(u>0) du = diff(sub.rhs(),x) g(u) = (f(x)/du).substitute(solve_for_x(sub,x)).expand() return show(integrate(g(u),u,hold = true))

Before we use ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub} to solve our new problem, let's verify that it works by using it on the integral from Example 1.

def f(x): return x^2 * e^(x^3) u_sub(f, u == x^3)

It works! Now let us try it with the integral 2x(x2+5)4 dx\displaystyle \int 2x(x^2 + 5)^{-4} \ dx. We have that f(x)=2x(x2+5)4f(x) = 2x(x^2 + 5)^{-4} and u=x2+5u = x^2 + 5. We now use ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub} to rewrite the integral in terms of uu.

def f(x): return 2*x*(x^2 + 5)^(-4) u_sub(f, u == x^2 + 5)

Now, we should be able to integrate the new integral without much trouble and get 1u4 du=13u3+C.\displaystyle \int \dfrac{1}{u^4} \ du = -\dfrac{1}{3u^3} + C. To get our final answer, we replace uu with x2+5x^2 + 5 to get 2x(x2+5)4 dx=13(x2+5)3+C.\displaystyle \int 2x(x^2 + 5)^{-4} \ dx = -\dfrac{1}{3(x^2 + 5)^3} + C. We can check our final answer in SageMath by using the normal integrate\textbf{integrate} command. (Don't forget that SageMath does not include the +C+ C.)

show(integrate(f(x),x))

Example 3

Now suppose we wish to evaluate the definite integral 113x2x3+1 dx.\displaystyle \int_{-1}^1 3x^2\sqrt{x^3 + 1} \ dx. We now not only need to rewrite the integrand in terms of uu, we also have to change the bounds to be in terms of uu. We can use a modified version of the ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub} function above to accomplish this.

def u_sub_with_bounds(f, sub, a, b): u= var('u') assume(u>0) new_a = sub.rhs().substitute(x=a) ## Rewrites a in terms of u new_b = sub.rhs().substitute(x=b) ## Rewrites b in terms of u du = diff(sub.rhs(),x) g(u) = (f(x)/du).substitute(solve_for_x(sub,x)).expand() return show(integrate(g(u),u,new_a, new_b, hold = true))

We can now use ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub_with_bounds… to perform a u-substitution on the integral above with integrand f(x)=3x2x3+1f(x) = 3x^2\sqrt{x^3 + 1} and u=x3+1u = x^3 + 1 and bounds a=1a = -1 and b=1b = 1.

def f(x): return 3*x^2 * sqrt(x^3 + 1) u_sub_with_bounds(f, u == x^3 + 1, -1, 1)

We are able to solve this definite integral since we know the antiderivative of u\sqrt{u}. 02u du=23u3/202=23(23/203/2)=423.\displaystyle \int_0^2 \sqrt{u} \ du = \dfrac{2}{3}u^{3/2} \biggr \rvert_0^2 = \dfrac{2}{3}(2^{3/2} - 0^{3/2}) = \dfrac{4\sqrt{2}}{3}. We can use SageMath to verify our answer by using the integrate\textbf{integrate} command with our original integrand and bounds.

show(integrate(f(x),x,-1,1))

Example 4

Use the functions ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub} and ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 10: \textbf{u_̲sub_with_bounds… to simply the following integrals by using u-substitition. Then solve the integrals by hand using their simplified form. Lastly, use SageMath to verify that your answer is correct.

  1. (3x+2)(3x2+4x)4 dx\displaystyle \int (3x + 2)(3x^2 + 4x)^4 \ dx

  2. xsin(x3/21) dx\displaystyle \int \sqrt{x} \sin(x^{3/2} - 1) \ dx

  3. 3x5x3+1 dx\displaystyle \int 3x^5\sqrt{x^3 + 1}\ dx

  4. ParseError: KaTeX parse error: Got function '\sqrt' with no arguments as superscript at position 22: …aystyle \int_0^\̲s̲q̲r̲t̲{7} x(x^2 + 1)^…

  5. 0π/4(1+etan(x))sec2(x) dx\displaystyle \int_0^{\pi/4} \left(1 + e^{\tan(x)}\right) \sec^2(x) \ dx

  6. ee31x(ln(x))2 dx\displaystyle \int_e^{e^3} \dfrac{1}{x(\ln(x))^2} \ dx