Lab 2

Published

May 19, 2024

Goals for today

Just like our first session, the idea of these labs is for you to learn enough so that you can use SymPy to help you out when checking answers if you work on examples and exercises in our book.

Our goals for today are:

  1. Wrap up Lab 1. Reload the webpage to get an updated version.
  2. As always, each student works through the exercise and picks up the skills needed to implement what you saw in the lecture in the computer.
  3. If you need to look up new commands, go to the documentation.
  4. A self-evaluation form will be available about 15 minutes before the end of the class.

Continuing other tasks in Section 8.2

Consider Example 3.

  1. Calculate the second derivative of \(f(x)=e^{x-1}-1\).
  2. Use reduce_inequalities() to check if \(f\) is convex or concave.

Working with unspecified functions

Consider Section 8.3 Example 1. Notice that the function \(Y(N)\) was left unspecified. How do we obtain (*) in the beginning of that example?

Set up the problem first.

P, N, q = symbols('P N q', positive = True, real = True)
Y = Function('Y')(N)
pi = Function('pi')(N)
pi = P*Y-q*N
pi

\(\displaystyle - N q + P Y{\left(N \right)}\)

Calculate the first derivative.

Derivative(pi, N).doit()

\(\displaystyle P \frac{d}{d N} Y{\left(N \right)} - q\)

Calculate the second derivative.

Derivative(pi, (N, 2)).doit()

\(\displaystyle P \frac{d^{2}}{d N^{2}} Y{\left(N \right)}\)

Next, consider Section 8.3, Example 3.

Set up the problem in SymPy and try to obtain the form of the first-order condition found in (*). You should be able to get something like what you see below and it reproduces what you see in (a):

\(\displaystyle Q \frac{d}{d Q} P{\left(Q \right)} - k + P{\left(Q \right)}\)

Next, you are going to do implicit differentiation to reproduce the results in (a).

idiff(Derivative(pi, Q).doit(), Q, k)

\(\displaystyle \frac{1}{Q \frac{d^{2}}{d Q^{2}} P{\left(Q \right)} + 2 \frac{d}{d Q} P{\left(Q \right)}}\)

Finally, to reproduce (c), we need to change our definitions to explicitly account for the fact that the optimal \(Q\) depends on \(k\).

Q = Function('Q')(k)
P = Function('P')(Q)
pi = Function('pi')(Q)
pi = P*Q-k*Q
Derivative(pi, k).doit().factor()

\(\displaystyle - k \frac{d}{d k} Q{\left(k \right)} + P{\left(Q{\left(k \right)} \right)} \frac{d}{d k} Q{\left(k \right)} + Q{\left(k \right)} \frac{d}{d Q{\left(k \right)}} P{\left(Q{\left(k \right)} \right)} \frac{d}{d k} Q{\left(k \right)} - Q{\left(k \right)}\)

Of course, you need to do the remaining work to really complete the analysis.1

Now, it is your turn. Do Section 8.3 Exercise 1.

Applying the algorithm (1) on page 271

Apply the algorithm to these functions.

  1. Section 8.4, Exercise 5: \(f(x)=(\ln x)^3-2(\ln x)^2+\ln x\) on \(x\in[1,e^3]\)

Run each command line by line, otherwise some of the output won’t show up. Try to make sense of what is happening for every line.

expr = (log(x))**3-2*(log(x))**2+log(x)
points = solve(Derivative(expr, x).doit(), x)
points 
points.extend([1, exp(3)])
expr.subs({x:points[0]})
expr.subs({x:points[1]})
expr.subs({x:points[2]})
expr.subs({x:points[3]})
# Instead of the previous four lines
[expr.subs({x:points[n]}) for n in range(4)]
  1. Section 8.4, Example 2: \(\displaystyle f(x)=\frac{x^{4}}{4} - \frac{5 x^{3}}{6} + \frac{x^{2}}{2} - 1\) on \(x\in[-1,3]\)

Modify the code used previously and apply the algorithm to find maxima and minima.

Try two versions of the code – with and without S(). This command was used before to deliberately leave fractions intact.

  1. Section 8.4: \(\displaystyle f(x)=x^{26} -32x^{23}-11x^5-2x^3-x+28\) on \(x\in [-1,5]\)

Modify the code used previously and apply the algorithm to find maxima and minima.

What happens when you look for stationary points?

Instead of using solve(), use nroots() and modify your code accordingly.

Footnotes

  1. I am not very sure whether SymPy could do more after, but this would be interesting to pursue. For our purposes, what we have is good enough. If you have ideas, please do reach out to me.↩︎