MATH102ManualPython 231
MATH102ManualPython 231
Department of Mathematics
College of Computing and Mathematics
1
Contents
1 Week 1: Areas and Distances (5.2 & 5.3) 14
1.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2 Week 2: Antiderivatives & The Fundamental Theorem of Calculus (5.1 & 5.4) 27
2.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.5 Example 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.6 Example 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2
9 Week 9: Trigonometric Substitution and Partial Fractions (8.4 & 8.5) 78
9.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.5 Example 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.6 Example 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.7 Example 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
10 Week 10: Rational Functions of Sine and Cosine & Improper Integrals (8.7 & 8.8) 84
10.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
10.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
10.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
10.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11 Week 11: Sequences & Series and Convergence (9.1 & 9.2) 87
11.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
11.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
11.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.5 Example 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12 Week 12: The integral Test and p-series & Comparison of Series (9.3 & 9.4) 91
12.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
12.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
13 Week 13: Alternating Series & Ratio and Root Tests (9.5 & 9.6) 95
13.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
13.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14 Week 14: Power Series & Taylor and Maclaurin Series (9.8 & 9.10) 98
14.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3
Acknowledgments
We would like to thank Mr. Firas Melaih and Ms. Sara Alkiswani for their valuable
feedback on the previous version of the manual.
4
Introduction
Python has become the most trending language in the scientific community due to its
accessibility and efficiency. It has been proven to be powerful in various scientific com-
puting areas, such as artificial intelligence, data analytics, and their applications.
In this manual, we introduce the mathematical packages needed to tackle problems re-
lated to Engineering Calculus. We illustrate these packages on several examples such as
integration, sequences, and series.
Arithmetic Operations
Symbol Operation Example
+ Addition 2+3
- Subtraction 2-3
* Multiplication 2*3
/ Division 2/3
Some examples of using Python as a calculator are given below
2*(5+4)
18
4/2 + 3
5.0
5 + 13 /2
11.5
9.0
Storing Values
a = 33*7 # a is stored in the memory
b = 11/3
a #to display a
231
5
b #to display b
3.6666666666666665
Notice that Python shows the outcome of the last variable only, to show them all use the
command display() or print()
display(a,b)
231
3.6666666666666665
print(a,b)
231 3.6666666666666665
print("a = ", a)
a = 231
print('b = ', b)
b = 3.6666666666666665
Exponent
We use ** to calculate the exponents for example, if we want to calculate 23 we type 2**3.
2**(3)
2**(0.5)
1.4142135623730951
sqrt(2)
␣
,→---------------------------------------------------------------------------
6
<ipython-input-15-5693fe9b0a8a> in <module>
----> sqrt(2)
NameError: name 'sqrt' is not defined
Here Python is not like MATLAB, it is more like an environment or platform where we
can recall libraries (packages) for specific purposes of calculation and processing.
import sympy as smp #Here 'as' means that we type 'smp' whenever we␣
,→want to recall this library
import numpy as np #Here 'as' means that we type 'np' whenever we want␣
,→to recall this library
Now whenever we want to use a function, we include smp. or np. in it. For example
smp.sqrt(5) or np.sqrt(5)
smp.sqrt(5)
√
5
np.sqrt(5)
2.23606797749979
√
Notice that when we asked for 5 through Sympy it returns the symbolic output of it
while Numpy gave its numerical value.
smp.log(4)
log (4)
np.log(4)
7
1.3862943611198906
for simplicity we can use the following the command to call everything from sympy or
numpy.
Be warned that by using this for two libraries together will cause the later library to over-
ride the former one.
sin(4)
sin (4)
The line of codes below shows how we call the number π from different libraries keeping
in mind that we set the default library to be sympy.
display(v_1, v_2,v_3)
sin(v_3)
π
3.141592653589793
π
0
8
x
smp.sin(x)
sin ( x )
The table below gives the syntax for the most used mathematical functions in sympy.
Mathematical Expression Sympy Mathematical Expression Sympy
sin( x ) sin(x) cot−1 ( x ) acot(x)
cos( x ) cos(x) sec−1 ( x ) asec(x)
tan( x ) tan(x) − 1
csc ( x ) acsc(x)
cot( x ) cot(x) e x exp(x)
sec( x ) sec(x) ln( x ) ln(x)
csc( x ) csc(x) logy ( x ) log(x,y)
−1
sin ( x ) asin(x) x n x**n
√
cos−1 ( x ) acos(x) x sqrt(x)
√
tan−1 ( x ) atan(x) n
x root(x,n)
If we want to define an unknown function then we simply use Function from sympy
t = smp.Symbol('t')
f=smp.Function('f')(x)
display(f)
f (x)
f ( x, t)
f(x, t)
Now let us define a function explicitly in terms of known functions
h = smp.sin(x) + x + smp.exp(x)
display(h)
x + e x + sin ( x )
Let us try to find the value of h when x = π
h(smp.pi)
9
␣
,→---------------------------------------------------------------------------
<ipython-input-29-5317fc83087a> in <module>
----> 1 h(smp.pi)
Using the previous definition of a function, we can not substitute as h(x). We have two
ways to do the substitution:
(1) We define the function using .subs( , ) as follow
h.subs(x,smp.pi)
π + eπ
(2) We define the function using lamba as follow
<function __main__.<lambda>(x)>
e x + sin ( x )
q(x) = exp(x) + sin(x)
Then we can substitute as we do in MATLAB
q(5)
sin (5) + e5
To get the numerical value of q(5) (or any numerical presented as a symbolic expression)
we use the command float to transform it into decimal form
float(q(5))
10
147.45423482791347
11
Plotting Functions/Equations
There are many libraries that can be used for plotting for example, sympy and matplotlib.
In sympy we use the command plot for plotting functions of a single variable.
smp.plot(x**(0.5))
We can plot equations using plot_implicit but first we have to define the variables that
are involved in the equation
Notice that we set the variables to be real numbers by adding real = True
12
Exercise
(1) Write a Python code to print the function
f ( x ) = x2 + 5, x ∈ R
13
Week 1
Solution
(a) First, we import the needed packages and libraries
Next, we declare the variables and the function, for example, we set a = 0, b = 4 and
n = 3.
x=smp.symbols('x', real=True)
f = smp.lambdify(x, 1/(1 + x**2), "numpy")
# The lambdify keyword is used to allow the function to take values over
# an array of values, namely, xvalues as you are going to see below
a=0; b=4; n=3
You can display the entered data and the output by using the command display() or
print(). It is a good way to understand how the program treats and processes the data.
display(f(x))
display(a)
display(b)
display(n)
( x2 + 1)−1.0
0
14
4
Note: Sometimes, the display command may not be able to show the expression in
case we used a function from sympy library through lambdify.
We define the line space for x and then the line space for the values of the function f at
each x.
Now we evaluate the width of subinterval, midpoints and then the Riemann sums.
# left-end points
Re_R = (np.sum(yvalues[1:])) * deltax # Reimann Sum when the samples are␣
,→the
# right-end points
Re_M = (np.sum(yvalues_M)) * deltax # Reimann Sum when the samples are␣
,→the
# midpoints
print("Approximating area using left-end points is ", Re_L)
print("Approximating area using right-end points is ", Re_R)
print("Approximating area using midpoints is ", Re_M)
array([0.66666667, 2. , 3.33333333])
15
fig = plt.figure(figsize = (8, 8)) #This command is for determining the␣
,→size of the figure
plt.plot(xvalues,yvalues,'r')
[<matplotlib.lines.Line2D at 0x181c33c3248>]
As we see in the figure that the curve of the function is not smooth enough as we know.
Thus, we need to expand the line space for x values.
16
ymorevalues = f(Xmorevalues)
Now we try to plot the function using the above line spaces
[<matplotlib.lines.Line2D at 0x181c45b3208>]
17
plt.plot(xvalues, yvalues, 'm.', markersize=15)
x_left = xvalues[:-1]
y_left = yvalues[:-1]
plt.bar(x_left, y_left, width= deltax, alpha=0.75 , align='edge',␣
,→edgecolor='b')
18
plt.plot(xvalues, yvalues, 'm.', markersize=15)
x_right = xvalues[1:]
y_right = yvalues[1:]
plt.bar(x_right, y_right, width= -deltax, alpha=0.75 , align='edge',␣
,→edgecolor='b')
19
plt.plot(midpoints, yvalues_M, 'm.', markersize=15)
x_mid = midpoints
y_mid = yvalues_M
plt.bar(midpoints, yvalues_M, width= deltax, alpha=0.75 , align='center',␣
,→edgecolor='b')
20
x=smp.symbols('x', real=True)
f = lambda x: 1/(1+x**2) # The Lambda keyword is to define f as a␣
,→function of x
# left-end points
Re_R = (np.sum(yvalues[1:])) * deltax #Reimann Sum when the samples are␣
,→the
# right-end points
Re_M = (np.sum(yvalues_M)) * deltax #Reimann Sum when the samples are␣
,→the
# midpoints
print("Approximating area using left-end points is ", Re_L)
print("Approximating area using right-end points is ", Re_R)
print("Approximating area using midpoints is ", Re_M)
Xmorevalues = np.linspace(a,b,n*n+100) # We increased the line space to␣
,→n*n+100
plt.figure(1)
fig = plt.figure(figsize = (8, 8))
ymorevalues = f(Xmorevalues)
plt.plot(Xmorevalues,ymorevalues,'r')
plt.plot(xvalues, yvalues, 'm.', markersize=15)
x_left = xvalues[:-1]
y_left = yvalues[:-1]
plt.bar(x_left, y_left, width= deltax, alpha=0.75 , align='edge',␣
,→edgecolor='b')
21
plt.figure(2)
fig = plt.figure(figsize = (8, 8))
plt.plot(Xmorevalues,ymorevalues,'r')
plt.plot(xvalues, yvalues, 'm.', markersize=15)
x_right = xvalues[1:]
y_right = yvalues[1:]
plt.bar(x_right, y_right, width= -deltax, alpha=0.75 , align='edge',␣
,→edgecolor='b')
plt.figure(3)
fig = plt.figure(figsize = (8, 8))
plt.plot(Xmorevalues,ymorevalues,'r')
plt.plot(midpoints, yvalues_M, 'm.', markersize=15)
x_mid = midpoints
y_mid = yvalues_M
plt.bar(midpoints, yvalues_M, width= deltax, alpha=0.75 , align='center',␣
,→edgecolor='b')
22
23
24
Exercise 1
√
Consider the function f ( x ) = 1 + 2x.
Write a program that calculates the approximating area of the region under the curve of f
where the user enters the interval endpoints a and b and the desired number of rectangles
using left-end points.
Hint You may need to use the following commands:
• input() (important to use)
• float()
• int()
25
Exercise 2
Solve Ex.31, Ex.34, Ex.67 and Ex.68 in Section 5.2.
26
Week 2
Solution
(a) We use the command integrate from sympy as follow
x5 − 3x + 9
2084
3
We repeat the same steps as above but with changing the function name. For part (b) we
have
g = smp.Function('g')(x)
g = smp.sec(x)**2 + smp.sin(x) #smp.sec for secant and smp.sin for sine
display(g)
27
smp.integrate(g, (x,0,smp.pi/3)) # smp.pi is to call pi
sin ( x ) + sec2 ( x )
1 √
+ 3
2
Part (c)
g1 = smp.Function('g1')(x)
g1 = smp.sqrt(5/x) #smp.sqrt is to call the square root
display(g1)
smp.integrate(g1, (x,1,6))
√
r
1
5
x
√ √
−2 5 + 2 30
For part (d) we need to define a piece-wise function.
g2 = smp.Function('g2')(x)
g2 = smp.Piecewise((6, (0<= x) &(x<2)), (9 - x**3 , (2<= x) & (x <= 4)))
# In the above syntax, we use & for expressing the condition 0<= x< 2 as␣
,→combined
# inequalities.
display(g2)
smp.integrate(g2, (x,1,4))
(
6 for x ≥ 0 ∧ x < 2
3
9 − x for x ≥ 2 ∧ x ≤ 4
−36
Exercise 1
Solve Ex.7, Ex.14, Ex.21, Ex.28, and Ex.41 in Section 5.4. (Hint: You may need to use the
command Abs() for defining the absolute value of an expression)
28
2.2 Example 2
Find the derivative of the function
Z x2
F(x) = ln(4 + t) dt.
1−2x
Solution
t = smp.Symbol('t', real=True)
f1 = smp.Function('f1')(x)
Notice that the answer expressed in terms log instead of ln because Python considers it
as the natural logarithm.
Exercise 2
Solve Ex.75, Ex.79, Ex.83, and Ex.86 in Section 5.4.
(Hint: You may need to use the command ‘smp.diff(function, variable, n)where n stands
for the order of the derivative.
2.3 Example 3
Verify by differentiation that the following formula is correct
Z
ln(1 + x ) dx = ( x + 1) ln(1 + x ) − (1 + x ) + C.
Solution
We differentiate the right-hand side of the equation.
29
f = (x+1)* smp.ln(1+x) - (1+x) # The expression of f where we ignored␣
,→the constant C (why?)
display(f)
display(smp.diff(f,x))
− x + ( x + 1) log ( x + 1) − 1
log ( x + 1)
Since the derivative output is the integrand, the equation is correct.
2.4 Example 4
Find the general indefinite integral.
R√ 5
1. x (2 + x − x 3 ) dx
R u+u1/2 2
2. u dx
Solution
We simply use the command integrate. For part (1)
f= smp.sqrt(x) * (2+x-x**(5/3))
display(f)
smp.integrate(f,x)
√ 1.66666666666667
x x−x +2
5 3
2x 2 4x 2
+ − 0.315789473684211x3.16666666666667
5 3
Notice that the exponent on x is expressed as an approximated decimal. To keep the
precision (i.e. the fraction), we use the command Rational( , ).
f= smp.sqrt(x) * (2+x-x**smp.Rational(5,3))
30
display(f)
smp.integrate(f,x)
√ 5
x −x + x + 2
3
19 5 3
6x 6 2x 2 4x 2
− + +
19 5 3
Also, notice that Sympy does not include the constant of integration. If needed, we add it
by ourselves. For example
c = smp.Symbol('c')
f= smp.sqrt(x) * (2+x-x**smp.Rational(5,3))
display(f)
smp.integrate(f,x)+c
√ 5
x −x 3 + x + 2
19 5 3
6x 6 2x 2 4x 2
c− + +
19 5 3
For part (2)
u = smp.Symbol('u', real=True)
g = smp.Function('g')(u)
g= ((u + u**(smp.Rational(1,2)))/u)**2
display(g)
√ 2
u+u
u2
√
4 u + u + log (u)
For part (3)
h = smp.Function('h')(x)
31
display(h)
smp.integrate(h)
smp.simplify(smp.integrate(h))
(sin ( x ) + 1) tan ( x )
Exercise 3
Solve Ex.17, Ex.33 and Ex.36 in Section 5.1.
2.5 Example 5
Given the acceleration function is a(t) = t2 − 4t + 1 with initial velocity v(0) = 6 for a
particle moving along a line during the period of time 0 ≤ t ≤ 10. Find
(a) the velocity at time t and
(b) the distance traveled during the given time interval.
Solution
(a) First, we find the general antiderivative of the acceleration function
t = smp.Symbol('t', real=True)
c = smp.Symbol('c') # We need to define the constant of␣
,→integration
a=smp.Function('a')(t)
v =smp.Function('v')(t)
a= t**2 -4*t+1
display(a)
display(v)
t2 − 4t + 1
32
t3
c+ − 2t2 + t
3
To solve for c, we first evaluate v(0) by using the command .subs( , )
v.subs(t,0)
c
Now we solve the equation v(0) = c + 45 = 3 by using the command solve. (The solution
of this equation is obviously c = −42)
display(c1)
v = v.subs(c,c1)
6
t3
− 2t2 + t + 6
3
R 10
(b) The distance formula is 0 |v| dt . Implementing this using integrate leads to the
traveled distance
smp.integrate(smp.Abs(v), (t,0,10))
√ 4 √ 3
√
!2
3 33 3 33
2 + 2 √ 3 33 4 2 + 2 1687
− − 6 33 − + + +
6 2 2 3 6
Exercise 4
Solve from Ex.94 to Ex.98 in Section 5.4.
2.6 Example 6
(Calculus Early Transcendentals, James Stewart Section 6.5, Question number 12)
2
Consider the function f ( x ) = 2xe− x on the interval [0,2]
Find the average value of f
Find c such that f ave = f (c)
33
Solution
1. define the function as specified in the question
2. calculate the average value of f
3. substitute c in place of x in the function f
4. solve the equation f ave = f (c) for c
5. Use matplotlib functionality to plot the rectangle and the function.
f_ave = (1/2)*integrate(f,(x,0,2))
f_ave
0.5
0.5 −
e4
Now define f (c) by substituting the symbol c in place of x in the function f
c = smp.Symbol('c', real=True)
f_c = f.subs(x,c)
f_c
2
2ce−c
2
Next we need to find c so that f (c) = f ave , in other words, find c so that 2ce−2c = 0.5 − 0.5
e4
.
However, solving the equation symbolically (in exact form) is not feasible (try the code
line solve(f_c-f_ave,c)).
So we need to find a suitable numerical approximation. For this purpose, we can use the
command nsolve from sympy which gives the numerical solutions to nonlinear equations.
To use this code we need to have a first guess to the solution(s) (like in Newton’s method)
which can be done from the graph.
The Figure below shows the plot of the function f c for c ∈ [0, 2] and the plot of f ave the
constant function (horizontal line).
Note that the plot below does not answer does not give us the desired values of c. It
simply lets us know the possible intersection of the curves f c and f ave . This will help us
34
choose an initial guess for the command nsolve.
<sympy.plotting.plot.Plot at 0x181c68caec8>
From the graph above, we have an idea that f (c) equals f ave around c = 0.25 and c = 1.25.
We will now solve the equation c from the equation f (c) − f ave = 0 using initial guesses
0.25 and 1.25.
c1 = nsolve(f_c-f_ave,c,0.25)
c1
0.262997081730703
c2 = nsolve(f_c-f_ave,c,1.25)
c2
1.28740318600638
Then the numbers c satisfying the f ave = f (c) are approximately c1 ≈ 0.2630 and c2 ≈
1.2874, to 4 decimal places.
35
Exercise 5
Solve Ex.47, Ex.48, Ex.49, Ex.53 in Section 5.4.
36
Week 3
(arctan( x ))2
Z
dx
x2 + 1
Z
5x sin(5x ) dx
1
Z
√ dx
(1 + x )4
Z p
x x2 + a2 dx, where a is a constant
Solution
We use the command integrate as follow
atan2 ( x )
x2 + 1
atan3 ( x )
3
y2 = 5**x * smp.sin(5**x)
display(y2)
smp.integrate(y2)
5x sin (5x )
cos (5x )
−
log (5)
37
y3 = 1 / (1+smp.sqrt(x))**4
display(y3)
smp.integrate(y3)
1
√ 4
x+1
√
3 x 1
− 3 √ − 3 √
3x 2 + 9 x + 9x + 3 3x 2 + 9 x + 9x + 3
y4 = x*smp.sqrt(x**2 + a**2)
display(y4)
p
x a2 + x 2
√ √
a2 a2 + x 2 x 2 a2 + x 2
+
3 3
Exercise 1
Solve Ex.13, Ex.18, Ex.21, Ex.24 and Ex.28 in Section 5.5.
3.2 Example 2
Make a substitution first and then evaluate the integral
Z 2
x dx
√
0 3x + 5
Solution
The command transform from Sympy will substitute an expression in the integral, with
the corresponding limits, by a variable.
38
i = smp.Integral(x / smp.sqrt(5 + 3*x),(x,0,2)) # We use the command␣
,→Integral to produce an unevaluated integral
display(i)
display(k)
Z2
x
√ dx
3x + 5
0
Z11 u
3−5
√ 3 du
3 u
5
smp.simplify(k)
√ √
8 11 20 5
− +
27 27
This substitution method is useful when you want to transform the integral without eval-
uating it.
Exercise 2
Solve Ex.43, Ex.47, Ex.53, Ex.61, Ex. 69, Ex. 79, Ex.84 in Section 5.5.
39
Week 4
Exercises 1
Solve Ex.16, Ex.26, Ex.39, and Ex. 70 in Section 5.7.
Exercises 2
Solve Ex.12, Ex.15, Ex.21, Ex.32, and Ex.66 in Section 5.8.
Exercises 3
Solve Ex.53, Ex.60, Ex.77 (without using the formula), and Ex.84 (without using the for-
mula) in Section 5.9.
40
Week 5
Solution
We will only use sympy functions in solving the question. Let us follow the
steps below:
1. We declare x and y as symbols. This requires the symbols function
from sympy package
2. We solve for the points of intersection of these two curves. To do this
symbolically, we need solve function
3. We will plot the two curves using the plot functionality
plot_implicit from sympy
solve((x-y**2+4*y, x-2*y+y**2), x, y)
41
# store the intersection points in t
sol = solve((x-y**2+4*y, x-2*y+y**2), x, y)
p1 = plot_implicit(x-y**2+4*y)
p2 = plot_implicit(x-2*y+y**2)
42
#The extend command is used in order to plot the two graphs on␣
,→the same figure
p1.extend(p2)
p1.show()
43
From the plots above, we see that the curve x = 2y − y2 is bigger than
x = y2 − 4y (in the sense that it lies to the right on the region of interest.
And we know the intersection points in values of y are (0, 3)
R3
So the area is given by calculating the definite integral 0 (6y − 2y2 ) dy
Since we know the region of interest is to the left of x = 2y − y2 and to
the right of x = y2 − 4y, we can visualize the region using the implicit_plot
with And command.
plot_implicit(And(x>y**2-4*y, x<2*y-y**2))
44
<sympy.plotting.plot.Plot at 0x181c4fddec8>
integrate(6*y - 2*y**2,(y,0,3))
5.2 Example 2
(Calculus Early Transcendentals, James Stewart Section 6.1, Question num-
ber 4)
Sketch the region enclosed by the given curves y = x4 and y = 2 − | x |.
Then find the area of the region.
45
Solution
p1 = plot_implicit(y - x**4)
p2 = plot_implicit(y + Abs(x) - 2)
p1.extend(p2)
p1.show()
46
47
It clear that the intersection points lie between x = −2 and x = 2. Now we
restrict our plots to [−2, 2].
48
49
From the third graph, one may guess that the x-coordinate points of inter-
section are x = −1 and x = 1 and the curve y = 2 − | x | is above y = x4 .
By using the solver command nsolve we get
nsolve(x**4 + Abs(x)-2,x,-1)
−1.0
nsolve(x**4 + Abs(x)-2,x,1)
1.0
50
You can check your solution by using the command
checksol(expression, variable, solution)
checksol(x**4 + Abs(x)-2,x,-1)
True
checksol(x**4 + Abs(x)-2,x,1)
True
51
<sympy.plotting.plot.Plot at 0x181c4dc1e48>
13
5
Exercises 1
Solve Ex.21, Ex.24, Ex.30, Ex.34, Ex.39, Ex.48, Ex.72 in Section 7.1.
52
Week 6
Solution
p1 = plot_implicit(y-x**2)
p2 = plot_implicit(x-y**2)
p1.extend(p2)
p1.show()
53
54
It is clear from the third plot above that the region of interest lies above y = x2 and x = y2 .
The region is drawn by the command below. The values of x are limited to [0, 2] in the
resulting plot.
The third graph shows the region, and the line y = 1 on the same figure. It is clear from
this figure that the object resulting from the rotation will be a washer.
55
56
√
The inner and outer radius are given by rin = 1 − x and rout = 1 − x2 , respectively.
Then the volume will be calculated as
Rb 2 2 ) dx
vol = π a (rout − rin
The limits of integration a and b are obtained by solving for the intersection of the two
curves. It is obvious from the graph that a = 0 and b = 1. This will be verified by solving
for the intersections points
sol
[(0, 0),
(1, 1),
((-1/2 - sqrt(3)*I/2)**2, -1/2 - sqrt(3)*I/2),
((-1/2 + sqrt(3)*I/2)**2, -1/2 + sqrt(3)*I/2)]
The complex roots are of no interest here, so the root are ( x, y) = (0, 0) and ( x, y) = (1, 1).
Which gives a = 0 and b = 1.
vol
11π
30
6.2 Example 2
(Calculus Early Transcendentals, James Stewart Section 6.2, Question number 6)
Find the volume of the solid obtained by rotating the region bounded by the curves 2x =
y2 , x = 0 and y = 4 about the y-axis. Sketch the region.
Solution
Based on the three curves given in the question, three inequalities are formulated and the
region satisfying the inequalities is plotted below.
57
It is obvious that the region below the x-axis does not involve the line y = 4, which is
required in the question. Thus, a constrain y > 0 has to be added to the plot_implicit
command. So the command and result below.
58
Rotating the above region about the y-axis, it is clear that the resulting solid is a disk.
Using the method of disk, the volume is given by
Rd
V = π c r2 dy
where c and d are simply the maximum and minimum values of y in the above figure.
y2
So c = 0 and d = 4. Also, the radius r is given by r = 2
The volume is evaluated with the integrate command below
r = (y**2)/2
vol = pi*integrate(r**2, (y,0,4))
vol
256π
5
Exercises 1
Solve Ex.14, Ex.19, Ex.24, Ex.31, Ex.39, Ex.51, Ex.73 and Ex.74 in Section 7.2.
59
6.3 Example 3
(Calculus Early Transcendentals, James Stewart Section 6.3, Question number 5)
Use the method of cylindrical shells to find the volume generated by rotating the region
2
bounded by the curves y = e− x , y = 0, x = 0, x = 1 about the y-axis.
Solution
The curves are shown below
p1 = plot(exp(-x**2))
p2 = plot_implicit(x - 1, x_var=x)
p1.extend(p2)
p1.show()
60
61
p3 = plot_implicit(And(x < 1 , x > 0, y > 0, y < exp(-x**2)), (x,-1,1),␣
,→(y,-1,1))
Rotating the above region about the y-axis and using the method of cylinderical shells,
the volume will be computed as
Using the method of disk, the volume is given by
Rb
V = 2π a rh dx
where a and b are simply the maximum and minimum values of x in the shaded region
of the above figure.
2
So a = 0 and b = 1. Also, the height h is given by h = e− x and radius r is given by r = x
The volume is evaluated with the integrate command below
h, r = exp(-x**2), x
vol = 2*pi*integrate(r*h, (x,0,1))
vol
1 1
2π −
2 2e
62
The simplify command: It can be useful for simplifying mathematical expressions. See the
example below:
vol.simplify()
π
− +π
e
The factor command can be useful for factorizing (collecting some like terms) in mathe-
matical expressions. See sample usage below
vol.factor()
π (−1 + e)
e
Exercises 2
Solve Ex.19, Ex.23, Ex.26, Ex.33, and Ex.35 in Section 7.3.
63
Week 7
Solution
1. Declare x and y as symbols. This requires the symbols or Symbol functions of sympy
package
2. Plot the implicit curve given in the question, to verify the symmetry. To do this sym-
bolically, we need to import the plot_implicit function of the sympy.plotting package
from the sympy library. For the implicit plot, the equation of the curve is written so
that the right-hand side equals zero.
3. setup the integral for the length of the curve and evaluate the integral. Use the
Integral and integrate functions for this purpose.
64
<sympy.plotting.plot.Plot at 0x235b3023188>
Clearly, from the plot above, it suffices to compute the length of the curve in any quadrant
and multiply by 4 to find the total length.
In the first quadrant, y = (1 − x2/3 )3/2 , 0 ≤ x ≤ 1
So the length of the implicit curve is
R1p
4 0 1 + (dy/dx )2 dx
First define the y as given above
y = (1 - x**(2/3))**(3/2)
y
1.5
0.666666666666667
1−x
#integrate(sqrt(1+diff(y,x)**2), (x,0,1))
it will run for a long time without yielding a result. The command is given above, how-
ever, it is commented. One reason for this to happen is that the number 23 on the expo-
65
nent of x has been replaced by the number 0.666666666666667 which makes the calcula-
tions more complicated to be processed. We can get around this by using the command
Rational( , ) from sympy to consider the fraction as it is without representing in decimal
form
y = (1 - x**(Rational(2,3)))**(3/2)
y
2
1.5
1 − x3
A step by step approach for evaluating the integral is given in the cells that follow.
• Compute 1 + (dy/dx )2
p
• Compute 1 + (dy/dx )2
• Integrate the resulting quantity
z = 1 + diff(y)**2
z.simplify()
2 1.0
1.0 1 − x 3
1+ 2
x3
The challenge here is that sympy sees 1.0 as a rational number not the same as the integer
1. See the comparison in the cell below.
• The first cell directly below shows that if you have any symbolic x and multiply by
1.0, then in sympy it is no longer same as x (in terms of its value).
• The cell that follows says if the symbol x is multiplied by 1, it remains the same as x
(in terms of the value it stores).
So combining the above reasonings, it is clear that 1 is not the same as 1.0 in sympy.
x == x * 1.0
False
x == x * 1
True
So to overcome this problem, first substitute 1.0 with 1 in z, then simplify z and verify
that the desired output is obtained.
The outcome of the substitution is shown in the cell directly below.
66
The second cell that follows shows a combined application of subs and simplify.
z.subs(1.0,1)
2
1 − x3
1+ 2
x3
sqrt(z.subs(1.0,1)).simplify()
1
√
3
x
Now we are in the integration step
integrate(sqrt(z.subs(1.0,1)), (x,0,1))
3
2
Do not forget to multiply the integral by 4 in order to get the length
4*integrate(sqrt(z.subs(1.0,1)), (x,0,1))
7.2 Example 2
(Calculus Early Transcendentals, James Stewart Section 8.1, Question number 35.)
1. Find the arc length function for the curve y = 2x3/2 with starting point P0 (1, 2).
2. Use the arc length function obtained in (1.) above to find the length of the curve
y = 2x3/2 for
a. 1 ≤ x ≤ 2
b. 1 ≤ x ≤ 3
c. 2 ≤ x ≤ 3
Solution
First import the necessary libraries from sympy
Declare symbols x and t.
Define y given in the question as a function of t
The compute the arc length function
67
Rxp
s( x ) = 0 1 + (dy/dt)2 dt
y = 2*t**Rational(3,2)
The command below is used to directly compute the desired integral to give the arc length
function
s
3 √
2 (9x + 1) 2 20 10
−
27 27
a. Length of the curve 1 ≤ x ≤ 2
The solution is given by s(2). Substitute 2 in place of x in the arc length function, s
s.subs(x,2)
√ √
20 10 38 19
− +
27 27
To get a numerical value, use the evalf function
s.subs(x,2).evalf()
3.79231876485844
b. Length of the curve 1 ≤ x ≤ 3.
The solution is given by s(3). Substitute 3 in place of x in the arc length function, s
s.subs(x,3)
√ √
20 10 112 7
− +
27 27
s.subs(x,3).evalf()
8.63254050503209
c. Length of the curve 2 ≤ x ≤ 3.
The solution here is given by s(3) − s(2)
s.subs(x,3) - s.subs(x,2)
68
√ √
38 19 112 7
− +
27 27
(s.subs(x,3) - s.subs(x,2)).evalf()
4.84022174017365
Or apply evalf to each one separately and take the difference
s.subs(x,3).evalf() - s.subs(x,2).evalf()
4.84022174017365
Exercises 1
Solve Ex.8, Ex.16, Ex.24, Ex.29, and Ex.33 in Section 7.4.
7.3 Example 3
(Calculus Early Transcendentals, James Stewart Section 8.2, Question number 10.)
√
The curve y = 1 + e x , 0 ≤ x ≤ 1 is rotated about the x-axis. Find the area of the
resulting surface.
Z b q
s= 2πy 1 + (dy/dx )2 dx
a
y = sqrt(1+exp(x))
y
√
ex + 1
(1+diff(y,x)**2).simplify()
e2x
4 + ex + 1
ex + 1
69
y*sqrt((1+diff(y,x)**2).simplify())
r
e2x
+ ex + 1
4
integrate(y*sqrt((1+diff(y,x)**2).simplify()), (x,0,1))
R1 R1
e x dx 2 dx
0 0
+
2 2
In situations like above, when sympy returns the answer in the form of an integral, the
.doit function can help obtain a final answer.
integrate(y*sqrt((1+diff(y,x)**2).simplify()), (x,0,1)).doit()
1 e
+
2 2
Multiply the final answer by 2π
S = 2*pi*integrate(y*sqrt((1+diff(y,x)**2).simplify()), (x,0,1)).doit()
S
1 e
2π +
2 2
Exercise
Solve Ex.41, Ex.44, Ex.49, and Ex.50 in Section 7.4.
70
Week 8
IBP
d
Z
u( x )v( x ) − v( x ) u( x ) dx
dx
8.1 Example 1
(Calculus Early Transcendentals, James Stewart Section 7.1, Question number 24.)
Evaluate the integral
R1 2 −x
0 ( x + 1) e dx
Solution
The Python algorithm for symbolic integration can directly evaluate the given integral.
However, if necessary the integration by parts steps can be used as in the formula above.
Below, the integral is evaluated directly, and Python is used to take off the integration by
parts.
integrate((x**2+1)*exp(-x), (x,0,1))
6
3−
e
71
Now use the integration-by-part step to demonstrate how the integral can be simplified
using this technique.
u = (x**2+1); dv = exp(-x)
# Next different u to get du and integrate dv to get v
du = diff(u); v = integrate(exp(-x))
res
Z1
2
− − −2xe−x dx + 1
e
0
It is clear that the resulting integral above again requires another integration by parts.
However, the objective here is to show how the integration by parts steps can be achieved.
Now we will just evaluate the integral on the right-hand side of the variable res using the
.doit() command.
res
6
3−
e
Clearly, the outcome is the same as before.
8.2 Example 2
x tan−1 xdx
R
Evaluate
Solution
Here use the integrate command and let the computer take care of the integration by part
steps.
x= Symbol("x", real=True)
integrate(x*atan(x))
x2 atan ( x ) x atan ( x )
− +
2 2 2
72
8.3 Example 3
(Calculus Early Transcendentals, James Stewart Section 7.1, Question number 29)
Rπ
Evaluate the integral 0 x sin x cos x dx
Solution
See the indefinite integral below to be sure that the integration was carried out as expected
integrate(x*sin(x)*cos(x))
integrate(x*sin(x)*cos(x), (x,0,pi))
π
−
4
8.4 Example 4
Evaluate the integral (sin−1 x )2 dx
R
Solution
integrate(asin(x)**2,x)
p
x asin2 ( x ) − 2x + 2 1 − x2 asin ( x )
8.5 Example 5
x3 (ln x )2 dx
R
Evaluate the integral
Solution
integrate(x**3 * ln(x)**2,x)
x4 log ( x )2 x4 log ( x ) x4
− +
4 8 32
Remark: To verify the above answer, we can differentiate the antiderivative above to see
that we get the integrand back:
v=integrate(x**3 * ln(x)**2,x)
73
diff(v,x)
x3 log ( x )2
Exercises 1
Solve Ex.24, Ex.26, Ex.33, Ex.70, and Ex.92 in Section 8.2.
8.6 Example 6
(Calculus Early Transcendentals, James Stewart Section 7.2, Question number 6)
Evaluate the integral
t cos5 (t2 )
R
Solution
In this example, the solution is first obtained by letting python do the integration and
take care of all the difficulties. However, you might observe a little delay in the execution
(depends on the speed of your system, of course)
Another approach is to first do a substitution to simplify the integral before carrying out
the integration. The integral is evaluated in a shorter time with this approach and the
resulting solution has a simpler form.
The code for the evaluating the integral directly is shown below
integrate(t*(cos(t**2))**5)
I = Integral(t*(cos(t**2))**5)
74
Z
t cos5 t2 dt
x = Symbol("x", real=True)
I2 = I.transform(t**2,x)
I2
cos5 ( x )
Z
dx
2
I3 = I2.doit() # Evaluate the transformed integral I2.
I3
8.7 Example 7
(Calculus Early Transcendentals, James Stewart Section 7.2, Question number 9)
Rπ
Evaluate the integral 0 cos4 (2t)dt
Solution
3π
8
8.8 Example 8
R
Evaluate the integral x sin x cos 3xdx
Solution
This is an example of integration where two techniques need to be applied: integration by
parts, and trigonometric integration. However, using the Python we get different answers
according to the command used:
3x sin ( x ) sin (3x ) x cos ( x ) cos (3x ) 5 sin ( x ) cos (3x ) 3 sin (3x ) cos ( x )
+ + −
8 8 32 32
75
Now, with simplify():
8.9 Example 9
(Calculus Early Transcendentals, James Stewart Section 7.2, Question number 38)
Evaluate the integral t csc4 t cot4 t dt
R
Solution
An attempt to integrate the above function directly might prove difficult do to its com-
plexity. See the integrate function below returns an integral object, rather than the desired
answer. And the command takes a while to evaluate
Another option here is to first attempt to simplify the problem using integration by parts
integrate(t*csc(t)**4*cot(t)**4)
Z
t cot4 (t) csc4 (t) dt
It is observed above that the integral was not evaluated. This is possibly due to the com-
plexity of the problem.
The approach below is to simplify the problem. First treat as an integration by parts
problem, taking u = t and dv = cot4 (t) csc4 (t) dt
The trigintegrate function is introduced below to provide an awareness of the function. It
has some useful functionalities for evaluating integral of trigonometric functions
u = t; dv = csc(t)**4*cot(t)**4
du = diff(u,t); v = trigintegrate(csc(t)**4*cot(t)**4, t)
76
IBP
Z
2 cos (t) cos (t) 8 cos (t) cos (t) 2 cos (t) cos (t)
t − − + − − − −
35 sin (t) 35 sin3 (t) 35 sin5 (t) 7 sin7 (t) 35 sin (t) 35 sin3 (t)
8 cos (t) cos (t)
+ − dt
35 sin (t) 7 sin7 (t)
5
The outcome above is the result of the integration by parts step and it still has an integral
on the right hand side, to be evaluated. Use the .doit command to evaluate the integral
res
2 cos (t) cos (t) 8 cos (t) cos (t) 2 log (sin (t)) 1
t − − 3
+ 5
− 7
+ − +
35 sin (t) 35 sin (t) 35 sin (t) 7 sin (t) 35 70 sin2 (t)
2 1
−
35 sin (t) 42 sin6 (t)
4
8.10 Example 10
π
x tan x sec2 xdx
R 4
Evaluate the integral 0
Solution
0.285398163397448
Exercise
Solve Ex.10, Ex.26, Ex.62, and Ex.74 in Section 8.3.
77
Week 9
Solution
I = Integral(sqrt(x**2 - 9)/x**3)
res = I.doit()
res
i acosh ( 3x )
+ q i − q9i for 9
>1
6 2x −1+ 92 2x3 −1+ 9 | x2 |
q x x2
3 1− 92
− asin ( x ) −
x
6 2x otherwise
Since we only deal with the real numbers in this course, we can easily assume that x2 −
9 ≥ 0.
So the solution we desire is the second part of the answer. It is possible to extract it by
using the .args command
res.args[1][0]
q
9
3 1−
asin x x2
− −
6 2x
78
If the output is a piecewise function (or object in general) of the form
expression 0, condition 0
expression 1, condition 1
... ..
.
then one can call a specific expression or condition by typing .args[n][m] where n
and m are the row and column number respectively.
9.2 Example 2
(Calculus Early Transcendentals, James Stewart Section 7.3, Question number 14)
Z 1
dx
Evaluate the integral dx
0 ( x2 + 1)2
Solution
integrate(1/((x**2 + 1)**2))
x atan ( x )
+
2x2 + 2 2
integrate(1/((x**2 + 1)**2), (x,0,1))
1 π
+
4 8
9.3 Example 3
(Calculus Early Transcendentals, James Stewart Section 7.3, Question number 22)
R1√
Evaluate the integral 0 x2 + 1 dx
Solution
integrate(sqrt(x**2 + 1), x)
√
x x2 + 1 asinh ( x )
+
2 2
integrate(sqrt(x**2 + 1), (x,0,1))
√ √
log 1 + 2 2
+
2 2
79
9.4 Example 4
Evaluate the integral
x dx
Z
√
6x − x2 − 5
Solution
g=x/sqrt(6*x-x**2-5)
integrate(g,x)
x
Z
p dx
− ( x − 5) ( x − 1)
We notice here, that due to some limitations, Sympy did not return the integral of the
function. One possible way is to do the following. First, we complete the square: 6x −
x2 − 5 = 4 − ( x − 3)2 . So the appropriate substitution is x − 3 = 2 sin t, thus we do it in
two stages
t+3
Z
√ dt
4 − t2
i2.transform(t, 2*sin(t)) # Now we replace every t by 2*sin(t)
i3
80
As we see above that the result is a function of t. One way to substitute back to x is to use
subs, notice that when x − 3 = 2 sin(t) then t = sin−1 ( 2x − 32 )
i4
x 3
q
2
− 4 − ( x − 3) + 3 asin −
2 2
Exercises 1
Solve Ex.22, Ex.41, Ex.69 in Section 8.4 and evaluate the following integrals
Z r Z 1
x−4 4 2 dt
dx and √
x 1
12 t(1 + 4t)
9.5 Example 5
Evaluate the integral
x2 + x + 2
Z
dx
x2 + 2x + 3
Solution
Rational functions are generally integrated by partial fractions. Since the degree of the nu-
merator is equal to the degree of the denominator, the long division will be implemented
first.
log x2 + 2x + 3
x−
2
9.6 Example 6
Evaluate the integral
1 − x + 2x2 − x3
Z
dx
x ( x 2 + 1)2
81
Solution
A direct application of the integration command gives:
integrate((1-x+2*x**2-x**3)/(x*(x**2+1)**2),x)
log x2 + 1
1
log ( x ) − − atan ( x ) − 2
2 2x + 2
9.7 Example 7
Evaluate the integral
ex − 1
Z
dx
e3x + 1
Solution
integrate((exp(x)-1)/(exp(3*x)+1),x).simplify()
√ ! √ !
2 log (e x + 1) 1 3i 1 3i
−x + + − log e x − − +
3 6 6 2 2
√ ! √ !
1 3i 1 3i
+ log e x − +
6 6 2 2
Clearly, Python used some integration methods that led to the use of complex numbers.
However, using aRsimple substitution we can get around this issue: Let t = e x , this turns
the integral into: t(tt3−+11) dt
t=symbols('t',real=True)
integrate((exp(x)-1)/(exp(3*x)+1),x)
2 log (e x + 1)
2 x
−x + + RootSum 9z − 3z + 1, (i 7→ i log (3i + e − 1))
3
Clearly, Sympy used some integration methods that led to the use of complex numbers.
However, using a simple substitution we can get around this issue: Let t = e x and the
integral becomes
i = Integral((exp(x)-1)/(exp(3*x)+1),x)
i
i.transform(exp(x),t)
t−1
Z
dt
t ( t3 + 1)
We can force Sympy to evaluate it by adding the command .doit() at the end
82
i.transform(exp(x),t).doit()
√ √ √
2 3t 3
2 log (t + 1) log t2
−t+1 3 atan 3 − 3
− log (t) + + +
3 6 3
Exercises 2
Solve Ex.23, Ex.27, Ex.30, and Ex.48 in Section 8.5
83
Week 10
Solution
diff(asin((2*x)/(x**2+1))).simplify()
2 − 2x2
√
( x2 + 1) x4 − 2x2 + 1
(Yet another limitation of Python, even though we used “simplify()”, it did not recognize
that x4 − 2x2 + 1 = ( x2 − 1)2 . More coding is needed for Python to do that.) Hence, after
simplification, du = x− 2
2 +1 . Now, by integration by parts, we get that the integral is equal
diff(x*asin((2*x)/(x**2+1))+ ln(x**2+1)).simplify()
q q
2 2
2x −4x + ( x + 1) − 2x x − 1 + −4x2 + ( x2 + 1) x2 + 1 asin x22x+1
2
2 2
q
−4x2 + ( x2 + 1)2 ( x2 + 1)
−1 2x
which is clearly equal to sin x 2 +1
after noticing that, in the numerator, the first two
terms cancel.
Exercises
Solve Ex.34, Ex.69, and Ex.73 in Section 8.7
84
10.2 Example 2
Find whether the following improper integral is convergent or divergent. Find the value
if it is convergent:
Z ∞
sin x dx
0
Solution
⟨0, 2⟩
Since the output is an interval, we understand that the integral is divergent.
10.3 Example 3
Find whether the following improper integral is convergent or divergent. Find the value
if it is convergent:
Z ∞
dx
2 x ln x
Solution
f=1/(x*ln(x))
integrate(f,(x,2,oo))
∞
So, the improper integral is divergent.
10.4 Example 4
Find whether the following improper integral is convergent or divergent. Find the value
if it is convergent:
Z ∞
tan−1 x
0 1 + x2
85
Solution
f=atan(x)/(1+x**2)
integrate(f,(x,0,oo))
π2
8
π2
Consequently, the integral is convergent and equal to 8 .
Exercises 2
Solve Ex.24, Ex.29, Ex.41, Ex.46, and Ex.88 in Section 8.8.
86
Week 11
Solution
We will use here a f or loop to print out the values of an :
for n in range(1,5):
an=(1-n)/(n*n)
print("n=",n)
print(" an = ",an)
n= 1
an = 0.0
n= 2
an = -0.25
n= 3
an = -0.2222222222222222
n= 4
an = -0.1875
11.2 Example 2
1−2n
Given the sequence an = 1+2n2
, find if the sequence converges or diverges.
Solution
We need to find the limit when n goes to infinity of an ., to do so we use the limit function
from sympy environment as well as the infinity oo (double o).
an=(1-2*n)/(1+2*n**2)
an
87
1 − 2n
2n2 + 1
L=limit(an, n, oo)
L
11.3 Example 3
72
The sequence a1 = 2, an+1 = 1+ an converges. What is its limit L?
Solution
72
The limit L satisfies: 1+ L = L; Therefore, we need to solve L2 + L − 72 = 0.
To do so, we need solve() function from sympy environment.
L = Symbol('L')
d=L**2 +L-72
d
L2 + L − 72
s=solve(d)
s
[-9, 8]
Exercises 1
Solve Ex.9, Ex.11, Ex.27, Ex.28, and Ex.35 in Section 9.1.
11.4 Example 4
Given the series 2 + 23 + 92 + + ... + 2
3n −1
+ ..., find its sum S?
Solution
The series is geometric with a = 2 and r = 13 . (Check)
1
Since |r | = 3 < 1, the series is convergent and its sum can be obtained from the formula
a
s=
1−r
88
from sympy import *
a = 2
r = Rational(1,3)
s = a/(1-r)
s
3
Therefore, S = 3.
11.5 Example 5
∞
4
Find the sum of the series: ∑ (4n − 3)(4n + 1)
.
k =1
Solution
One can observe that the series can be written in telescoping form. To do this, we use the
function apart( ) from sympy that performs partial fraction decomposition on a rational
function
an = 4/((4*n-3)*(4*n+1))
an
4
(4n − 3) (4n + 1)
an1 = apart(an)
an1
1 1
− +
4n + 1 4n − 3
n
As you have seen from the lecture, many terms of the partial sum Sn = ∑ ak cancel each
k =1
other for instance
1 1 C1 C1 1 Z 1 1 1
Sn = (1 − ) + ( − C ) + ( C − ) + ... + ( ZZ − ) = 1− .
5 5 9C 9C 13 4n − Z
3 4n + 1 4n + 1
This can be done in sympy by using sum(expression, (variable, first number, last number)).doit
but first we have to change the variable in an to ak as follow
89
k = Symbol("k")
ak=an.subs(n,k)
Sum(ak,(k,1,n)).doit()
4n
4n + 1
Now we can evaluate the limit of the nth partial sum
Sn = Sum(ak,(k,1,n)).doit()
limit(Sn,n,oo)
1
Therefore the series converges and its sum is 1.
Exercises 2
Solve Ex.25, Ex.35, Ex.38, Ex.48, and Ex.85. in Section 9.2.
90
Week 12
Solution
In order to apply the integral test, the function f ( x ) = x12 must be positive, continu-
ous and decreasing on the interval [1, ∞). We will do it step by step. First, we import
solve_univariate_inequality from sympy.solvers.inequalities to find all possible
values for x such that the function f is positive.
1
x2
from sympy.solvers.inequalities import solve_univariate_inequality
solve_univariate_inequality(f >= 0, x)
x ̸= 0
which means that the function is positive for all real values x except zero. This includes
the interval [1, ∞).
Next, we check the continuity of the function by calling continuous_domain from
sympy.calculus.util which gives the domain where the function is continuous.
(−∞, 0) ∪ (0, ∞)
From the output we conclude that the function is continuous on [1, ∞).
For the decreasing condition, we use again solve_univariate_inequality but for the
derivative of f . According to the first derivative test, the function decreases on the inter-
val where its derivative is negative.
91
from sympy.solvers.inequalities import solve_univariate_inequality
solve_univariate_inequality(diff(f,x) < 0, x)
0<x
which means that the function is decreasing on the interval (0, ∞) that includes [1, ∞).
Since all the three conditions are satisfied, we can use the integral test on the series.
integrate(f, (x,1,oo))
1
The integral converges to 1. Therefore, the series converges.
12.2 Example 2
Use the integral test to determine if the series converges or diverges: Σ∞ 1
n =1 n n .
√
Solution
The function f ( x ) = is positive, continuous and decreasing on [1, ∞). (Check!)
1
√
x x
R∞ 1
Therefore, we can use the integral test on 1 x√ x
dx.
integrate(1/(x*sqrt(x)), (x,1,oo))
2
The integral converges to 2. Therefore, the series converges.
integrate(1/x*sqrt(x), (x,1,oo))
We will get
∞
which is a wrong answer. Can you tell where is the mistake?
Exercises 1
Solve Ex.13, Ex.15, Ex.20, Ex.21, and Ex.57 in Section 9.3.
92
12.3 Example 3
Use the limit comparison test to determine if the series converges or diverges
∞
1
∑ n2 + 3
n =1
Solution
The series Σ∞ 1
n=1 n2 converges as a p-series with p = 2 > 1. Both series have positive terms,
therefore we can use the limit comparison test. We can use the limit when n goes to
infinity of bann , with an = n21+3 and bn = n12 .
an = 1/(n**2 +3)
an
1
n2 +3
bn = 1/n**2
bn
1
n2
R=an/bn
R
n2
n2 + 3
L=limit(R,n,oo)
L
1
The limit is 1 > 0. Therefore, both series converge.
12.4 Example 4
Use the limit comparison test to determine if the series converges or diverges
∞
√
2 n+1
∑ n+2
n =1
93
Solution
The series Σ∞ √1 1
n=1 n diverges as a p-series with p = 2 < 1.Both series have positive terms,
therefore we can use √
the limit comparison test.We can use the limit when n goes to infinity
n +1
of bann , with an = 2 n+ 2 and bn = n .
√1
an = 2*sqrt(n+1)/(n+2)
an
√
2 n+1
n+2
bn = 1/sqrt(n)
bn
1
√
n
R=an/bn
R
√ √
2 n n+1
n+2
L=limit(R,n,oo)
L
2
The limit is 2 > 0. Therefore, both series diverge.
Exercises 2
Solve Ex.23, Ex.26, and Ex.49 in Section 9.4.
94
Week 13
13 Week 13: Alternating Series & Ratio and Root Tests (9.5
& 9.6)
Exercises 1
Read section 9.5 in your textbook about Alternating Series Test, Alternating Series Re-
mainder, and Absolute Convergence Theorems then solve Ex.13, Ex.27, Ex.33, Ex.47, and
Ex.58 in Section 9.5.
13.1 Example 1
Use the Ratio test to determine if the series converges or diverges
∞
2n
∑ n!
n =1
Solution
a n +1
The Ratio Test involves in evaluating the limit lim
n→∞ an
an = 2**n / factorial(n)
an
2n
n!
Now we want to find an+1
an1=an.subs(n,n+1)
an1
2n +1
( n + 1) !
a n +1
Next we find the ratio
an
R = abs(an1/an)
R
95
2−n 2n+1 n!
( n + 1) !
We see a simplification can done to the output, hence we assign the simplified output to
bn
bn = R.simplify()
bn
2
n+1
a n +1
Finally we evaluate the limit limn→∞
an
limit(bn,n,oo)
0
The limit is 0 < 1. Therefore, the series converges.
13.2 Example 2
Use the Root test to determine if the series converges or diverges
∞
nn
∑ n2
n =1 3
Solution
nn
p
n
The Ratio Test is about the limit when n goes to infinity of | an |, with an = 2.
3n
an = n**n / 3**(n**2)
an
2
3− n n n
To apply the nth root we use root( , ) from sympy
root(abs(an),n)
2
1
n
3− n n n
bn = root(abs(an),n).simplify()
bn
96
3− n n
√
n
Now we evaluate the limit lim an
n→∞
limit(bn, n, oo)
0
The limit is 0 < 1. Therefore, the series converges.
Exercises 2
Solve Ex.28, Ex.36, Ex.44, Ex.48, and Ex.68 in Section 9.6.
97
Week 14
Solution
We can use either the Ratio Test or Root Test. Here we use Ratio Test but first, we define
the series using sum( , ) that comes from sympy
7− n x n
n4
an1 = an.subs(n,n+1)
an1
7− n −1 x n +1
( n + 1)4
R = abs(an1/an)
R
7n 7− n −1 n 4 | x | n +1 | x − n |
( n + 1)4
98
bn = R.simplify()
bn
n4 | x |
7 ( n + 1)4
l = limit(bn,n,oo)
l
|x|
7
|x|
We know that the power series converges if 7 < 1. To solve this inequality and obtain
the intervals, we use the command solve_univariate_inequality
−7 < x ∧ x < 7
which it reads the interval (−7, 7). We need to check the endpoints. When x = −7 we see
the series becomes
s.subs(x,-7).simplify()
∞
(−7)n 7−n
∑ n4
n =1
We know, from the Alternating Series Test, that this series is convergent.
Now when x = 7 we have the series
s.subs(x,7).simplify()
∞
1
∑ 4
n =1 n
99
There is the function .is_convergent that can check the convergence of a series.
∞
1
For example, is we used it to check the series in the previous example ∑ 4 we get
n =1 n
b2.is_convergent()
True
which means that the series is convergent. However, this command may run into
errors for some cases like the series s.subs(x,-7) in the previous example. (Try it).
Another function worth knowing is .is_absolutely_convergent() which is clear
to what it does
Exercises 1
Solve Ex.19, Ex.22, Ex.29, Ex.34, and Ex.70 in Section 9.8.
14.2 Example 2
Find the Maclaurin series for the functions f ( x ) = e x and g( x ) = sin( x ).
Solution
We can easily have the Maclaurin series (even Taylor series) for a function by calling the
command fps. The output formula may look complicated but one can work with the
starting terms.
x = Symbol('x', real=True)
fps(exp(x))
∞
( k !
x
for k mod 1 = 0
∑ 0k! otherwise +1
k =1
x2 x3 x4 x5 x6 x7 x8 x9
1+x+ + + + + + + + + O x10
2 6 24 120 720 5040 40320 362880
For the function g( x ) = sin( x ) we have
100
fps(sin(x))
k−1
∞ (− 14 ) 2 2 xk
for k mod 2 = 1
x + ∑ k−1
( 3 )( 2 2 ) ( k − 1 )!
2 2 2
k =2
0 otherwise
fps(sin(x)).truncate(10)
x3 x5 x7 x9
x− + − + + O x10
6 120 5040 362880
The command .series() also calculates the series expansion for a given function for
example
f = exp(x)
f
ex
x2 x3 x4 x5 x6 x7 x8 x9
1+x+ + + + + + + + + O x10
2 6 24 120 720 5040 40320 362880
14.3 Example 3
Find the Taylor series for the functions f ( x ) = ln( x ) centered at a = 1.
Solution
We can use either fps or .series() but with specifying the center as follow
x = Symbol('x', real=True)
f = ln(x)
f
log ( x )
101
f.series(x,1,10) # In the second entry of .series( , , ) we put 1␣
,→as it is a center
Exercises 2
Solve Ex.7, Ex.24, Ex.30, Ex.39, Ex.51, Ex.55, and Ex. 69 in Section 9.10.
102