Python/C2/Using-Sage/English

From Script | Spoken-Tutorial
Revision as of 11:32, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello Friends and Welcome to the tutorial on 'Using Sage'.
Show Slide 2

learning objectives

At the end of this tutorial, you will be able to,
  1. Learn the range of things for which Sage can be used.
  2. Know the functions used for Calculus in Sage.
  3. Learn about graph theory and number theory using Sage.


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Sage".

Let us begin with Calculus. We shall be looking at limits, differentiation, integration, and Taylor polynomial.

Open sage notebook We have our Sage notebook running. In case, you don't have it running, start is using the command, sage --notebook.
lim(x*sin(1/x), x=0) To find the limit of the function x*sin(1/x), at x=0, we say
lim(1/x, x=0, dir='right') We get the limit to be 0, as expected.

It is also possible to limit a point from one direction. For example, let us find the limit of 1/x at x=0, when approaching from the positive side.

lim(1/x, x=0, dir='left') To find the limit from the negative side, we say,
var('x')
f = exp(sin(x^2))/x
diff(f, x)
Let us now see how to perform differentiation, using Sage. We shall find the differential of the expression exp(sin(x^2))/x w.r.t x. For this, we shall first define the expression, and then use the diff function to obtain the differential of the expression.
var('x y')
f = exp(sin(y - x^2))/x

diff(f, x)

diff(f, y)
We can also obtain the partial differentiation of an expression w.r.t one of the variables. Let us differentiate the expression exp(sin(y - x^2))/x w.r.t x and y.
integrate(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y) Thus we get our partial differential solution. Now, let us look at integration. We shall use the expression obtained from the differentiation that we calculated before, diff(f, y) which gave us the expression ---e^(sin(-x^2 + y))*cos(-x^2 + y)/x. The integrate command is used to obtain the integral of an expression or function.
integral(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y, 0, pi/2) As we can see,we get back the correct expression. The minus sign being inside or outside the sin function doesn't change much.

Now, let us find the value of the integral between the limits 0 and pi/2.

var('x n')
taylor((x+1)^n, x, 0, 4)
Hence we get our solution for the definite integration. Let us now see how to obtain the Taylor expansion of an expression using sage. Let us obtain the Taylor expansion of (x + 1)^n up to degree 4 about 0.
We easily got the Taylor expansion,using the function taylor(). This brings us to the end of the features of Sage for Calculus, that we will be looking at. For more, look at the Calculus quick-ref from the Sage Wiki.
Show Slide 4

Equation

Next let us move on to Matrix Algebra. Let us begin with solving the equation Ax = v, where A is the matrix matrix([[1,2],[3,4]]) and v is the vector vector([1,2]).
Switch back to sage notebook page
A = matrix([[1,2],
            [3,4]])

v = vector([1,2])
x = A.solve_right(v)
x
To solve the equation, Ax = v we simply say
x = A.solve_left(v)
x
To solve the equation, xA = v we simply say
G = Graph({0:[1,2,3], 2:[4]}) The left and right here, denote the position of A, relative to x.

Now, let us look at Graph Theory in Sage.

We shall look at some ways to create graphs and some of the graph families available in Sage.

The simplest way to define an arbitrary graph is to use a dictionary of lists. We create a simple graph by using the Graph() function.

G.show() to view the visualization of the graph, we say
G = DiGraph({0:[1,2,3], 2:[4]}) Similarly, we can obtain a directed graph using the DiGraph function.
G = graphs.CompleteGraph(5)
G.show()
Sage also provides a lot of graph families which can be viewed by typing graph.<tab>. Let us obtain a complete graph with 5 vertices and then show the graph.
prime_range(100, 200) Sage provides other functions for Number theory and Combinatorics. Let's have a glimpse of a few of them. prime_range gives primes in the range 100 to 200.
is_prime(1999) is_prime checks if 1999 is a prime number or not.
factor(2001) factor(2001) gives the factorized form of 2001.
C = Permutations([1, 2, 3, 4])
C.list()
The Permutations() gives the permutations of [1, 2, 3, 4]
C = Combinations([1, 2, 3, 4])
C.list()
And the Combinations() gives all the combinations of [1, 2, 3, 4]
Show Slide 5

Summary slide

This brings us to the end of the tutorial. In this tutorial, we have learnt to,
  1. Use functions for calculus like -- - lim()-- to find out the limit of a function - diff()-- to find out the differentiation of an expression - integrate()-- to integrate over an expression - integral()-- to find out the definite integral of an
    Unexpected indentation.
    expression by specifying the limits
    Block quote ends without a blank line; unexpected unindent.
    • solve()-- to solve a function, relative to it's position.
  1. Create Both a simple graph and a directed graph, using the functions graph and digraph respectively.
  2. Use functions for Number theory.For eg: - primes_range()-- to find out the prime numbers within the
    Unexpected indentation.
    specified range
    Block quote ends without a blank line; unexpected unindent.
    • factor()-- to find out the factorized form of the number specified
    • Permutations(), Combinations()-- to obtain the required permutation and combinations for the given set of values.


Show Slide 6

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. How do you find the limit of the function x/sin(x) as x tends to 0 from the negative side.
  2. List all the primes between 2009 and 2900
  3. Solve the system of linear equations
    x-2y+3z = 7 2x+3y-z = 5 x+2y+4z = 9


Show Slide 7

Solution of self assessment questions on slide

And the answers,
  1. To find out the limit of an expression from the negative side,we add an argument dir="left" as

Enumerated list ends without a blank line; unexpected unindent.

lim(x/sin(x), x=0, dir="left")

2. The prime numbers from 2009 and 2900 can be obtained as,

prime_range(2009, 2901)
  1. We shall first write the equations in matrix form and then use the solve() function

Enumerated list ends without a blank line; unexpected unindent.

A = Matrix([[1, -2, 3],
            [2, 3, -1],
            [1, 2, 4]])

b = vector([7, 5, 9])

x = A.solve_right(b)

To view the output type x

x
Show Slide 8

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika