14-440-127 Lecture 7 Notes

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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. • 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....
View Full Document

This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.

Page1 / 8

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online