You can insert a new cell above or below any existing cell or block of text. To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple horizontal line pop up. Click on this horizontal line to produce a new Sage cell. Try that just below right now and then type in some other complicated expression of numbers and then click "evaluate."
Inserting a new block of text
You can also insert a new block of text (also called an "html cell") above or below any existing Sage cell or other block of text. To insert a new html cell, you Shift+click the purple horizontal line that pops up instead of just clicking on it. This will pop open a tiny text editor for you to type in. There are lots of things you can do inside an html block, but for now, we will keep it simple. You can always edit an existing html cell by simply double-clicking on the corresponding cell. After you are done editing an html cell, click "Save changes."
Add an html cell at the very top of this worksheet and type in your names.
Either one clicks help on the top right of the page or one tries search_src("my querie") to get an answer.
This worksheed will introduce you to the basic properties and use of symbolic functions in Sage.
Throughout this worksheet I have include blank "Sage cells" in which you can type examples and try new things. To evaluate a cell, just click on the cell and then click "evaluate."
The first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. (Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) Without doing this, Sage will not let me use $x$ and $y$ without giving an error. The %auto command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.
{{{id=40| %auto var('x,y') /// (x, y) }}} {{{id=0| solve(x^2+3*x+2==0,x) /// [x == -2, x == -1] }}}You can quickly factor polynomials in Sage using factor. Evaluate each of the next two cells.
{{{id=37| factor(x^2+3*x+2) /// (x + 1)*(x + 2) }}} {{{id=38| factor(x^2+3x+2) #without the * between 3 and x, there is an error. /// line 3 factor(x**_sage_const_2 +3x+_sage_const_2 ) #without the * between 3 and x, there is an error. ^ SyntaxError: invalid syntax }}}Note:
Now, you try one below.
{{{id=57| /// }}}
You can also use the factor command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms.
{{{id=4| factor((x^2+4*x+3)/(x^2-9)) /// (x + 1)/(x - 3) }}}OK, your turn. Try a new one below.
{{{id=126| /// }}}If you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. Here I store a rational expression as f, then simplify it, integrate it, differentiate it, print it, and then plot it. Notice that you can often write f.[something] to perform commands.
{{{id=127| f=(x^5-4*x+1)/(x^2-9) /// }}} {{{id=128| f.partial_fraction() /// 9*x + 116/3/(x - 3) + 115/3/(x + 3) + x^3 }}} {{{id=97| f.integrate() /// 1/4*x^4 + 9/2*x^2 + 116/3*log(x - 3) + 115/3*log(x + 3) }}} {{{id=98| g=f.diff() g.full_simplify() /// (3*x^6 - 45*x^4 + 4*x^2 - 2*x + 36)/(x^4 - 18*x^2 + 81) }}} {{{id=43| plot(f,(x,-2,4)) ///
}}}
Since the expression has a vertical asymptote, you can use ymin and ymax to give a top and bottom range to your plot.
{{{id=44| plot(f,(x,-2,4),ymin=-10,ymax=10) ///
}}}
Deleting an expression works as follows:
{{{id=7| del(f) /// }}} {{{id=8| f /// Traceback (most recent call last): File "To use (callable) functions in SAGE (so that you can write something like f(2)) just write f(x)=... before writing the function expression. Then you can use f(2), f(a), f(x+h) or any kind of function notation that you are familar with from algebra. Notice that f=... is an expression and not a callable function. Inserting arguments into expressions might be a problem.
{{{id=10| f(x)=5*x^2+4 /// }}} {{{id=11| f /// x |--> 5*x^2 + 4 }}} {{{id=12| f(4) /// 84 }}} {{{id=13| a=4 /// }}} {{{id=14| f(a) /// 84 }}} {{{id=16| g(x)=x^2-3*x+2 f(g(x)) /// 5*(x^2 - 3*x + 2)^2 + 4 }}} {{{id=17| g(f(x)).show() # nice output like if you click "typeset" on top ///Functions as Expressions can be used for differentiation and integration and to define new piecewise objects. First we introduce two callable functions $ \sinh $ and $ \arcsin$ which are already implemented in SAGE and calculate derivatives and (definite) integrals.
{{{id=101| f1(x)=sinh(x) /// }}}Notice that $ f1 $ is a callable function, so is its indefinite integral. If we integrate $ f1(x) $ we obtain as a result an expression.
{{{id=100| f1.integrate() /// x |--> cosh(x) }}} {{{id=139| f1(x).integrate() /// cosh(x) }}} {{{id=103| f1.integrate(x,0,1) /// cosh(1) - 1 }}} {{{id=102| f1.diff() /// x |--> cosh(x) }}} {{{id=104| f2(x)=arcsin(x) /// }}} {{{id=105| f2.diff() /// x |--> 1/sqrt(-x^2 + 1) }}} {{{id=106| f2.plot() ///
}}}
{{{id=107|
f2.integrate()
///
x |--> x*arcsin(x) + sqrt(-x^2 + 1)
}}}
We can also work with $ \arcsin $ as an expression, not as a callable function.
{{{id=108| f3=arcsin(x) /// }}} {{{id=109| f3.diff() /// 1/sqrt(-x^2 + 1) }}} {{{id=110| f3.integrate() /// x*arcsin(x) + sqrt(-x^2 + 1) }}} {{{id=120| numerical_integral(f3,0,1) /// (0.57079632246586909, 8.9230242453635791e-07) }}} {{{id=121| f3.integrate(0,1) /// 1/2*pi - 1 }}} {{{id=111| f3.plot() ///
}}}
Next we introduce a Python function, which has no symbolic meaning anymore and can generically not be integrated and differentiated symbolically.
{{{id=133| def f4(z): if (z<0): return 0 else: return 1 /// }}} {{{id=135| f4(x).integrate() /// Traceback (most recent call last): File "A numerical integration is certainly possibly!
{{{id=136| numerical_integral(f4,-1,1) /// (1.0, 1.1102230246251565e-14) }}}Last we introduce piecewise defined functions.
For subtlties on function expressions and callable functions see here.
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.
{{{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 }}}
}}}
Let us verify some of the standard limit formulae using SAGE, as follows
{{{id=56| 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=58|
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=59|
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)
///
}}}
Cool, isn't it?
Sage will try to return symbolic answers when it can. Use .show() to get a pretty output. use .n() to get a numeric value. Below I show you mutliple ways to use the n() function.
{{{id=19| sqrt(2) /// sqrt(2) }}} {{{id=47| sqrt(2).show() ///Try similar things with the number $e$.
{{{id=81| e.n(digits=500) /// 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931 }}}
}}}
{{{id=25|
#You can include multiple functions in a list
plot((x^2,sqrt(x),sin(x)), (x, 0, 1))
///
}}}
{{{id=26|
#You can also combine plots by adding them. This allows me to make each plot a different color.
plot(x^2, (x, 0, pi),color='red')+plot(sqrt(x), (x, 0, pi),color='green')+plot(sin(x), (x, 0, pi))
///
}}}
{{{id=85|
///
}}}
SAGE will give you an exact value, and simplify it when possible.
{{{id=27| cos(pi/2) /// 0 }}} {{{id=28| cos(pi/6) /// 1/2*sqrt(3) }}} {{{id=88| sin(2) /// sin(2) }}} {{{id=89| tan(pi/2) /// Infinity }}} {{{id=29| cos(pi/12).show() ///
}}}
{{{id=93|
///
}}}
Reference sheets allow you to quickly access the most important structures in Sage, you find them here.
{{{id=140| /// }}}