Lesson 2 - Functions - Computer Applications – Fall 2010...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Computer Applications – Fall 2010 Friday, Sept. 17th, 2010. Todo: • • • • Project o Choose teams by Sunday Lecture Lab Assignment Home Work: o Work on Project 1, Part A this week. FUNCTIONS Matlab user‐defined functions o Zeros – try 1 input arg, 2 input args, 1 ouput arg, 2 output args - try using variables and floating numbers as inputs o Size – try 1 input arg, 2 input args, 1 output arg, 2 output args Remember Polymorphism = dependence of a function’s behavior on type or number of the argument(s) specified for it when it is called. In‐class exercise: Write a function called randcol that generates a column vector of N random numbers, where N is the argument used in the call to randcol. Here is an example of how it should work. >> randcol(3) ans = 0.54881 0.71519 0.60276 Write the function: Here is how to write you function: After you have started the Matlab editor, type this line: function v = randcol(N) Then, write a statement that calls rand, using N as one of the arguments in that call, and assign the result to v. o o o Adding help to your function: Give the command help randcol After the function line, insert these two comment lines: % RANDCOL Column of random numbers % Author: Put your name here Get a larger range by multiplying by something big. For example: 100*randcol(3). Get random integers by rounding. round(x) rounds each element of x to the nearest integer Matrix Multiplication: Z = X*Y o Matrices must be compatible, as shown in Ch 3.2 (color) o “Inner” dimensions must be equal to each other o Resulting matrix has the size of the outer dimensions o Matrix multiplication is a row‐times‐column operation o It is not commutative: A*B usually does not equal B*A. Example: function [a,vol] = area_volume(r) a = 4*pi*r^2; vol = 4/3*pi*r^3; Remember the basics of writing a function: First line of M‐file must contain the word “function” Name of M‐file should be the same as function name (except that it ends with “.m”). Input argument = is the input value passed by caller to function. Output argument = output o Must be set by an assignment statement inside the function o Two or more arguments must be separated by commas and surrounded by square brackets: [a, b, c, … ] o To receive more than one output value from a function, the caller must request more than one: Request for multiple outputs: >> [disk,ball] = area_volume(7) disk = 615.75 ball = 1436.8 Requests for one output: >> [disk] = area_volume(7) disk = 615.75 >> disk = area_volume(7) disk = 615.75 >> area_volume(7) ans = 615.75 Scope = the set of statements over which a variable is accessible. Local scope = scope limited to one function or to Command Window. o Scope is, by default, local in Matlab. o All variables in a function, including the formal arguments, are local variables. Global scope = scope that is not local. o Global variables were in common use during the first 20 years of computing, and they caused lots of bugs! Now, good programmers avoid them the way good writers avoid forward references. (“This will be make sense after your read the next chapter.”) o Why global variables cause bug: They are visible everywhere and can be changed without warning. (Functions are visible everywhere too by means of the path, but they cannot be changed without warning.) It is difficult to follow the progress of a program during execution if global variables are involved. o For these reasons, students in CS 103 (and 101) are not allowed to use global variables! (If you want to find out how to make a global variable, use help.) Debugging Running an M‐file in debug mode Including a break point activates debug mode. o Break point can be cleared by clicking again o Adding a condition Click right on and click Set/Modify Condition Example: current_value > 300e12 (sic) Dot changes color: Clicking yellow dot gives Clicking again clears it Run (click down arrow or dbcont) Step (click the curved arrow or dbstep) Exit debug mode (ends execution but doesn’t exit debug mode!) o click the down arrow with the red X or type dbquit Printing Functions Formatted printing to the command window with fprintf (see Section 3.6 of Ch. 3). Characters are printed ver batim except for Escape characters, which are not printed, but are treated as directions for printing: \, %, and ‘ o \n starts a new line, \t inserts a tab character o \\ prints one \ o %% prints one % o ‘‘ prints one ‘ Conversion characters, which are also not printed and are also treated as directions for printing, comprise a percent sign (%) followed by a format specifier: o %f, Fixed‐point with 6 places to right of decimal o %e, Scientific notation o %d, If integer, no decimal, otherwise = %e o See list in Table 9, Chapter 3 o Format is as follows o %m.nv, v can be: d, e, f, or g. m = minimum number of characters used in output including decimal point and minus sign if they are there; n = number of digits to the right of the decimal place. Right justified. If more than m characters needed, they will be used. o fprintf ignores the imaginary part of complex numbers (see also disp), so you have to use real and imag. Plotting in Matlab plot(x,y,a,s) plots two lines at once and selects different colors o Use hold to use multiple plot commands in the same figure. o hold on, hold off Annotating plots o grid [on|off], or click Edit/Axes Properties.../X‐tab/Grid o title o xlabel and ylabel Using plots to “solve” equations: o Example: Find x between 0 and 4 such that 3x 5 4 sin x >> x = 0:0.01:4; >> y = (3*x-5)/4; >> hold on >> plot(x,y) >> grid on ...
View Full Document

Ask a homework question - tutors are online