CS107: Computing for Math CS107: Computing for Math and Science and Science Lecture 24: Maple CS107, Prof. Steinberg, f10 Lecture 23 1
Maple Maple • Running maple via internet: Like matlab but – Use alfalfa.rutgers.edu in place of remus .rutgers.edu – Use xmaple in place of matlab – To exit: quit CS107, Prof. Steinberg, f10 Lecture 23 2
Running Maple Running Maple • Or run Maple in a computer lab • Or buy it and run on your own computer: • Students may purchase their own Maple Student Edition for $59 by going to: http://webstore.maplesoft.com – Requires proof of student status. – Choose a Category: Student – Select your Location: US – Promotional Code: RUTGERS72 – Click: Start Shopping CS107, Prof. Steinberg, f10 Lecture 23 3
? To access help system ? To access help system • EG ?+ or ?factor • Or click on ? Icon at top right of window • Calling Sequence, Params, Description, Examples CS107, Prof. Steinberg, f10 Lecture 23 4
Numbers Numbers • Integers - of any size • Real - of any precision – E.g. Digits := 20;evalf(Pi); 20 3.1415926535897932385 CS107, Prof. Steinberg, f10 Lecture 23 5
Variables Variables • Use := for assignment x:=1+2; • But variables don’t have to have values x:= a + b; • So Maple combines variables – as in program: store a value – As in math: represent an unspecified value • = means an equation, not assignment x = a + b does not change value of x – Can even do x := c = a + b CS107, Prof. Steinberg, f10 Lecture 23 6
Variables Variables • To “clear” a variable means to remove any value it might have • Clear all variables with restart; • Clear, e.g., x with unassign(‘x’); Note ‘ ‘ CS107, Prof. Steinberg, f10 Lecture 23 7
Variables Variables > a:=b+2; a := b + 2 > a; b + 2 > b:=3; b := 3 > a; 5 > b:=4; b := 4 > a; 6 > unassign('b' ); > a; b + 2 CS107, Prof. Steinberg, f10 Lecture 23 8
But Compare But Compare • a := b + 2; • y := 1; • b := 1; • x := y+2; • a • x 3 3 • b := 2; • y :=2; • a • x 4 3 CS107, Prof. Steinberg, f10 Lecture 23 9
But Compare But Compare value in a b value in x y a := b + 2; b+2 y := 1; 1 b := 1; 1 x := y+2; 3 a x 3 3 b := 2; 2 y :=2; 2 a x 4 3 CS107, Prof. Steinberg, f10 Lecture 23 10
Evaluating variables Evaluating variables • Top-level variables (not local to a proc) are evaluated all the way until no variable has a value – Eg i := h; h := f+g; f := 3; sets i to 3+g CS107, Prof. Steinberg, f10 Lecture 23 11
Dittos Dittos • % • %% • %%% CS107, Prof. Steinberg, f10 Lecture 23 12
Symbolic expressions Symbolic expressions Value of a variable can be an expression • a := y= x^2-2*x+1; Maple has functions that operate on expressions: • factor(a); returns y = (x - 1) 2 The function op returns pieces of an expression • op(1, a) returns y op(0, a) returns = op(a) returns y, x^2-2x+1 A sequence is written with commas a, b, c • If s is a sequence, s[j] is j’th element of s • op(a)[2] is x 2 -2x+1 CS107, Prof. Steinberg, f10 Lecture 23 13
Ranges Ranges • 1 .. 3 means 1, 2, 3 CS107, Prof. Steinberg, f10 Lecture 23 14
Sequences Sequences seq(expression, var=range) • seq(j^2, j=2..5) is 4, 9, 16, 25 op(expr) • op(a*x^2+b*x+c) is a*x^2, b*x, c nops(expr) • 2:5 • 1, 2, 3, 4, 5 CS107, Prof. Steinberg, f10 Lecture 23 15
Lists & Sets Lists & Sets • Lists: [ seq ] • Sets: { seq } like lists but no repetition or order > evalb([2, 3, 3] = [2, 3]); false > evalb({2, 3, 3} = {2, 3}); true > evalb([3, 2] = [2, 3]); false > evalb({2, 3} = {2, 3}); true CS107, Prof. Steinberg, f10 Lecture 23 16
Solving Equations Solving Equations • solve(x=2*a^2/b, b); • solve(a*x^2+b*x+c=0, x) • solve({a*x+b*y=c,d*x+e*y=f},{x,y}); • Assign s = solve(...); assign(s); Assigns solution values to variables CS107, Prof. Steinberg, f10 Lecture 23 17
Functions Functions • Expression vs function x^2+y^2 is an expression - an expression is a computation or an equation (x,y)->x^2+y^2 is a function - a function is a mapping from 1 or more arguments to an expression CS107, Prof. Steinberg, f10 Lecture 23 18
apply apply • apply(function, arg) • Result is an expression apply(sqrt, 4) 2 apply(foo, a, b, c) foo(a, b, c) f:= (x)-> b+2*x^3 apply(f, a) b+2*a^3 CS107, Prof. Steinberg, f10 Lecture 23 19
unapply unapply • unapply(expression, variables) Result is a function from variables to expression f:= unapply(b + 2*x^3, x) x->b+2*x^3 f(d) b+2*d^3 CS107, Prof. Steinberg, f10 Lecture 23 20
Functions as data Functions as data • Stored as value of a variable f := x-> x^2+2; f(3); • Used as arguments and values of functions unapply(x^2+2, x); D(f); map(f, [2, 4, 5]) CS107, Prof. Steinberg, f10 Lecture 23 21
Differentiating Differentiating • D(f) is f’, I.e. a function – E.g. D(cos) is sin • diff(a,x) is derivative of expression a with respect to x – E.g., diff(cos(x), x) is sin(x) CS107, Prof. Steinberg, f10 Lecture 23 22
Finding maxima Finding maxima • f := x -> sin(x) * cos(x) • D(f) x->cos(x)^2-sin(x)^2 • solve(D(f)(x)=0,x) 1/4 pi, 3/4 pi • See cannon.mw CS107, Prof. Steinberg, f10 Lecture 23 23
Programming Programming • If if x > y then z:= z elif x = y then z := 0 else z := y end if CS107, Prof. Steinberg, f10 Lecture 23 24
Programming Programming • If if x > y then z:= z elif x = y then z := 0 else z := y end if CS107, Prof. Steinberg, f10 Lecture 23 25
Programming Programming • For for j from 1 by 2 to 9 do x:=x+j; end do; CS107, Prof. Steinberg, f10 Lecture 23 26
Programming Programming • proc(vars) end proc • read “filename.mpl” to read in definitions from a file. CS107, Prof. Steinberg, f10 Lecture 23 27
Recommend
More recommend