lecture1.mw

> # 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;

2

> # hey, we can compute 1+1, great.

> %+1;

3

> # the % stands for the latest result, so you don't have to type it again, that's nice

> %+1;

4

> # note that = is test for equality, not assignment

> x = 3;

x = 3

> # the := is used to assign something to something
# in this case, we assign the value 3 to x.

> x := 3;

x := 3

> # now x has the value 3;

> x;

3

> # test for equality:

> x = 3;

3 = 3

> # how can we get rid of the assignment?
# well we need to unassign it, here is how this is done:

> x := 'x';

x := x

> x;

x

> # that's right, now x is a symbol without a value.

> x := 4;

x := 4

> # there's another way to get rid of all assigned variables,
# it is a complete memory clear:

> restart;

> x;

x

> # OK, now everything is fresh again.
# let's move on.

> #guess what, we are going to do some fractions, yeah!

> 1/3;

1/3

> 0.333333;

.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);

.3333333333

> # you can have more digits if you like:

> evalf(1/3,100);

.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333

> # 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!;

120

> # Let's do 6!= 6*5*4*3*2*1 = 6 * 5! = 6 * 120 = 720

> 6!;

720

> # now let's do double factorials

> 6!!;

26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...26012189435657951002049032270810436111915218750169457857275418378508356311569473822406785779581304570826199205758922472595366415651620520158737919845877408325291052446903888118841237643411919510455053...

> # 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(%);

0.2601218944e1747

> # 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);

0.2601218944e1747

> evalf(a,100);

0.2601218943565795100204903227081043611191521875016945785727541837850835631156947382240678577958130457e17470.2601218943565795100204903227081043611191521875016945785727541837850835631156947382240678577958130457e1747

> # this was a floating point number with 100 decimal digits precision.

> # Do you know what Pi is?

> Pi;

Pi

> # hmmm, maybe you were expecting 3.14...
# here we go:

> evalf(Pi);

3.141592654

> #want more digits? Here we go:

> evalf(Pi,100);

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

> # 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;

8/15

> # that was correct! 1/3 + 1/5 = 5/15 + 3/15 = 8/15.

> 1/%;

15/8

> # now we want powers, this is how we do it:

> 2^2;

4

> #more powers:

> 2^3;

8

> 2^4;

16

> # 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;

2

4

8

16

32

64

128

256

512

1024

> # 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;

1

3

7

15

31

63

127

255

511

1023

> # 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;

4

8

16

32

64

128

256

512

1024

> # 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;

seq1 := 1, 2, 3

> #another one:

> seq2 := 4,5,6;

seq2 := 4, 5, 6

> #let's concatenate the sequences:

> seq3 := seq1,seq2;

seq3 := 1, 2, 3, 4, 5, 6

> #let's access elements of the sequence, for example the third:

> seq3[3];

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;

seq3 := 1, 2, 3, 4, 5, 6, 7, 8, 9

> #how long is it?

> length(seq3);

19

> # well, that can't be right. In fact you must use nops and square brackets, don't ask why at this point

> nops([seq3]);

9

> # OK let's add more stuff:

> seq3 := seq3,a,b,c;

seq3 := 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c

> nops([seq3]);

12

> # 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;

1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

> # lets make a sequence of the first few powers of 2:

> S := seq( 2^i, i=1..10);

S := 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024

> # we could also do it this way:

> S := seq( 2^i, i={1,2,3,4,5,6,7,8,9,10});

S := 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024

> # let's do something crazy

> S := seq(i^2, i={x,y,z});

S := x^2, y^2, z^2

> # 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];

y^2

> # BTW, that was the second element of S.

> # now we want to have a set:

> {1,2,3,4,5};

{1, 2, 3, 4, 5}

> #let's give it a name:

> S := %;

S := {1, 2, 3, 4, 5}

> # another set:

> T := {2,4,6,8};

T := {2, 4, 6, 8}

> #union, minus, intersect:

> S union T;

{1, 2, 3, 4, 5, 6, 8}

> S minus T;

{1, 3, 5}

> S intersect T;

{2, 4}

> #is 2 a member of S?

> member(2, S);

true

> # OK, and at which position?

> member(2, S, 'position');

true

> position;

2

> #OK, second position of S.

> S[2];

2

> for i to 10 do
 2^i;

end;

>

>

2

4

8

16

32

64

128

256

512

1024

> for i from 0 to 10 do
 2^i;

end;

>

1

2

4

8

16

32

64

128

256

512

1024

> 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 := Matrix([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])

> A[1,1];

0

> A[7,7];

0

> 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;

Matrix([[1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8], [1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9], [1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10], [1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11], [1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12], [...

> A[7,7];

1/14

> # let's learn how to use lists!

> # a list is just a sequence put into square brackets

> L := [1,2,3,4,5];

L := [1, 2, 3, 4, 5]

> # access goes from 1 to 5:

> L[1];

1

> L[2];

2

> L[3];

3

> L[4];

4

> L[5];

5

> # we can change an entry in the list using the assignment operator :=

> L[1] := 2;

L[1] := 2

> L;

[2, 2, 3, 4, 5]

> # how can we append?
# Our first guess is this

> L := [L,6];

L := [[1, 2, 3, 4, 5], 6]

> # hmm, that was't what we wanted.

> # here is the solution. Let's first restore L:

> L := [1,2,3,4,5];

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:

1, 2, 3, 4, 5

> [op(L),6]; #now we add an element and make another list:

[1, 2, 3, 4, 5, 6]

> # we can have a loop create a list as long as we wish:

> L := [];  # start with an empty list

L := []

> for i from 1 to 10 do
 L := [ op(L), i ]:

end;

L := [1]

L := [1, 2]

L := [1, 2, 3]

L := [1, 2, 3, 4]

L := [1, 2, 3, 4, 5]

L := [1, 2, 3, 4, 5, 6]

L := [1, 2, 3, 4, 5, 6, 7]

L := [1, 2, 3, 4, 5, 6, 7, 8]

L := [1, 2, 3, 4, 5, 6, 7, 8, 9]

L := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> # 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;

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> # 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

L := [3, 3, 3, 3, 3, 3, 3]

>

> 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

>