1 / 12

Introduction to Programming in MATLAB

Introduction to Programming in MATLAB. Intro. MATLAB Peer Instruction Lecture Slides by  Dr. Cynthia Lee, UCSD  is licensed under a  Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License . Based on a work at  www.peerinstruction4cs.org .

petula
Download Presentation

Introduction to Programming in MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Based on a work at www.peerinstruction4cs.org.

  2. Are your fingers tired of typing things again and again? Trying writing… Scripts

  3. 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.

  4. Variable Scope in Scripts I entered some data about my dog Diablo into MATLAB, including her name (Diablo), weight (22kg) and the radius of her torso (15cm). I now want to calculate the circumference of her circular-shaped torso, using a script. Circumference.m (script) Command Window d = ‘Diablo’; w = 22; r = 15; Circumference circumf circumf = 94.2478 d d = 2 * r; circumf = pi * d; • 30 • Diablo • Other/None/Error What is printed here?

  5. Another way to save yourself some typing: FUNCTIONS

  6. 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 • It tells you that you need to assign the input value to a variable named r before you call the function Circumference. • It tells you that Circumference takes one input, called r. • It tells you that Circumference will output a value in a variable named r. • It tells you that Circumference returns the value of r. • None/more than one/other

  7. Which is NOT a valid way to call the Circumference function from the Command Window? Circumference.m (function) circumf= Circumference(10); Circumference(10); c = Circumference(10); x = 10; c = Circumference(x); None/More than one/Other Command Window function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end

  8. Variable Scope in Functions I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22kg) and the radius of his torso (15cm). I now want to calculate the circumference of his circular-shaped torso, using a function. Circumference.m (function) Command Window d = ‘Diablo’; w = 22; r = 15; circumf= Circumference(r) circumf= 94.2478 d function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end • 30 • Diablo • Other/None/Error What is printed here?

  9. (a) Functions work this way, OR (b) Scripts work this way

  10. (a) Functions work this way, OR (b) Scripts work this way

  11. Variable Scope in Functions I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22kg) and the radius of his torso (15cm). I now want to calculate the circumference of his circular-shaped torso, using a function. Circumference.m (function) Command Window d = ‘Diablo’; w = 22; r = 15; circumf= Circumference(r) circumf= 94.2478 r function [ circumf ] = Circumference( r ) d = 2 * r; r = ‘messing with r’; circumf = pi * d; end • 15 • messing with r • messing with r 30 • Other/None/Error What is printed here?

  12. Scripts vs. Functions Scripts Functions 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 • 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

More Related