CS107: Computing for Math CS107: Computing for Math and Science - - PowerPoint PPT Presentation

cs107 computing for math cs107 computing for math and
SMART_READER_LITE
LIVE PREVIEW

CS107: Computing for Math CS107: Computing for Math and Science - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS107, Prof. Steinberg, f10

1

Lecture 23

CS107: Computing for Math CS107: Computing for Math and Science and Science

Lecture 24: Maple

slide-2
SLIDE 2

CS107, Prof. Steinberg, f10

2

Lecture 23

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

slide-3
SLIDE 3

CS107, Prof. Steinberg, f10

3

Lecture 23

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

slide-4
SLIDE 4

CS107, Prof. Steinberg, f10

4

Lecture 23

? 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

slide-5
SLIDE 5

CS107, Prof. Steinberg, f10

5

Lecture 23

Numbers Numbers

  • Integers - of any size
  • Real - of any precision

– E.g. Digits := 20;evalf(Pi);

20 3.1415926535897932385

slide-6
SLIDE 6

CS107, Prof. Steinberg, f10

6

Lecture 23

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

slide-7
SLIDE 7

CS107, Prof. Steinberg, f10

7

Lecture 23

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

slide-8
SLIDE 8

CS107, Prof. Steinberg, f10

8

Lecture 23

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

slide-9
SLIDE 9

CS107, Prof. Steinberg, f10

9

Lecture 23

But Compare But Compare

  • a := b + 2;
  • b := 1;
  • a

3

  • b := 2;
  • a

4

  • y := 1;
  • x := y+2;
  • x

3

  • y :=2;
  • x

3

slide-10
SLIDE 10

CS107, Prof. Steinberg, f10

10

Lecture 23

But Compare But Compare

value in a b a := b + 2; b+2 b := 1; 1 a 3 b := 2; 2 a 4 value in x y y := 1; 1 x := y+2; 3 x 3 y :=2; 2 x 3

slide-11
SLIDE 11

CS107, Prof. Steinberg, f10

11

Lecture 23

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

slide-12
SLIDE 12

CS107, Prof. Steinberg, f10

12

Lecture 23

Dittos Dittos

  • %
  • %%
  • %%%
slide-13
SLIDE 13

CS107, Prof. Steinberg, f10

13

Lecture 23

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
  • p(1, a) returns y
  • p(0, a) returns =
  • p(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
  • p(a)[2] is x2-2x+1
slide-14
SLIDE 14

CS107, Prof. Steinberg, f10

14

Lecture 23

Ranges Ranges

  • 1 .. 3 means 1, 2, 3
slide-15
SLIDE 15

CS107, Prof. Steinberg, f10

15

Lecture 23

Sequences Sequences

  • seq(expression, var=range)

seq(j^2, j=2..5) is 4, 9, 16, 25

  • p(expr)
  • p(a*x^2+b*x+c) is a*x^2, b*x, c
  • nops(expr)
  • 2:5

1, 2, 3, 4, 5

slide-16
SLIDE 16

CS107, Prof. Steinberg, f10

16

Lecture 23

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

slide-17
SLIDE 17

CS107, Prof. Steinberg, f10

17

Lecture 23

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

slide-18
SLIDE 18

CS107, Prof. Steinberg, f10

18

Lecture 23

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

slide-19
SLIDE 19

CS107, Prof. Steinberg, f10

19

Lecture 23

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

slide-20
SLIDE 20

CS107, Prof. Steinberg, f10

20

Lecture 23

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

slide-21
SLIDE 21

CS107, Prof. Steinberg, f10

21

Lecture 23

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

slide-22
SLIDE 22

CS107, Prof. Steinberg, f10

22

Lecture 23

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)

slide-23
SLIDE 23

CS107, Prof. Steinberg, f10

23

Lecture 23

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
slide-24
SLIDE 24

CS107, Prof. Steinberg, f10

24

Lecture 23

Programming Programming

  • If

if x > y then z:= z elif x = y then z := 0 else z := y end if

slide-25
SLIDE 25

CS107, Prof. Steinberg, f10

25

Lecture 23

Programming Programming

  • If

if x > y then z:= z elif x = y then z := 0 else z := y

end if

slide-26
SLIDE 26

CS107, Prof. Steinberg, f10

26

Lecture 23

Programming Programming

  • For

for j from 1 by 2 to 9 do x:=x+j; end do;

slide-27
SLIDE 27

CS107, Prof. Steinberg, f10

27

Lecture 23

Programming Programming

  • proc(vars)

end proc

  • read “filename.mpl” to read in definitions

from a file.