Introduction to Programming in MATLAB Intro MATLAB Peer
Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-Non. Commercial-Share. Alike 3. 0 Unported License. Based on a work at www. peerinstruction 4 cs. org. 1
Are your fingers tired of typing things again and again? Trying writing… SCRIPTS 2
Using Scripts • I entered some important data about my dog Diablo into MATLAB, including: – Her name (‘Diablo’) – Her weight (22 kg) – The radius of her torso (15 cm) • I now want to calculate the circumference of her circular-shaped torso, using a script. 3
Variable Scope in Scripts I entered some data about my dog Diablo into MATLAB, including her name (Diablo), weight (22 kg) and the radius of her torso (15 cm). I now want to calculate the circumference of her circular-shaped torso, using a script. Circumference. m (script) d = 2 * r; circumf = pi * d; a) 30 b) Diablo c) Other/None/Error Command Window » d = ‘Diablo’; » w = 22; » r = 15; » Circumference » circumf = 94. 2478 » d What is printed here? 4
Another way to save yourself some typing: FUNCTIONS 5
What is the role of the pink part of the function definition line? Circumference. m (function) function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end a) It tells you that you need to assign the input value to a variable named r before you call the function Circumference. b) It tells you that Circumference takes one input, called r. c) It tells you that Circumference will output a value in a variable named r. d) It tells you that Circumference returns the value of r. e) None/more than one/other 6
Which is NOT a valid way to call the Circumference function from the Command Window? Circumference. m (function) function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end e) » circumf = Circumference(10); » c = Circumference(10); » x = 10; » c = Circumference(x); None/More than one/Other Command Window a) b) c) d) 7
Variable Scope in Functions I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22 kg) and the radius of his torso (15 cm). I now want to calculate the circumference of his circular-shaped torso, using a function. Circumference. m (function) function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; Command Window end » d = ‘Diablo’; » w = 22; » r = 15; a) 30 » circumf = Circumference(r) b) Diablo circumf = c) Other/None/Error 94. 2478 » d What is printed here? 8
(a) Functions work this way, OR (b) Scripts work this way 9
(a) Functions work this way, OR (b) Scripts work this way 10
Variable Scope in Functions I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22 kg) and the radius of his torso (15 cm). I now want to calculate the circumference of his circular-shaped torso, using a function. Circumference. m (function) function [ circumf ] = Circumference( r ) d = 2 * r; r = ‘messing with r’; Command Window circumf = pi * d; » d = ‘Diablo’; end » w = 22; » r = 15; a) 15 b) messing with r » circumf = Circumference(r) c) messing with r 30 circumf = d) Other/None/Error 94. 2478 » r What is printed here? 11
Scripts vs. Functions Scripts Functions • Location: in a file (file’s name will be how you call the script) • Use: type script name in the Command Window • Input/Output: must be careful to have variable(s) with certain names set to input values before calling, then look at variable(s) with certain names to see what happened/output • Variable scope: script has full access to all variables as if its code had been just typed in the Command Window • • Location: in a file with same name as the function Use: type function name in Command Window, include assignment of output and/or include input arguments Input/Output: input(s) are provided as specified arguments, output(s) are provided as specified return values Variable scope: function has no access to read any variables from the Command Window, except as they are explicitly provided in the input arguments. Function has no access to change/write to any variables to the Command Window, except as explicitly given in the outputs 12
- Slides: 12