| > | # 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; |
| > | # or you may jump up in steps of 2: |
| > | for i from 0 by 2 to 10 do
2^i; end; |
| > |
| > | # what if the bounds don't fit exactly? |
| > | for i from 1 by 2 to 10 do
2^i; end; |
| > | # OK, we stop once we leave the range. |
| > | A := Matrix(7,7); |
| > | for i to 7 do
for j to 7 do A[i,j] := i + j; end; end; |
| > | A; |
| > | # let's compute the sum of all matrix entries: |
| > | s := 0; |
| > | for i to 7 do for j to 7 do s := s + A[i,j]; end; end; |
| > | s; |
| > | # scalar multiply the matrix, i.e.
# multiply each entry of the matrix by the same value: |
| > | 2*A; |
| > | # guess what is the sum of entries of this matrix?
# yes, it is twice of what it was before: |
| > | B := %: |
| > | t := 0; |
| > | for i to 7 do for j to 7 do t := t + B[i,j]; end; end; |
| > | t; |
| > | 2*s; |
| > | # so, can you compute the factorial without using Maple's ! function? |
| > | # here we go: |
| > | s := 1; |
| > | for i from 1 to 6 do s := s * i; end; |
| > | s; |
| > | # or downward: |
| > | s := 1; |
| > | for i from 6 by -1 to 1 do s := s * i; end; |
| > | s; |
| > | # new topic: graphics
# let's clear the memory first: |
| > | restart; |
| > | plot(sin(t),t); |
![[Plot]](images/lecture2_46.gif)
| > | plot(sin(t),t=-10*Pi..10*Pi); |
![[Plot]](images/lecture2_47.gif)
| > | # 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]](images/lecture2_48.gif)
| > | #another way to define a function |
| > | f := x -> exp(-x^2)*sin(Pi*x^3); |
| > | plot(f,-2..2); |
![[Plot]](images/lecture2_50.gif)
| > | # you can draw the plot over the full x-axis to infinity: |
| > | plot(f, 0..infinity); |
![[Plot]](images/lecture2_51.gif)
| > | # OK, let's plot the sin up to infinity: |
| > | plot(sin(t),t=0..infinity); |
![[Plot]](images/lecture2_52.gif)
| > | # multiple plots in one: |
| > | plot([f(x), exp(-x^2),-exp(-x^2)],x=-2..2);
|
![[Plot]](images/lecture2_53.gif)
| > | # 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]](images/lecture2_54.gif)
| > | plot([sin(2*t),cos(3*t+Pi/2),t=0..10*Pi]); |
![[Plot]](images/lecture2_55.gif)
| > | # 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]](images/lecture2_56.gif)
| > | plot([sin(30*x),x,x=0..2*Pi],coords=polar); |
![[Plot]](images/lecture2_57.gif)
| > | # point plot: |
| > | plot(sin,0..Pi, scaling=constrained, style=point,symbol=circle,symbolsize=20); |
![[Plot]](images/lecture2_58.gif)
| > | # 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]](images/lecture2_59.gif)
| > | # 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]](images/lecture2_60.gif)
| > | # 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]](images/lecture2_61.gif)
| > | # 3D plots:
|
| > | plot3d(sin(x)*cos(y),x=-Pi..Pi,y=-Pi..Pi); |
![[Plot]](images/lecture2_62.gif)
| > | # 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]](images/lecture2_63.gif)
| > | # 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
![]()
![]()
![]()
| > | cylinderplot(1,theta=0..2*Pi,z=-1..1);
|
![[Plot]](images/lecture2_68.gif)
| > | # 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]](images/lecture2_69.gif)
| > | # aahh, this looks much better!
# what else is there? try sphereplot, for example: |
| > | sphereplot(1,theta=0..2*Pi,phi=0..Pi);
|
![[Plot]](images/lecture2_70.gif)
| > | # lets make the sphere wobbly: |
| > | sphereplot(10+sin(10*theta)+cos(10*phi),theta=0..2*Pi,phi=0..Pi);
|
![[Plot]](images/lecture2_71.gif)
| > | # 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]](images/lecture2_72.gif)
| > | # 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]](images/lecture2_73.gif)
| > | # 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]](images/lecture2_74.gif)
| > | # 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]](images/lecture2_75.gif)
| > |
| > | # 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? |