CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer...

19
CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is given to CS121 Fall 2008 staff and students to use and reproduce these notes for their own use.

Transcript of CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer...

Page 1: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

CS 121 Engineering Computation Lab Lab 3

Department of Computer ScienceDrexel University

Summer 2009©By the author. All rights reserved. Permission is given to CS121 Fall 2008 staff and students to use and reproduce these notes for their own use.

Page 2: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Your class instructor and TA

• Instructor for this section:• Office:• Email:• Your TAs are:

Page 3: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Week 2 retrospective

• Lab 2• Limited Precision Arithmetic• Data structure, functions

• Quiz 2

• Getting stuck on problems during the quizzes? – Use the Cyber Learning Center (UC147) – Use the discussion board for short questions

Page 4: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Lab 3

• Overview

• Part 1 Scripts (ch. 4 readings)• Assignments (function)

• Part 2 User defined functions

Page 5: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Assignments

• Assignments are of the formname := expression

where a is a variable and n can be an expression• (discussed in lab 2)• Name can be a single letter (upper-/lower- case

sensitive), or a combination of a letter followed by letters and numbers and underscores.

A a a1 expr I eqn1 my_grade largestScore• Some names are reserved by Maple and cannot be used

as something to assign to:

Pi for I end quit

Page 6: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Demo of Assignments• p := x^2

x^2• p

x^2• p - 1=0

x ^ 2-1 = 0

• unassign(‘p’) • p

p• p-1=0

p-1=0• p does not stand for “x^2” any more.

Page 7: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Scripts

• What is a script?– A script is a sequence of operations. Usually, the later

operations use the results of the earlier ones.

– Labels or assignments are used to make the references convenient.

• How are scripts useful?– Scripts are useful when you expect to solve a series

of similar problems.

– Reuse: cut, paste, edit, re-execute.

Page 8: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Pointers on Script Use

• Rather than use the clickable interface, it’s better to lay out the operations textually one line at a time.

• You can create a script using only clickable operations if you don't need to change the operations that are specified by pop-up menu.

• Use assignments and labels to refer to results of previous steps.

• Place “editable quantities” as assignments at the beginning of the script, rather than scattering the editing task all over the script (“I forget, what do I need to change?”)

Page 9: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Demo of Script Use

• Demo the sheep problem. Compared to the script given in section 4.5, we’re replacing the clickable operations with the function version of the solve, eval, and plot operations.

• This solves the problem forsheepEqn :=N = 220/(1+10*.83^t)popSize := 80

• Copy region• Edit

sheepEqn := N = 330/(1+10*.79^t);popSize := 85;

• Re-execute edited region

Page 10: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

User Defined Functions

• Maple users can define new functions

• That is one of the key features that makes Maple less like a calculator and more like a programming language

Page 11: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

How to define your own function

•Through the clickable interface•The general form is from the f:=a->y in the Expression palette

•User-defined function textually•function name:= input name-> expression involving inputs

•The ( ....) - > .....defines the function.

•The function name := gives the function a name

Page 12: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Usage of User Defined Functions

•Format: function name (values for inputs)

•Example: f := (x) -> sqrt(x) + 1 f(9)

evaluates to 4 as a result.

•Example: g := (a, b) -> (a+b)/2 g(3.5, 6.5)

evaluates to 5 as a result.

Page 13: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Demo of User Defined Functions

• Define three functions t, s and p

• t := n -> 1/n*sin(n*Pi*x);

• s(m) = the sum of t(i)

• s := m -> sum(t(2^i), i=1..m);

• p(m) = produces a plot of s(m) for x between 0 and 4

• p := m -> plot(s(m), x=0..4)

• Here is one example of its use:

• p(1);

Page 14: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Demo of sequences, lists, sets

• Maple demo of:– Assignment – Use of indexing to get at pieces– Concatenation of sequences– Sequences as “inside” of function invocation: binomial(3,5);

versus s := 1,2; binomial(s);– Convert sequence to list: [s]– Convert sequence to set: {s}– Convert list to sequence: op(L)

Convert set to sequence: op(S)– nops(…) gives number of elements– sort( list ) sorts in ascending order; sort(list, `>`) descending

Page 15: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Script test data in Problem 1.2

• Problem 1.2 asks you to generate a list of random numbers to test your scripts on. Just follow the directions, entering the instructions as listed. You should see a list of random numbers which you can apply your script to.

• Don't worry about the random number generator's operating principles yet. This lab, we just want you to use the generator.

Page 16: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Finishing up – save your work

• Save worksheets onto the Desktop. You can call them Lab3Part1, Lab3Part2, etc. Or you could put all the work into one worksheet and just call it Lab 3.

• Submit a copy to Blackboard site as evidence that you did the lab.

• Email a copy to yourself and/or your lab partners as an attachment so you can look at what you did for review purposes later.

• We will use this week's work in the next Lab.

Page 17: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Turn in your verification sheet

• Make sure your name/user id/section number/ date,time/instructor name are on the verification sheet.

• Get the verification sheet signed for all parts you completed and hand it in.

Page 18: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

Week 3

• Take Quiz 3 Tuesday-Friday Week 3– Maple TA server down for maintenance 9-

10am and briefly at 3pm each day during the week.

Page 19: CS 121 Engineering Computation Lab Lab 3 Department of Computer Science Drexel University Summer 2009 ©By the author. All rights reserved. Permission is.

>

>

>

> >

>

Demo of RandomToolsInstructors can use the following slides optionally.