This notebook shows how to use python-based computer algebra to solve some of the Midterm 2 exam problems. The content of this notebook is not examinable material for this course, but you may find it useful to adapt the techniques shown here to double-check homework or do symbolic manipulations when solving problems you encounter elsewhere.

We are using the sympy package which is part of the Anaconda distribution. As with pylab, you need to load this extension into python. This is done in the following two lines, which initialize your sympy session.

In [1]:
from sympy import init_session
init_session()
IPython console for SymPy 0.7.6 (Python 2.7.8-64-bit) (ground types: gmpy)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at http://www.sympy.org

In order to manipulate mathematical symbols in python, you need to assign a python variable to each symbol you use. When initializing sympy as above, a few such symbols are already assigned as shown in the command output. If you need to use more symbols, just follow the same pattern.

Midterm Question 2(a):

In [2]:
diff((1+x)**2,x)
Out[2]:
$$2 x + 2$$

Midterm Question 2(b):

In [3]:
a, b, c = symbols('a b c')
f = a*x / (b*x+c)
simplify(diff(f,x))
Out[3]:
$$\frac{a c}{\left(b x + c\right)^{2}}$$

Midterm Question 2(c):

In [4]:
simplify(diff(x**2 * exp(-x),x))
Out[4]:
$$x \left(- x + 2\right) e^{- x}$$

Midterm Question 3: (Note that the computation shown here only verifies the final result. It does not set up a new special function with a given derivative. This is also possible in sympy, but requires a deeper understanding of the sympy internals.)

In [5]:
simplify(diff(atan(1/x**4),x))
Out[5]:
$$- \frac{4 x^{3}}{x^{8} + 1}$$

Midterm Question 4:

We first set up the difference quotient. On the expression f we use the subs method: f.subs(a,b) substitutes b for a in expression f.

In [6]:
h = symbols('h')
f = 1/x
d = (f.subs(x,x+h)-f)/h
d
Out[6]:
$$\frac{1}{h} \left(\frac{1}{h + x} - \frac{1}{x}\right)$$

And now we compute the derivative as the limit of the diffence quotient:

In [7]:
limit(d,h,0)
Out[7]:
$$- \frac{1}{x^{2}}$$

Of course we can also compute the derivative directly by using the diff function, the result is the same:

In [8]:
diff(f,x)
Out[8]:
$$- \frac{1}{x^{2}}$$

Midterm Question 4:

Let us first set up the function f and the location x=a at which we want to compute the tangent line.

In [9]:
r = symbols('r')
f = log(1+r*x)
a = 0

Next we compute the slope m of the tangent line.

In [10]:
m = diff(f,x).subs(x,a)

Finally, we need to find the y-intercept b so that the tangent line matches f at x=a:

In [11]:
b = f.subs(x,a) - m*a

We can now put it all together to find the equation of the line:

In [12]:
m*x+b
Out[12]:
$$r x$$

Midterm Question 6:

In [13]:
f = 1/(2*x) + log(2*x)

Clearly, f is only defined for positive x. x=0 is a candidate for the vertical asymptote. Let's test this:

In [14]:
limit(f,x,0)
Out[14]:
$$\infty$$

So x=0 is a vertical asymptote. To see if there is a horizontal asymptote as x goes to infinity, we compute

In [15]:
limit(f,x,oo)
Out[15]:
$$\infty$$

So there is no horizontal asymptote. To find the critical points, we find the first derivative and compute its roots:

In [16]:
df = simplify(diff(f,x))
df
Out[16]:
$$\frac{1}{x^{2}} \left(x - \frac{1}{2}\right)$$
In [17]:
solve(df,x)
Out[17]:
$$\left [ \frac{1}{2}\right ]$$

So x=1/2 is a critical point, we clearly see that f is changing sign from negative to positive at this point. So f has a local minimum at x=1/2.

If you don't see which way the sign is changing, you can brute-force test for it:

In [18]:
df.subs(x,1/4), df.subs(x,2)
Out[18]:
$$\left ( -4.0, \quad \frac{3}{8}\right )$$

Let's now look for critical points of the derivative of f:

In [19]:
ddf = simplify(diff(f,x,2))
ddf
Out[19]:
$$\frac{1}{x^{3}} \left(- x + 1\right)$$

Clearly, the second derivative is changing sign from positive to negative at x=1. So f is concave up on the left, has a point of inflection at x=1, and is concave down to the right.

Finally, we can plot the function to visually verify what we have found:

In [20]:
plot(f,(x,0.1,3))
Out[20]:
<sympy.plotting.plot.Plot at 0x7f1f6600fd90>

Note that this is the sympy plot function which works directly with symbolic expressions while the pylab plot function does not.