This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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/511/11, and second Project assigned 10/24 and due 11/9. 1 UserDefined 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 socalled builtin functions such as sin . You can write your own, which we’ll call userdefined functions . 1.1 Creating a UserDefined Function To write your own function called myFirstFunction , you just need to create an mfile, with a few complications: • You MUST save your code as an mfile, using the file name myFirstFunction.m . Of course, change the name to match the desired name of your function. • The first line of this mfile 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 mfiles, and your function will execute as if it were a builtin 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.
 Fall '08
 Finch

Click to edit the document details