> | # this is a comment line.
if you read this, then you have successfully started your session, congratulations!!! |
> | # I recommend you use ctrl-3 to adjust the font size. Better? yeah! |
> | 1+1; |
> | # hey, we can compute 1+1, great. |
> | %+1; |
> | # the % stands for the latest result, so you don't have to type it again, that's nice |
> | %+1; |
> | # note that = is test for equality, not assignment |
> | x = 3; |
> | # the := is used to assign something to something
# in this case, we assign the value 3 to x. |
> | x := 3; |
> | # now x has the value 3; |
> | x; |
> | # test for equality: |
> | x = 3; |
> | # how can we get rid of the assignment?
# well we need to unassign it, here is how this is done: |
> | x := 'x'; |
> | x; |
> | # that's right, now x is a symbol without a value. |
> | x := 4; |
> | # there's another way to get rid of all assigned variables,
# it is a complete memory clear: |
> | restart; |
> | x; |
> | # OK, now everything is fresh again.
# let's move on. |
> | #guess what, we are going to do some fractions, yeah! |
> | 1/3; |
> | 0.333333; |
> | # 1/3 and 0.333333 is not the same thing, so get used to it! |
> | # however, if you want to treat 1/3 as floating point number,
# you can give evalf on it. |
> | evalf(1/3); |
> | # you can have more digits if you like: |
> | evalf(1/3,100); |
> | # Maple treats mathematical quantities exact, i.e. the way mathematicians
# would treat them. For applications, you may want to switch to # floating points. As we have seen, you can do that. |
> | # another example is that of factorials.
# n! (read n factorial) is defined as the product of all # numbers from 1 to n. For example 5! = 5 * 4 * 3 * 2 * 1 = 120: |
> | 5!; |
> | # Let's do 6!= 6*5*4*3*2*1 = 6 * 5! = 6 * 120 = 720 |
> | 6!; |
> | # now let's do double factorials |
> | 6!!; |
> | # wow, what was that?
# That was 6!! = 720! = 720 * 719 * 718 * ... * 2 * 1, # which of course is a very large number. Notice that Maple gives you # the exact answer, even though you might not have expected such a large output. # This is typical for Maple. # Now if you like, you can turn that number into a floating point. # Here, the % notation comes in handy. |
> | evalf(%); |
> | # Let's give it a name using the assignment operator.
# Notice that it is now already the next to last result, so we # use the double percent notation: |
> | a := %%: # no output this time |
> | evalf(a); |
> | evalf(a,100); |
> | # this was a floating point number with 100 decimal digits precision. |
> | # Do you know what Pi is? |
> | Pi; |
> | # hmmm, maybe you were expecting 3.14...
# here we go: |
> | evalf(Pi); |
> | #want more digits? Here we go: |
> | evalf(Pi,100); |
> | # OK, I hope you see the difference between a symbolic system and a numeric system.
# Maple is symbolic, Matlab is numeric, for example. |
> | # let's move on. |
> |
> |
> |
> |
> | #uhh adding fractions, let Maple do it: |
> | 1/3+1/5; |
> | # that was correct! 1/3 + 1/5 = 5/15 + 3/15 = 8/15. |
> | 1/%; |
> | # now we want powers, this is how we do it: |
> | 2^2; |
> | #more powers: |
> | 2^3; |
> | 2^4; |
> | # want more? why not have a little loop? guess what, Maple starts counting from 1 (I repeat: one) |
> | for i to 10 do 2^i; end; |
> | # you can do whatever you want inside the loop. |
> | # lets say you want to compute 2^i - 1 |
> | for i to 10 do 2^i - 1; end; |
> | # now we want to start the loop from i=2.
# this is how we do it. |
> | for i from 2 to 10 do 2^i; end; |
> | # notice that the output starts with 2^2 = 4 rather that 2^1 = 2. |
> | # I want a sequence. Notice the assign operator := |
> | seq1 := 1,2,3; |
> | #another one: |
> | seq2 := 4,5,6; |
> | #let's concatenate the sequences: |
> | seq3 := seq1,seq2; |
> | #let's access elements of the sequence, for example the third: |
> | seq3[3]; |
> | # uhh, don't access the zeroth element, this is Maple and not C, OK!? |
> | seq3[0]; |
Error, invalid subscript selector
> | #let's make the sequence longer: |
> | seq3 := seq3,7,8,9; |
> | #how long is it? |
> | length(seq3); |
> | # well, that can't be right. In fact you must use nops and square brackets, don't ask why at this point |
> | nops([seq3]); |
> | # OK let's add more stuff: |
> | seq3 := seq3,a,b,c; |
> | nops([seq3]); |
> | # you have noticed that we always have a semicolon at the end of a command. |
> | # we can also have a colon. That would then suppress output, as follows: |
> | seq3:=seq3,d,e,f: |
> | # want to see seq3 ?, here you go: |
> | seq3; |
> | # lets make a sequence of the first few powers of 2: |
> | S := seq( 2^i, i=1..10); |
> | # we could also do it this way: |
> | S := seq( 2^i, i={1,2,3,4,5,6,7,8,9,10}); |
> | # let's do something crazy |
> | S := seq(i^2, i={x,y,z}); |
> | # these are just symbolic terms, we cannot do much with them now. |
> | # notice that we have overwritten S each time |
> | # in the last few examples. That is OK, as long as we don't need the old content any more. |
> | %[2]; |
> | # BTW, that was the second element of S. |
> | # now we want to have a set: |
> | {1,2,3,4,5}; |
> | #let's give it a name: |
> | S := %; |
> | # another set: |
> | T := {2,4,6,8}; |
> | #union, minus, intersect: |
> | S union T; |
> | S minus T; |
> | S intersect T; |
> | #is 2 a member of S? |
> | member(2, S); |
> | # OK, and at which position? |
> | member(2, S, 'position'); |
> | position; |
> | #OK, second position of S. |
> | S[2]; |
> | for i to 10 do
2^i; end; |
> |
> |
> | for i from 0 to 10 do
2^i; end; |
> |
> | for i from 0 to 10 do
printf("%d ", 2^i); end; |
> |
1 2 4 8 16 32 64 128 256 512 1024
> | A := Matrix(7,7); |
> | A[1,1]; |
> | A[7,7]; |
> | A[8,8]; |
Error, Matrix index out of range
> | for i from 1 to 7 do
for j from 1 to 7 do A[i,j] := 1/(i+j); end; end; |
> | A; |
> | A[7,7]; |
> | # let's learn how to use lists! |
> | # a list is just a sequence put into square brackets |
> | L := [1,2,3,4,5]; |
> | # access goes from 1 to 5: |
> | L[1]; |
> | L[2]; |
> | L[3]; |
> | L[4]; |
> | L[5]; |
> | # we can change an entry in the list using the assignment operator := |
> | L[1] := 2; |
> | L; |
> | # how can we append?
# Our first guess is this |
> | L := [L,6]; |
> | # hmm, that was't what we wanted. |
> | # here is the solution. Let's first restore L: |
> | L := [1,2,3,4,5]; |
> | # its a bit tricky, we need to get the contents
# of the list first, and then add one more entry and # make it a list, such as |
> | op(L); #the content of the list, as a sequence: |
> | [op(L),6]; #now we add an element and make another list: |
> | # we can have a loop create a list as long as we wish: |
> | L := []; # start with an empty list |
> | for i from 1 to 10 do
L := [ op(L), i ]: end; |
> | # uhh, what was that.
# That was one line of output for each execution of the # loop body. |
> | # Maybe that is too much output for you.
# turning the ";" to a ":" after the end prohibits output: |
> | L := []: for i from 1 to 10 do
L := [ op(L), i ]: end: |
> | L; |
> | # there is a quick way to generate repeated elements in lists
# for example 0$10 would generate 10 entries 0 |
> | L := [3$7]; # seven 3's |
> |
> | HOMEWORK PROBLEMS: |
> | # My big advice is that you open up a word file next to the
# maple window and that you copy past stuff into a word document # once you are done with it. # Otherwise, you may loose data if the Maple session # happens to crash |
> | # hw 1 |
> | the Fibonacci sequence
1, 1, 2, 3, 5, 8, 13, 21, ... is defined as follows: f[1] = 1, f[2] = 1, f[i] = f[i-2] + f[i-1] for i >= 3. Create a list with the first 30 Fibonacci numbers. Use a for loop to create the list. |
> | # hw 2
# the Chebyshev Polynomial T[i] is defined as # T[0] = 1, T[1] = x and T[i] = 2*x*T[i-1] - T[i-2] for i >= 2 # create a list holding the first 10 Chebychev polynomials. # Attention, you cannot access the zero's element of lists, # hence you need to store T[i] in T[i+1]. |
> | # hw 3: |
> | # Pascal's Triangle is the following array of numbers: |
> | # 1
# 1 1 # 1 2 1 # 1 3 3 1 # 1 4 6 4 1 # 1 5 10 10 5 1 # 1 6 15 20 15 6 1 # etc. |
> | # Figure out what is going on. |
> | # After that, have a little Maple program |
> | # compute Pascal's Triangle of 15 rows |
> | # use a Matrix to store the numbers. |
> | # The unused spots in the matrix shall be zero. |
> | # Remark: to copy the matrix into a word document,
# you may want to "right click" on the Maple matrix and # export it to a "Tab Delimited" file. # In word you go file .. insert... and insert that same file. # Notice: If you cannot find your file the reason might # be that Word is looking for "doc" files, # whereas your file is only a simple text file. # you may want to change the search option to "all files" # in word. You should then see your exported file. OK? |
> |
> | # hw 3 |
> | # Read Kraft's notes, Worksheet #1 |
> |