14-440-127 Lecture 7 Notes

14-440-127 Lecture 7 Notes - 14:440:127 Introduction to...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
14:440:127– Introduction to Computers for Engineers Notes for Lecture 7 Rutgers University, Fall 2008 Instructor- Blase E. Ur After this lecture, you’ll be halfway through this class. Congratulations! Also, we’ve decided to deviate a bit from the syllabus starting after this lecture; we’ll post an updated syllabus this week. You’ll have your second Problem Set due on 10/27, second Exam given 11/5-11/11, and second Project assigned 10/24 and due 11/9. 1 User-Defined Functions As you saw in our early lectures, you can type in something like sin(0.32) . What this does is calculate the sine of 0.32. To be more technical, sin is the name of a function. You pass it a single input value or argument , 0.32 in this case. It returns some value (output), 0.3146 in this case. In Matlab, you’re not just constrained to using so-called built-in functions such as sin . You can write your own, which we’ll call user-defined functions . 1.1 Creating a User-Defined Function To write your own function called myFirstFunction , you just need to create an m-file, with a few complications: You MUST save your code as an m-file, using the file name myFirstFunction.m . Of course, change the name to match the desired name of your function. The first line of this m-file needs to be as follows: function OUTPUT = NAME(INPUT) NAME should be replaced by the name of your function. OUTPUT should be replaced by the name of some variable that you’ll use in your function. After Matlab runs all of the code in your function, it will look up the final value of your output variable and return that as the result of your function. Do you want to include more than one output? No problem! Create a vector of output variables, which means you should replace OUTPUT with something like: [out1 out2 out3] . If you always want to return all of these values, make sure your OUTPUT variable is a vector or matrix. INPUT is a comma delimiated list of the input variables. Let’s say you typed (in1,in2) as your list of input variables (notice that these are in parentheses, and separated by a comma– they’re not a vector). Then, if someone typed myFirstFunction(5,10) in Matlab to execute your function, in1 would be set equal to 5, and in2 would be set equal to 10.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Following your first line, just type Matlab code that implements your function (performs any calculations you need). Don’t forget that by the end of the last line of this code, your output variables need to have been set. 1.1.1 Example Function Let’s say you wanted to create a function called doubler , which doubles some number x when you type double(x) . You can type the following code, which you MUST save as doubler.m function y = doubler(x) y = 2*x; Now, once you’ve saved this file as doubler.m , you can type something like doubler(22) in the Matlab workspace or in other m-files, and your function will execute as if it were a built-in function.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern