# Calculus

## Sage

### Derivatives

We will start with a simple equation 5x2 + 6x + 100

Now to find the derivative in Sage you can use the diff or derivative function (both functions do the same thing, but have different names).

Here is an example of finding the derivative of our equation:

#Remember we must explicitly declare our symbolic variables
var('x')
eq = 5*x^2 + 6*x + 100
diff(eq)
#output: 10x+6

#alternatively since Python is an object oriented language we can use the diff
#method associated with the SymbolicArthimetic Class
eq.diff( eq )
#output: 10x+6


Example: Using the Chain Rule

f(x) = sin(x)
g(x) = cos(x)
show( derivative(f(g(x))) )
#Output: -sin(x)cos(cos(x))


### 2nd Derivatives

Let's continue with the example equation above: 5x2 + 6x + 100

Now to find the 2nd derivative or 3rd,etc the syntax is just like with the first derivative except that you add an additional parameter specifying which derivative you want.

Here is an example:

#Remember we must explicitly declare our symbolic variables
var('x')
eq = 5*x^2 + 6*x + 100
#Find the second derivative
diff( eq, 2 )
#output: 10

#object oriented method
eq.diff(2)
#Output: 10


If we wanted to find the 3rd or 4th derivative we would just replace the 2 in eq.diff(2) with a 3 or 4.

You can use the derivative method with other functions like solve to accomplish a variety of tasks.

Example: Finding where the derivative equals 0

var('x')
eq = 5*x^2 + 6*x + 100
solve( [ eq.diff() == 0 ], x )
#output: x = -3/5


### Finding the Equation of the Tangent Line

Sage does not have a built-in function for calculating the tangent line of a function, but writing one is fairly trivial.

Tangent Line Function:

def tangent_line(f,x,a):
slope = limit( (f - f(a) ) / (x-a), x=a)
eq = slope * (x - a) + f(a)
return eq


The function tangent_line takes a function f, a variable x, and a point a. First the function finds the slope by taking $\lim_{x \to + a}\frac{f(x) - f(a)}{x - a}$

then it creates the equation of the line slope * (x - a) + f(a).

Usage:

expand( tangent_line( x^2, x, 1 ) )
#output: 2x - 1


#### Graphing the Tangent Line

The tangent_line function above can also be used to graph a function and it's tangent line at some point x.

Example: Graph x2 and it's tangent line at x = 1

plot([x^2,tangent_line(x^2,x,1)],(-5,5))


Output:

### Integrals

Using the function : 5x2 + 6x + 100 we can integrate it using the integrate or integral functions.

Example:

var('x')
eq = 5*x^2 + 6*x + 100
eq.integrate()


Output: $\frac{{5 {x}^{3} }}{3} + {3 {x}^{2} } + {100 x}$

#### Integrating from finite start and end points

Lets say we want to perform this integration: $\int_0^{100} 5x^2 + 6x + 100\,dx$

All we need to is add parameters for the variable we are integrating for and our two endpoints.

Here is the Sage Code:

var('x')
eq = 5*x^2 + 6*x + 100
eq.integrate(x,0,100)


Output: $\frac{5120000}{3}$

#### Improper Integrals

Let's say we want perform this integration $\int_1^\infty \frac{1}{x^2}\,dx$

It actually works just like with finite start and end points

Example:

var('x')
integrate( eq2, x, 1, infinity)


Output: 1

However this doesn't work with all functions. For example consider: $\int_1^\infty x^2\,dx$

var('x')
integrate( x^2, x, 1, infinity)


Output: ValueError: Integral is divergent.

Often when talking about indefinite integrals we think of them in terms of $\lim_{t \to + \infty} \int_1^t f(x)\,dx$

We can write our code in Sage to explicitly express this idea.

limit( integrate( 1/x^2, x, 1, t), t=infinity)


Ouput: 1

### Implicit Differentiation

Example of Implicit Differentiation by Dr. Krug:

y = function("y",x)
F = x^2 + y^2 - 4*x - 1
show(F.diff())


Output: 2y(x)diff(y(x),x,1) + 2x - 4

show( solve(F.diff(x) == 0,diff(y(x),x,1)) )


Output: $\left[{\rm diff}({\rm y}(x), x, 1) = \frac{2 - x}{{\rm y}(x)}\right]$

#### Implicit Plot

We can plot the function in Dr. Krug's example using Sage's implicit_plot function.

var('a b')
implicit_plot(a^2 + b^2 - 4*a - 1, (-10,10), (-10, 10), plot_points=100)


Output: