lab17 due now deliverable 2 quizzes continue next lab. on functions. 1

12
Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

Upload: arabella-chase

Post on 31-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

1

Lab17

• DUE NOWDeliverable 2

• Quizzes continue next Lab. on Functions.

Page 2: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

2

QUIZ TIME!

• Close all notes and MATLAB.

• Blackboard, under 2.Labs/HW/SolutionsLab17_QuizFunctions(5 minutes - timed) password required1 attempt

Page 3: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

3

Functions!

1. Remember it’s a completely separate .m file.2. You’ll need an actual script file (other file) to make it fully

operational.You should of course test in the cwd (Command Window) first.

3. The main script file communicates to the definition files through what is called the “function call” (which is 1 line of code).

What is crucial is the order of the return-values and arguments.

4. What does “collecting return values” mean?

Page 4: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

THE ORDER MATTERS

• When calling a function (whether built-in or programmer-defined):

The order matters on the argument list (the right side)a. fprintf(age, ‘name: %s age: %d\n’ , name);b. fprintf(‘name: %s age: %d\n’, name, age);c. fprintf(‘name: %s age: %d\n’, age, name);d. fprintf(age, name, ‘name: %s age: %d\n’);

The order matters when “collecting return-values” (the left side)a. [x y z] = xlsread(‘data.xlsx’ , ‘Sheet1’);b. [raw, txt, nbs] = xlsread(‘data.xlsx’ , ‘Sheet1’);c. [nbs, txt, raw] = xlsread(‘data.xlsx’ , ‘Sheet1’);d. [txt, ~, nbs] = xlsread(‘data.xlsx’ , ‘Sheet1’);

4

NEW SLIDE

Page 5: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

5

MATLAB can help you start..

• File> New> Function

• This is THE MOST GENERALIZED form• SIMPLIFY IT – some things are useless

– delete the [ ] when there is only 1 return value– NEVER INCLUDE THE end keyword (no matter who tells you to)

• This would be way out of “introduction to programming”, let the file end peacefully when there is no more lines of code!

✗✗

Page 6: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

6

Some examples from last semester’s projects

example with no parameters

Page 7: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

7

examples, cont.

example with no return-info

Page 8: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

8

examples, cont.

PS: improve comments…!

both return-values & parameters

Page 9: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

9

Remember GUIDE?

The figure drawn, with all buttons and boxes (etc..) generates a function file.

Here is my main script file, 7 lines.

Note: returning values from a GUIDE function file requires a bit of research…

Page 10: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

Try it #1: 5 minutes

• Translate this to a function. Show you tested: Create a function which receives 1 argument (weight of a satellite) and

calculates and returns the weight of the final payload. (All units are Newtons).

• The client also gives the following data:

• Create a script file to see if the new keyword works! 10

Weight of Payload = W_structure + W_telemetry + W_power + W_guidance

Where:W_structure = 2.16 * W_satellite W_telemetry = 0.78 * W_satelliteW_power = 1.24 * W_satelliteW_guidance = 1.21 * W_satellite

both problems due at end of class.

Page 11: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

11

Try it #2: timeleft/2 minutes

• The goal for this task is to write a function to shuffle a deck of cards, or more generically to mix up any vector it is given. The basic algorithm for this is as follows:

– Select 2 positions at random– Swap the contents of those 2 positions. (Note, swapping requires a third

temporary spot to copy one of the two values being swapped).– Repeat this process for a large number of times (generally, looping 20

times the number of items should be sufficient).

• Your function should accept a single vector as an input argument and return a vector containing all those items randomly rearranged.

problem by Dr. Verleger

Page 12: Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1

12

Sample Output

Sample Output: >> MyCards = 1:10 MyCards = 1 2 3 4 5 6 7 8 9 10 >> MyShuffledCards = ShuffleDeck( MyCards ) MyShuffledCards = 3 9 8 4 7 6 2 1 5 10