11-13 math-156 lab

1833 days ago by beckstranda0808

Sage Homework: Derivatives and Graphs

In class, we have outlined a procedure for finding pertinent information about the graph of a function by using Calculus.  The "problem" with this is of course the fact that the algebraic aspects can be challenging for a complicated function.  Here is an example of how we can use computer to help us with the mathematics.

Example 1: Consider the function  f(x) = 3x^5-30x^4+70x^3+60x^2-225x+5.  Our goal is to get a decent graph of this function.  Let's just see what Sage gives us on a standard window.

f(x) = 6*x^5-15*x^4-830*x^3+1260*x^2+25920*x+15 plot(f, (x, -20, 20)) 
       

That's not very enlightening, is it?  Let's see if the graph has any bumps?

So, let's compute the derivative:

fp(x) = derivative(f, x) show(fp) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 30 \, x^{4} - 60 \, x^{3} - 2490 \, x^{2} + 2520 \, x + 25920
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 30 \, x^{4} - 60 \, x^{3} - 2490 \, x^{2} + 2520 \, x + 25920

Now we can try to see when this is zero.  Note that the x in the following line tells Sage what to solve for and the double equal sign == is what Sage uses to test equality. The single equals sign = is used for assignment.

solve(fp(x) == 0, x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(-3\right), x = 9, x = \left(-8\right), x = 4\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(-3\right), x = 9, x = \left(-8\right), x = 4\right]

In this case, Sage gives us exact answers.  Indeed, it's possible to factor this.

factor(fp) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}30 \, {\left(x - 9\right)} {\left(x - 4\right)} {\left(x + 3\right)} {\left(x + 8\right)}
\newcommand{\Bold}[1]{\mathbf{#1}}30 \, {\left(x - 9\right)} {\left(x - 4\right)} {\left(x + 3\right)} {\left(x + 8\right)}

Task 1: Draw the chart of increasing/decreasing for f(x).

We can easily use Sage to compute test values.  For example, if we want to compute the derivative when x = 0, we simply execute the following:

print fp(-10) print fp(-5) print fp(0) print fp(5) print fp(10) 
       
111720
-22680
25920
-12480
42120
111720
-22680
25920
-12480
42120

You should see that the graph of f(x) increases, then decreases, then increases, then decreases, then increases. Correct? So, we have relative minima at x = 1 and x = 5.  And relative maxima at x = -1 and 3.  We can use Sage to get the function values:

print f(-8) print f(-3) print f(4) print f(9) 
       
40207
-46668
73039
-13836
40207
-46668
73039
-13836

Now, how about concavity?  We need the second derivative.  We compute that with the following command:

fpp = derivative(fp, x) show(fpp) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 120 \, x^{3} - 180 \, x^{2} - 4980 \, x + 2520
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 120 \, x^{3} - 180 \, x^{2} - 4980 \, x + 2520

Again, let's try finding the zeros.

solve(fpp(x) == 0, x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = 7, x = \left(\frac{1}{2}\right), x = \left(-6\right)\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = 7, x = \left(\frac{1}{2}\right), x = \left(-6\right)\right]
print N(-10) print N(0) print N(3) print N(10) 
       
-10.0000000000000
0.000000000000000
3.00000000000000
10.0000000000000
-10.0000000000000
0.000000000000000
3.00000000000000
10.0000000000000
print fpp(-10) print fpp(0) print fpp(3) print fpp(10) 
       
-85680
2520
-10800
54720
-85680
2520
-10800
54720

Again we get exact answers.

Task 2: Draw the chart of concavity.  You should see that the graph is concave down, then concave up, then down, and finally up again.  So x = 2-\sqrt(5), x = 2, x = 2+\sqrt(5) are all inflection points.  Again, let's evaluate them.

This is an exact value, but we can have Sage give us an approximate value by using the numerical evaluation function N:

If we want more accuracy, we can use:

print N(f(-6)) print N(f(0.5)) print N(f(7)) 
       
3039.00000000000
13185.5000000000
23332.0000000000
3039.00000000000
13185.5000000000
23332.0000000000

With all of this information, we see that the "interesting" stuff happens between x = -1 and x = 5 with y-values between -245 and 187. So, let's try graphing the function with a slightly larger window:

plot(f(x), (x, -6, 7), ymin=3039, ymax=23332) 
       

Indeed, we see all the interesting behavior of the graph.

We can even add a few points to the graph to highlight all the interesting points.

p = plot(f(x), (x, -6, 7), ymin=3039, ymax=23332); p += point( [(3039)], color='red'); p += point( [(-8, 40207)], color='red'); p += point( [(4, 73039)], color='red'); p += point( [(3,-46668)], color='green'); p += point( [(9, -13836)], color='green'); p += point( [(-6, 3039)], color='black'); p += point( [(.5, 13185.5)], color='black'); p += point( [(7, 23332)], color='black'); show(p); 
       
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.rings.integer.Integer' object does not support indexing
Traceback (most recent call last):    p += point( [(-6, 3039)], color='black');
  File "", line 1, in <module>
    
  File "/tmp/tmpiasO5Y/___code___.py", line 4, in <module>
    p += point( [(_sage_const_3039 )], color='red');
  File "/home/sageserver/sage/sage-5.0-stock/local/lib/python2.7/site-packages/sage/plot/point.py", line 312, in point
    return point3d(points, **kwds)
  File "/home/sageserver/sage/sage-5.0-stock/local/lib/python2.7/site-packages/sage/plot/plot3d/shapes2.py", line 1022, in point3d
    A = sum([Point(z, size, **kwds) for z in v])
  File "/home/sageserver/sage/sage-5.0-stock/local/lib/python2.7/site-packages/sage/plot/plot3d/shapes2.py", line 680, in __init__
    self.loc = (float(center[0]), float(center[1]), float(center[2]))
TypeError: 'sage.rings.integer.Integer' object does not support indexing