% Script 3 for week 1 % Fun with M-files and Functions % There are three ways you can create things that act like functions (without using the symbolic package). First, there is the one-off function defined for the current session in the command window. Second, there is a named function file in the active directory that acts just like any of the built-in MATLAB functions such as sine or logarithm. Third is a script file that is really just a saved list of commands, so you don't have to type them in if you want to re-use them. % Here is how you create a one-off function in the command window for temporary use: f2=@(x,y) x.^2 + y.^2; f3=@(x,y) x^2 + y^2; f2(3,6) f3(3,6) f2([0 3], [7 6]) f3([0 3], [7 6]) % Generally in these scripts if you see a row of asteriks they will delineate the contents of an M-file. It is intended that these contents be placed in a file whose name terminates in ".m" and this file is to be placed in the MATLAB active directory. % Put the following (from the word "function" down to "end") in a file named sizeout.m %**************************************** function [s x a] = sizeout(x) s = size(x) a=s a=0*a s=a end %**************************************** % This function and the following script illustrate the difference between "interior" and "exterior" variables and their use in scripts compared to function M-files. Type the following in the command window: clear z= [9 0 7 5 6] s=3 y=sizeout(z) % Here is a script that does the same thing as the function above. First try to run it on the command line, note the error, then let x=z and run it again. Then create a file called test.m and put just these four lines in the file in MATLAB's working directory and (right click on filename) or drag it to the command window run it. s = size(x) a=s b=a c=b % The significance of what you did is the following. The sizeout FUNCTION uses symbols x, a, b and c internally, but has no idea what they are called externally and only knows what x is because that is whatever it sees in sizeout's "parentheses." These variables are not created or changed in the "global" world, only inside the "local" world of the function. An M-file script test.m on the other hand needs x to be defined first and then will create or "pollute" the variables named s,a,b and c and these changes which will have global effect as you can see in the Workspace. This can be bad. If the script is complex and introduces many interior variables you might inadvertently wipe out some variable you need for other reasons! % Below are use two useful function m-files (provided, examine them!) that illustrate how to create, internally document and use function M-files. (They also are functions you may need for later!) %**************************************** x=[1 4 6 3 7 9 7 8 7 9] y=[5 6 4 3 7 8 10 11 8 8] [a1,a0]=LinearRegression(x,y) [b1,b0]=LinRegPlots(x,y) % The rest of this script involves something that is often useful: namely, functions can call each other like the two functions below. % Try invoking callfun.m by z=callfun(5,3) % "callfun" calls "callme", so you need to create both and put them in the same directory to get callfun to work. %**************************************** function z=callfun(x,y) z=3*x+100*callme(y); end %**************************************** %**************************************** function h=callme(y) h=y/1000; end %**************************************** % Here is a function that calls itself! Note square brackets around function output, required for self-calling functions. I don't actually use this feature often, but some people are self-referential-function fanatics. The programming language Lisp is all about recursion. It may be much more efficient in some situations, I don't know exactly when though. Also, I don't know how often in practice this is an issue. Anyway, to try it put this in a file called selfcall.m %**************************************** function [s]=selfcall(y) if y>-100, s=selfcall(y-9); else s(1)=y; s(2)=y^2; end end %**************************************** % Here is another function that calls itself. Note square brackets around function output. Put this in a file called FactorialRecursive.m %**************************************** function [s] = FactorialRecursive(x) if x > 1 s = FactorialRecursive(x - 1)*x; else s = 1; end end %****************************************