Differential Calculus in SAGE: A Primer
system:sage

<h2 style="text-align: center;"><span class="cmr-17">Differential Calculus in SAGE: A Primer</span></h2>
<h3 style="text-align: center;">Sourav Sen Gupta</h3>
<h4 style="text-align: center;">University of Washington</h4>
<p class="noindent" style="text-align: center;"><br /> <span class="cmbx-12">Version 1.0</span><br /> <span class="cmbx-12">March 1, 2009</span></p>
<p class="noindent" style="text-align: center;"><span class="cmbx-12"><br /></span></p>
<h3 class="likesectionHead">Introduction</h3>
<p class="noindent">This <span class="cmtt-12">is a compilation</span> of SAGE commands that are useful for a student in an introductory course in undergraduate calculus. It is not intended to teach SAGE or calculus formally. There are many good texts on calculus and lots of information about SAGE can be found at <span class="cmtt-12">sagemath.org</span>. The primary focus of this document is to make a student familiar with commands in SAGE that correspond to the theory he/she learns during a regular calculus course. The students are encouraged to experiment and learn more about SAGE and its various applications in different branches of mathematics.</p>
<!--l. 79-->
<p class="indent">This primer is distributed in PDF format besides this SAGE worksheet. The worksheet version can be imported into the SAGE notebook environment running in a web browser, and then the displayed snippets of code may be executed by SAGE if one clicks on the small &ldquo;evaluate&rdquo; link below each cell, or press <span style="font-family: courier new,courier;">&lt;SHIFT&gt;&lt;ENTER&gt;</span> after each command.</p>
<p class="indent">To get familiar with basic SAGE operations and elementary numeric and symbolic commands, please refer to the "SAGE as a Smart Calculator" primer.</p>

<h3 class="likesectionHead">Variables and Functions</h3>
<h4>Declaring a Variable</h4>
<p>By default, 'x' is defined to be a variable in SAGE. So, you can use 'x' as a variable anytime. Otherwise, you are required to declare a variable as follows</p>

{{{id=0|
var('y')
///
y
}}}

<h4>Constructing a Function</h4>
<p>Once you have a variable to work with (or 'x' by default), you can define a function as follows</p>

{{{id=5|
f = x^2+3*x-1; f
///
x^2 + 3*x - 1
}}}

<p>This defines the function f(x). You can try evaluating it at different values of 'x', as follows</p>

{{{id=7|
f(2)
///
9
}}}

<p>or get the function evaluated symbolically at some variable point 'x=t' (note that we needed to declare 't' first)</p>

{{{id=13|
var('t')
f(t)
///
t^2 + 3*t - 1
}}}

<p>Try defining another function as follows, and you can do some cool arithmetic operations on those.</p>

{{{id=18|
g = x^3 + 1; g
///
x^3 + 1
}}}

{{{id=9|
f+g
///
x^3 + x^2 + 3*x
}}}

{{{id=8|
f*g; expand(f*g)
///
(x^2 + 3*x - 1)*(x^3 + 1)
x^5 + 3*x^4 - x^3 + x^2 + 3*x - 1
}}}

{{{id=16|
f/g
///
(x^2 + 3*x - 1)/(x^3 + 1)
}}}

{{{id=17|
f(g); expand(f(g))
///
(x^3 + 1)^2 + 3*(x^3 + 1) - 1
x^6 + 5*x^3 + 3
}}}

<h4>Function in two or more variables</h4>
<p>You can also declare multiple variables at the same time and construct function based on those</p>

{{{id=21|
x,y,z = var("x,y,z")
h = x^3 + y^2 + z; h
///
z + y^2 + x^3
}}}

{{{id=22|
h(1,2,3)
///
8
}}}

{{{id=23|
h(y=2)
///
z + x^3 + 4
}}}

<h4>Plot of Functions</h4>
<p>By the way, SAGE provides you with the cool feature of plotting any 2D (1 variable) or 3D (2 variables) functions.</p>

{{{id=25|
var('x')
f = x^2
plot(f, [x,-10,10])
///
}}}

{{{id=24|
var('x,y')
g = x^2 + y^3
plot3d(g, [x,-10,10], [y,-10,10])
///
}}}

<p>Please refer to "SAGE as a Smart Calculator" primer for more information regarding variables and function definitions, handling and plotting procedures. You will find different useful trigonometric, exponential and logarithmic functions out there as well. It is strongly recommended that you are familiar with basic trigonometric fuctions and their properties before you dive into calculus.</p>
<h4>Limits of Functions</h4>
<p>Now that you have played with functions, let's see if we can do a bit of calculus. Let us check some limiting values for different functions. We shall define the variables and functions for each of these cases. Try to verify all these limits using the known limit laws that you learnt in your calculus course.</p>

{{{id=31|
var('x')
f = (x^2 + x - 2)/(x^3 - 1)
limit(f, x=1)
///
1
}}}

{{{id=32|
var('x')
f = (sqrt(x^2 + 2) - 2)/(2*x - 1)
limit(f, x=infinity)
///
1/2
}}}

<p><img src="cells/35/sage95.png?1235878534" alt="" /></p>

{{{id=33|
var('x')
f = log(abs(x))
limit(f, x=0)
///
-Infinity
}}}

{{{id=34|
plot(f, [x, -3, 3])
///
}}}

<h4>Some Special Limits</h4>
<p>Let us verify some of the standard limit formulae using SAGE, as follows</p>
<p>&nbsp;</p>

{{{id=37|
var('x')
f = (e^x - 1)/x
limit(f, x=0)
///
1
}}}

{{{id=51|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
}}}

{{{id=38|
var('x')
f = sin(x)/x
limit(f, x=0)
///

1
}}}

{{{id=50|
plot(f, [x,-100,100]).show(ymin=-0.5,ymax=1)
///
}}}

{{{id=40|
var('x')
f = log(x+1)/x
limit(f, x=0)
///
1
}}}

{{{id=49|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
}}}

{{{id=48|
var('x')
f = (x + 1)^(1/x)
limit(f, x=0)
///
e
}}}

{{{id=41|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
}}}

<p>Another cool feature of SAGE is that it throws an error asking you "Is * positive or negative?" or outputs "und" if the limit of the function does not exist at the point you are checking. A couple of examples are as follows.</p>

{{{id=44|
var('x')
f = log(x)
limit(f, x=0)
///
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sage/sagenb/sage_notebook/worksheets/souravsg/10/code/38.py", line 9, in <module>
    exec compile(ur'limit(f, x=_sage_const_0 )' + '\n', '', 'single')
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/SQLAlchemy-0.4.6-py2.5.egg/", line 1, in <module>
    
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/functional.py", line 313, in limit
    return f.limit(dir=dir, taylor=taylor, **argv)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/calculus.py", line 2376, in limit
    l = self._maxima_().limit(v, a)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 1288, in __call__
    return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 1208, in function_call
    ['%s=%s'%(key,value.name()) for key, value in kwds.items()])))
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 1026, in new
    return self(code)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 961, in __call__
    return cls(self, x, name=name)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 1331, in __init__
    raise TypeError, x
TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(x>0)' before integral or limit evaluation, for example):
Is  x  positive or negative?
}}}

{{{id=52|
plot(f, [x,0,5])
///
}}}

{{{id=42|
var('x')
f = 1/x
limit(f, x=0)
///
und
}}}

{{{id=43|
plot(f, [x,-5,5]).show(ymin=-5, ymax=5)
///
}}}

<h4>Continuity of Functions</h4>
<p>As you know from your calculus course, a function f(x) is continuos at a point x = a if and only if the limit of f(x) exists as x -&gt; a and the functional value is the same at that point as well. Notationally, this means</p>
<p>f(x) continuous \Rightarrow \lim f(x) = f(a)</p>
<p>Let us check for some of the continuities using SAGE. We will use some basic coding techniques (if statements and other conditional checks) in this piece. If you are not familiar with coding at all, try to look at the pieces as simple English text or refer to "SAGE as a Smart Calculator" primer for SAGE programming basics.</p>

{{{id=53|
var('x')
f = x^2
l = limit(f, x=0)
if l==f(0):
    print 'the function is continuous'
else:
    print 'the function is discontinuous'
///
the function is continuous
}}}

{{{id=55|
var('x')
f = 1/x
l = limit(f, x=0)
if l==f(0):
    print 'the function is continuous'
else:
    print 'the function is discontinuous'
///
Traceback (most recent call last):        print 'the function is discontinuous'
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/calculus.py", line 4917, in __call__
    return SymbolicConstant( self._operator(*map(lambda x: x._obj, new_ops)) )
  File "element.pyx", line 1199, in sage.structure.element.RingElement.__div__ (sage/structure/element.c:9099)
  File "integer.pyx", line 1195, in sage.rings.integer.Integer._div_ (sage/rings/integer.c:9591)
  File "integer_ring.pyx", line 230, in sage.rings.integer_ring.IntegerRing_class._div (sage/rings/integer_ring.c:4745)
ZeroDivisionError: Rational division by zero
}}}

{{{id=56|

///
}}}