{{{id=14| # An example Sage Worksheet for Instructables see: # A line with a "#" at the beginning is a comment, this line is a comment. # The next line is probably the easiest way to get a total. # remember to see the result of the cell you need to evaluate it. This cell evaluates to 1 # blank lines do nothing. Next line prints 1 1 /// 1 }}} {{{id=1| # The next line is probably the easiest or at least quickest way to get a total. 5 + 7 + 22 +33.6 + 99 /// 166.600000000000 }}} {{{id=2| # This is a slightly more indirect way of getting the total # I like it better because the total is also stored in "mytotal" mytotal = 5 + 7 + 22 +33.6 + 99 # the line above does not print, to see the answer type the next line mytotal /// 166.600000000000 }}} {{{id=3| # The expression on the last line is always printed, to print with a line anywhere in the cell use # the word print. The next two lines show this. # in this sheet, normally print only at the end of a cell, but in my other work I usually print a lot. mytotal = 5 + 7 + 22 +33.6 + 99 print mytotal /// 166.600000000000 }}} {{{id=4| # Lets compute a fraction ( or a division problem ) where both the numerator and denominator are sums. # first I will do it the super short way ( 5 + 7 + 22 +33.6 + 99 ) / ( 99.3 + 287 ) /// 0.431271032876003 }}} {{{id=5| # Same problem as above, but doing it in a more complicated way # which I like better, but take your choice # the comments are unnecessary I put them in for you num = 5 + 7 + 22 +33.6 + 99 # the numerator denom = 99.3 + 287 # the denomerator fract = num/denom # the fraction print fract /// 0.431271032876003 }}} {{{id=6| # Lets add some fractions -- this can give some cool # but perhaps some unexpected results: mytotal = ( 1/2 ) + ( 1/3 ) # use () to make sure that the math is done in the order you expect print mytotal # see next cell for comment /// 5/6 }}} {{{id=7| # Sage knows a lot of math, including how to find the common denomanator and # add fractions as fractions, very nice if that is what you want. But you may # just want a number. For this use the function n() like below mytotal = ( 1/2 ) + ( 1/3 ) # actually we do not have to do this because mytotal from the cell above is still around print n( mytotal ) # n() make is a ( decimal ) number -- because n() is a useful function do not use it for something else like n = 2 + 3 /// 0.833333333333333 }}} {{{id=8| # I will not do a lot of calculations in this cell, but will tell you some of the operations you can use in sage. # there are lots more # Common Math Operators and Grouping # operation Sage Example # addition + a = 7 + 3 # subtraction - f = 7 - 3 # multiplication * area = length * width # you need the * "length width" is not multiplication # division / q = 7 / 3 # powers ^ a = 3.14159 * r ^ 2 # grouping () y = ( 2 * ( 5 + 7 ) ) # may be nested /// }}} {{{id=9| # Powers can be cool, lets us the area of a circle a = pi * ( r ^ 2 ) # lets take r = 93.337 a = pi * ( 93.337 ^ 2 ) print n( a ) # couple of thinks to note: Sage knows pi ( it also knows e ) # since pi's digits go on forever it leaves pi in the answer unless you use n() ( try it ) /// 27368.9129591465 }}} {{{id=10| # Continuing with powers, if you solve the equation for the radius given the are you get # r = the square root of ( a/pi ) # there are several ways to do the square root but a cool one is to note that the square root # is the 1/2 or .5 power. Lets take the area as one close to the cell above, we should get # a radius close to the one above r = ( 27000 / pi ) ^ .5 print n( r ) /// 92.7058084855655 }}} {{{id=15| # Need functions? Sage is Trancendental # well at least Sage has Trancendental functions. If you are ready for them # not much explanation is necessary ( except that angles are in radians not degrees ) # sin( x ) # cos( x ) # tan( x ) .... # atan( x ) ..... # log( x ) # ln( x ) # and hundreds ( thousands ) more # here is a nasty calculation done quickly 22. * sin( .22 ) * ( cos( .1 ) ^ 3 ) /// 4.72945480587161 }}} {{{id=11| # There are many more calculations you can do, you should know enough to pull them off now # Sage can do so much more: # calculations with 100s of digits, # equations, # vectors, # matrices, # limits, # integerals ( calculus ), # derivaritives ( calculus ), # complex numbers # Understand all of Sage is a big topic, but just the ability to do simple calculations like this makes it really useful and is not very hard. # Here is one more thing just as a tease. We will plot the area of a circle vs its radius # it is just a bit harder than the above cells. I will not explain it much, you can look it up ( the Sage site or googel SageMath plot ) var( "r" ) # this odd statement makes r a variable a = pi * ( r ^ 2 ) # formula for area show( plot( a, r, 0, 5 ) ) # show a plot from r = 0 to 5 /// }}} {{{id=13| /// }}}