lecture2.mw

> # let's talk about for loops again:
# it is possible to go down in the loop variable

# ie to decrement it.

> for i from 10 by -1 to 1 do
 2^i;

end;

1024

512

256

128

64

32

16

8

4

2

> # or you may jump up in steps of 2:

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

end;

>

1

4

16

64

256

1024

> # what if the bounds don't fit exactly?

> for i from 1 by 2 to 10 do
 2^i;

end;

2

8

32

128

512

> # OK, we stop once we leave the range.

> 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]])

> for i to 7 do
 for j to 7 do

   A[i,j] := i + j;

 end;

end;

> A;

Matrix([[2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6, 7, 8, 9, 10, 11], [6, 7, 8, 9, 10, 11, 12], [7, 8, 9, 10, 11, 12, 13], [8, 9, 10, 11, 12, 13, 14]])

> # let's compute the sum of all matrix entries:

> s := 0;

s := 0

> for i to 7 do for j to 7 do s := s + A[i,j]; end; end;

> s;

392

> # scalar multiply the matrix, i.e.
# multiply each entry of the matrix by the same value:

> 2*A;

Matrix([[4, 6, 8, 10, 12, 14, 16], [6, 8, 10, 12, 14, 16, 18], [8, 10, 12, 14, 16, 18, 20], [10, 12, 14, 16, 18, 20, 22], [12, 14, 16, 18, 20, 22, 24], [14, 16, 18, 20, 22, 24, 26], [16, 18, 20, 22, 2...

> # guess what is the sum of entries of this matrix?
# yes, it is twice of what it was before:

> B := %:

> t := 0;

t := 0

> for i to 7 do for j to 7 do t := t + B[i,j]; end; end;

> t;

784

> 2*s;

784

> # so, can you compute the factorial without using Maple's ! function?

> # here we go:

> s := 1;

s := 1

> for i from 1 to 6 do s := s * i; end;

s := 1

s := 2

s := 6

s := 24

s := 120

s := 720

> s;

720

> # or downward:

> s := 1;

s := 1

> for i from 6 by -1 to 1 do s := s * i; end;

s := 6

s := 30

s := 120

s := 360

s := 720

s := 720

> s;

720

> # new topic: graphics
# let's clear the memory first:

> restart;

> plot(sin(t),t);

[Plot]

> plot(sin(t),t=-10*Pi..10*Pi);

[Plot]

> # you can click on the plot and resize the window, for example.

> # two plots in one:

> plot([sin(t), cos(t)], t=-Pi..Pi);

[Plot]

> #another way to define a function

> f := x -> exp(-x^2)*sin(Pi*x^3);

f := proc (x) options operator, arrow; exp(-x^2)*sin(Pi*x^3) end proc

> plot(f,-2..2);

[Plot]

> # you can draw the plot over the full x-axis to infinity:

> plot(f, 0..infinity);

[Plot]

> # OK, let's plot the sin up to infinity:

> plot(sin(t),t=0..infinity);

[Plot]

> # multiple plots in one:

> plot([f(x), exp(-x^2),-exp(-x^2)],x=-2..2);

[Plot]

> # for folks who use unix/linux etc.
# you can export Maple plots in PostScript files

> plotsetup(PostScript, plotoutput="test.eps",

> plotoptions="portrait,noborder,height=3in,width=4.5in"):

> plot([f(x), exp(-x^2),-exp(-x^2)],x=-2..2);

> # now a file "test.eps" has been created.
# to get back to normal mode use

> plotsetup(default);

> # a parametric x/y - plot:
# notice: the only difference from

# the previous "two plots in one"

# is the position of the

# closing square bracket:

> plot([sin(t), cos(t), t=-Pi..Pi]);

[Plot]

> plot([sin(2*t),cos(3*t+Pi/2),t=0..10*Pi]);

[Plot]

> # you may play around with the constants !
# next, a polar coordinate plot:

> plot([sin(4*x),x,x=0..2*Pi],coords=polar,thickness=3);

[Plot]

> plot([sin(30*x),x,x=0..2*Pi],coords=polar);

[Plot]

> # point plot:

> plot(sin,0..Pi, scaling=constrained, style=point,symbol=circle,symbolsize=20);

[Plot]

> # another way is creating the list first

> l := [[ n, sin(n*Pi/20)] $n=0..20]:

> plot(l, x=0..21, style=point,symbol=circle);

[Plot]

> # we need to load a "package"

> with(plots):
Warning, the previous binding of the name arrow has been removed and it now has an assigned value

Error, missing operator or `;`

> # implicit plots of algebraic curves

> implicitplot(y^2=x*(x+1)*(2*x+1)/6,x=-3..3,y=-4..4);

[Plot]

> # it does not look nice.
# field plots

> fieldplot([cos(x),cos(y)],x=-2*Pi..2*Pi,y=-2*Pi..2*Pi, arrows=SLIM, grid=[11,11], axes=boxed);

>

[Plot]

> # 3D plots:

> plot3d(sin(x)*cos(y),x=-Pi..Pi,y=-Pi..Pi);

[Plot]

> # you can click on the plot and strech it around to make it look nice
# we want to plot the quadratic surface x^2+y^2-z^2=1

# this is a "hyperboloid of one sheet"

#  also known from atomic power plants.

# we first try to solve it for z, i.e.

# z = sqrt(x^2+y^2-1)

> plot3d(sqrt(x^2+y^2-1),x=-3..3,y=-3..3);

[Plot]

> # it does not look good.
# to improve, we need to choose better coordinates

# we need to load another packages, however:

> with(plottools);

Warning, the assigned name arrow now has a global binding

[arc, arrow, circle, cone, cuboid, curve, cutin, cutout, cylinder, disk, dodecahedron, ellipse, ellipticArc, hemisphere, hexahedron, homothety, hyperbola, icosahedron, line, octahedron, parallelepiped...[arc, arrow, circle, cone, cuboid, curve, cutin, cutout, cylinder, disk, dodecahedron, ellipse, ellipticArc, hemisphere, hexahedron, homothety, hyperbola, icosahedron, line, octahedron, parallelepiped...[arc, arrow, circle, cone, cuboid, curve, cutin, cutout, cylinder, disk, dodecahedron, ellipse, ellipticArc, hemisphere, hexahedron, homothety, hyperbola, icosahedron, line, octahedron, parallelepiped...[arc, arrow, circle, cone, cuboid, curve, cutin, cutout, cylinder, disk, dodecahedron, ellipse, ellipticArc, hemisphere, hexahedron, homothety, hyperbola, icosahedron, line, octahedron, parallelepiped...

> cylinderplot(1,theta=0..2*Pi,z=-1..1);

[Plot]

> # OK, let's do the math first,
# if x^2 + y^2 = 1+z^2

# and for a given z we have an x/y plane

# where the ray with angle theta and length r

# projects onto the x and y axis with x and y, respectively,

# by Pythagoras, we have x^2 + y^2 = r^2, i.e.

# r = sqrt(x^2+y^2) = sqrt(1 + z^2) by the above.

# hence

> cylinderplot(sqrt(1+z^2),theta=0..2*Pi,z=-1..1);

[Plot]

> # aahh, this looks much better!
# what else is there? try sphereplot, for example:

> sphereplot(1,theta=0..2*Pi,phi=0..Pi);

[Plot]

> # lets make the sphere wobbly:

> sphereplot(10+sin(10*theta)+cos(10*phi),theta=0..2*Pi,phi=0..Pi);

[Plot]

> # OK, now for another cylinderplot
# this time we plot the real quadric cone

# with equation x^2+y^2=z^2 or x^2+y^2-z^2 = 0

> cylinderplot(z,theta=0..2*Pi,z=-1..1);

[Plot]

> # elliptic paraboloid: x^2+y^2+z = 0
# r = (x^2+y^2), so r = sqrt(-z)

> cylinderplot(sqrt(-z),theta=0..2*Pi,z=-1..1);

[Plot]

> # Hyperbolic paraboloid x^2-y^2+z = 0
# back to ordinary plots z in terms of x and y:

> plot3d(x^2-y^2,x=-1..1,y=-1..1);

[Plot]

> # for more info on quadric surfaces, see
# http://www.math.umn.edu/~rogness/quadrics/index.shtml

> HOMEWORK PROBLEMS:

> # hw 1   a) plot the Hyperboloid of one sheet x^2+y^2-z^2=1
#        b) plot the Hyperboloid of two sheets x^2+y^2-z^2=-1

>

> # hw 2 here is a plot. Find the command to recreate it:


[Plot]

>

> # hw 3
# Plot the first 5 Chebyshev polynomials in one plot.

# (you may want to create a list

# of the polynomials first)


> # hw 4
# The "tribonacci" numbers are defined as

# f(1) = 1, f(2) = 1, f(3) = 1,

# f(n) = f(n-1) + f(n-2) + f(n-3) for n >= 4.

# Compute the first 20 tribonacci numbers.

# compute the ratio f(i)/f(i-1) for i <= 20.

# Does it converge?