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.
from sympy import init_session
init_session()
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):
diff((1+x)**2,x)
Midterm Question 2(b):
a, b, c = symbols('a b c')
f = a*x / (b*x+c)
simplify(diff(f,x))
Midterm Question 2(c):
simplify(diff(x**2 * exp(-x),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.)
simplify(diff(atan(1/x**4),x))
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.
h = symbols('h')
f = 1/x
d = (f.subs(x,x+h)-f)/h
d
And now we compute the derivative as the limit of the diffence quotient:
limit(d,h,0)
Of course we can also compute the derivative directly by using the diff function, the result is the same:
diff(f,x)
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.
r = symbols('r')
f = log(1+r*x)
a = 0
Next we compute the slope m of the tangent line.
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:
b = f.subs(x,a) - m*a
We can now put it all together to find the equation of the line:
m*x+b
Midterm Question 6:
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:
limit(f,x,0)
So x=0 is a vertical asymptote. To see if there is a horizontal asymptote as x goes to infinity, we compute
limit(f,x,oo)
So there is no horizontal asymptote. To find the critical points, we find the first derivative and compute its roots:
df = simplify(diff(f,x))
df
solve(df,x)
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:
df.subs(x,1/4), df.subs(x,2)
Let's now look for critical points of the derivative of f:
ddf = simplify(diff(f,x,2))
ddf
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:
plot(f,(x,0.1,3))
Note that this is the sympy plot function which works directly with symbolic expressions while the pylab plot function does not.