Analysis tutorial: Limits
system:sage

<hr />
<h1 style="text-align: center;"><span style="color: #000080;">Calculus tutorial: Limits with Sage</span></h1>
<p style="text-align: center;">Jose Guzman(*) and Estefan&iacute;a J. Mart&iacute;nez</p>
<p style="text-align: center;">version 1.0.2</p>
<p style="text-align: center;">April 17, 2009</p>
<p>(*) Please send any suggestions or comments to  <a href="mailto:nin@neurohost.org">nin@neurohost.org</a></p>
<hr />
<h2 style="text-align: left;">Index</h2>
<ol style="text-align: left;">
<li> <a href="#intro"> Introduction </a></li>
<li><a href="#equation"> Free falling equation<br /></a></li>
<li><a href="#analysis"> Analytical and graphical representation<br /></a></li>
<li><a href="#instant"> Calculation of the instant velocity</a></li>
<li><a href="#limitssage"> Calculate limits with Sage</a></li>
<li><a href="#limitsdef">Limits rules</a></li>
</ol> 
<hr />
<p><a name="intro"></a></p>
<h2>Introduction<br /></h2>
<p>A limit of a function is the value that the function takes as it approaches to a given variable (such as t). Image for example that we would like to know the velocity of an object exactly 1 second after free fall (at t=1). We would want to calculate the value that the velocity function takes as the time approaches to 1 second. To calculate the velocity we could record the distance (meters) from t=1 to t=2 and than divide everything by one second. This would give us the velocity in m/s from t=1 to t=2. However, this represents only the mean velocity during the whole first second (for example from t=1.0 to t=2.0). We could calculate the distance between t=1 and t=1.5 and divide everything by 0.5. However, this would give us the mean velocity between t=1 and t=1.5. In analog manner, we could try everytime smaller and smaller t intervals (for example t=1.0001) but this would always give us the mean velocity in this small interval. If we finally decide to take t=1 as the final interval, the distance calculated will be divided by zero, giving us an indetermination. There should be a method to calculate the velocity at exactly one given time (and not time interval). This is the instant velocity.</p>
<p>If we need to calculate the velocity exactly at that time (t=1) we need to calculate the limit of the velocity function as it approached to t=1. This is the instant velocity at time t=1.</p>
<p><a name="equation"></a></p>
<h2>Free falling equation example<br /></h2>
<p>We will calculate here the instant velocity of a ball falling after 1 second. For that we need first to know the equation which describes the free falling:</p>
<p style="text-align: left;">$d(t)={\displaystyle \frac{1}{2}gt^{2}}$</p>
<p style="text-align: left;">This equation calculates the distance (d) traveled after a time given&nbsp; by (t) in seconds. Remember this is not the velocity function. We will use this function to calculate the distances as a function of time.</p>
<p><a name="analysis"></a></p>
<h2 style="text-align: left;">Analytical and graphical representation</h2>
<p>To get familiar with sage commands, we can simply type the equation in sage with the following code:</p>

{{{id=6|
G = 9.81 # grav. constant in m/sec^2

import sympy # we will need sympy package later (symbolic variable manipulation) 
t = var('t') # time (t) is the independent variable
g = var('g') # g is a constant, but we will have to define it algebraically before using it
d(t) = (1/2)*g*(t**2) # equation of free fall d(t)=1/2*g*t^2
///
}}}

<p>We will first calculate some values with python for&nbsp; the free falling equation. For that we can calculate d for different t values. We can tell Sage to give us different t values the <strong>srange()</strong> funcion. We will create a list of values starting in 0.0 until 3.0 in intervals of 0.25 seconds. We do it with srange(0.0,3.0,0.25). After that we only have to solve the equation d(t) and substitute the symbolic variable g for the G constant defined above. We do it with <strong>d(t).subs(g=G)</strong></p>

{{{id=46|
print '[distance (m)] [time(sec)]'
for t in srange(0.0,3.0,0.25): print '[%12.4f] [%9.4f]' %(d(t).subs(g=G),t)
///
[distance (m)] [time(sec)]
[      0.0000] [   0.0000]
[      0.3066] [   0.2500]
[      1.2263] [   0.5000]
[      2.7591] [   0.7500]
[      4.9050] [   1.0000]
[      7.6641] [   1.2500]
[     11.0363] [   1.5000]
[     15.0216] [   1.7500]
[     19.6200] [   2.0000]
[     24.8316] [   2.2500]
[     30.6562] [   2.5000]
[     37.0941] [   2.7500]
}}}

<p>We can display the free falling equation within the t=0 and t=3 values with the command plot(). Plot takes as argument the function to plot and the limits of the function. After that, the show() command will plot the object/s with the given additional preferences (in our case x and y coordinates and a tuple with the x,y size)</p>

{{{id=45|
fig1 = plot(d.subs(g=G),0,3) # to plot we need to define g because only displays x vs.y
xaxis = text('time (sec)', (2,-10), rgbcolor='black')

show(fig1+xaxis,xmin=0,xmax=3,ymin=-10,ymax=50,figsize=[3,3])
///
}}}

<p><a name="instant"></a></p>
<h2>Calculation of the instant velocity<br /></h2>
<p>We are still interested in calculating the velocity at time t=1. Because the velocity is the ratio between the distance traveled and the time, we would only need to find the function which represents the velocity when t is exactly equal to 1. If we calculate velocities in smaller time intervals, we will approximate to the value of the instantaneous velocity at t=1. We will first define the function of the obtained velocity with the different values of t.</p>
<p>$f(t)= {\displaystyle \frac{d(t)-d(1)}{(t-1)}=\frac{\frac{1}{2}gt^{2}-\frac{1}{2}(1)^{2}g}{(t-1)}=\frac{gt^{2}-g(1)^{2}}{2(t-1)}=\frac{gt^{2}-g}{2(t-1)}=\frac{g(t^{2}-1)}{2(t-1)}} $</p>
<p>As the value of t is getting smaller and approaching to t, the velocity values arrive to the instant value at t=1 (see table bellow).</p>

{{{id=49|
f(t)=(d(t)-d(1))/(t-1) # velocity function

pt = point((1, 9.81), rgbcolor='white', pointsize=40, faceted=True)

fig = plot(f.subs(g=9.81), xmin=0,xmax=1) + plot(f.subs(g=9.81), xmin=1, xmax=1.5)

show(fig+pt)
///
}}}

<p>We can simply calculate the different values of the function f(t) with t values between t=1.01 seconds and t=1.001 in 10 msec steps (0.0010 seconds). As you can see in the routine, as we approach to t=1.01 the functions takes the value of the limit (9.81)</p>

{{{id=30|
print '[     t    ][ delta_time ] [velocity]'

for t in srange(1.01,1.0000,-.0010):
    print '[%10.6f][%12.8f] [%8.6f]'%(t,(t-1),f(t).subs(g=G))
///
[     t    ][ delta_time ] [velocity]
[  1.010000][  0.01000000] [9.859050]
[  1.009000][  0.00900000] [9.854145]
[  1.008000][  0.00800000] [9.849240]
[  1.007000][  0.00700000] [9.844335]
[  1.006000][  0.00600000] [9.839430]
[  1.005000][  0.00500000] [9.834525]
[  1.004000][  0.00400000] [9.829620]
[  1.003000][  0.00300000] [9.824715]
[  1.002000][  0.00200000] [9.819810]
[  1.001000][  0.00100000] [9.814905]
}}}

<p>If we look carefully at the function f(t), we can see that setting t=1 would make both the numerator (t-1) and the denominator $(t^{2}-1)$ equal to zero and we cannot calculate the velocity there. Because f(1) is undefined,  we have to analyze it by way of limits. We have to find the limit of that function when this tends to the value. The notation for this would be:</p>
<p>$ {\displaystyle \lim_{ t \to 1}f(t)}$</p>
<p>We can calculate the limit of our function when it approaches to 1 by simply substitution of the value of t by 1 in the function</p>
<p>$ {\displaystyle \lim_{t\to 1}<span class="typeset"><span class="scale"></span></span> {\displaystyle \frac{g(t^{2}-1)}{2(t-1)} }}$</p>
<p>&nbsp;</p>

{{{id=51|
f(1).subs(g=G)
///
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sage/sagenb/sage_notebook/worksheets/joseguzman/9/code/7.py", line 7, in <module>
    exec compile(ur'f(_sage_const_1 ).subs(g=G)' + '\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/calculus.py", line 4427, in subs
    return self.substitute(*args, **kwds)
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/calculus.py", line 4421, in substitute
    X = self.simplify()
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/calculus.py", line 3986, in simplify
    S = evaled_symbolic_expression_from_maxima_string(self._maxima_init_())
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/calculus/calculus.py", line 10147, in evaled_symbolic_expression_from_maxima_string
    return symbolic_expression_from_maxima_string(maxima.eval(x))
  File "/home/sage/sage_install/sage-a/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 981, in eval
    raise TypeError, 'error evaluating "%s":\n%s'%(code,s)
TypeError: error evaluating "((((g) * (1)) / (2)) - ((g) / (2))) / (0)":
Error executing code in Maxima
CODE:
	((((g) * (1)) / (2)) - ((g) / (2))) / (0);
Maxima ERROR:
	
Division by 0
}}}

<p>As we can see, Sage prompt us an error of division by 0. We can temporally solve this problem by simplify the function before calculating of the limit.</p>
<p>&nbsp;</p>
<p>$f(t)={\displaystyle \frac{g(t^{2}-1)}{2(t-1)} = \frac{g(t+1)}{2}}$</p>
<p>&nbsp;</p>
<p>And calculate the limit again of the new function by substitution</p>

{{{id=12|
f_new(t)=(g*(t+1))/2
print f_new(1).subs(g=G)

# limit of the function
# f(t)=g*(t^2-1)/2*(t-1)

#h(t)=(d(t)-d(1))/(t-1)
#sympy.limit(f.subs(g=G),t,1)
///
9.81000000000000
}}}

<p><a name="limitssage"></a></p>
<h2>Calculate limits with Sage</h2>
<p>Sage allows us to calculate limits very easily. The syntax to calculate a limit is <strong>limit(f,x,a)</strong> where f is the function, x is the variable and a is the value to approach. We will calculate the limit of our function. We have to put the sympy funciton (explain why)</p>

{{{id=53|
# calculate the limit of f(t) when t tends to 1
# we need the sympy (symbolic manipulation library) to solve the limit correctly.
sympy.limit(f.subs(g=G),t,1)
///
9.81000000000000
}}}

<p>we can test that the limit of the function f(t) and the limit of the new function resulting of substition in the f_new(t) have the same limit (9.81)</p>

{{{id=54|
# calculate the limit of f_new(t) when t tends to 1
sympy.limit(f_new.subs(g=G),t,1)
///
9.81000000000000
}}}

<p><a name="limitsdef"></a></p>
<h2>Limit rules</h2>
<p><br />There are some limits whose solution is known. We will display them here and calculate with Sage:</p>
<p>$ {\displaystyle \lim_{x\to1}k=k}$</p>

{{{id=65|
# the limit of a constant is simply the constant
var('K')
limit(K,x=1)
///
K
}}}

{{{id=31|
f(x)=1/x
p = plot(f,0,1)
#label = text('${\displaystyle \lim_{x \to \infty}{\displaystyle \frac{1}{x}=0} }$',(0.5,100))
fig = p

fig.show(xmin=0,xmax=1,ymin=0,ymax=100)
///
}}}

<p>$ {\displaystyle \lim_{x \to \infty}{\displaystyle \frac{1}{x}=0} }$</p>

{{{id=32|
# the limit of 1/x when x tends to infinity is zero

limit(1/x,x=oo)
///
0
}}}

<p><span>${\displaystyle&nbsp; \lim_{x \to \infty^{-}} \frac{1}{x}=0 }$</span></p>

{{{id=56|
# The limit of 1/x when x tends to -infinity is zero too.
limit(1/x,x=oo,dir='below')
///
0
}}}

<p><span>${\displaystyle&nbsp; \lim_{x \to 0^{+}} \frac{1}{x}=\infty }$</span></p>

{{{id=34|
# the limit of 1/x when x tends to zero above is infinity
limit(1/x,x=0,dir='above')
///
+Infinity
}}}

<p>$<span>{\displaystyle&nbsp; \lim_{x \to 0^{-}} \frac{1}{x}=-\infty }$</span></p>

{{{id=66|
# the limit of 1/x when x tends to zero below is + infinity
limit(1/x,x=0,dir='below')
///
}}}

{{{id=35|
f(x)=sin(x)/x
p = plot(f,0,2*pi)
p
///
}}}

<p>$<span>{\displaystyle&nbsp; \lim_{x \to 0} \frac{sin(x)}{x}=1 }$</span></p>

{{{id=67|
# the limit of sin(x)/x when x tends to zero is one
limit(sin(x)/x,x=0)
///
}}}

{{{id=58|
f(x) = (cos(x)-1)/x
plot(f,0,2*pi)
///
}}}

<p><span>${\displaystyle&nbsp; \lim_{x \to 0} \frac{cos(x)-1}{x}=0 }$</span></p>

{{{id=68|
# the limit of (cos(x)-1)/x when x tends to zero is zero
limit((cos(x)-1)/x,x=0)
///
}}}

{{{id=60|
f(x) = (1+1/x)**x
p = plot(f,0,20)
ln = line([(0,e),(20,e)],rgbcolor='gray',linestyle='--')
(p+ln).show(xmin=0,xmax=20)
///
}}}

<p>$<span>{\displaystyle&nbsp; \lim_{x \to \infty} (1+ 1/x)^{x} =e }$</span></p>

{{{id=64|
# the limit of (1+1/x)**x when x tends to infinity is e
limit( (1+1/x)**x , x=oo)
///
e
}}}

{{{id=69|

///
}}}