Week+of+1-21+to+1-27

January 23rd and 24th
This week is a short week, the first week of the semester, and a busy one for me, so I plan to take care of Chapter 7 in order to finally begin with GUIs next week. Chapter 7 is about functions, the MATLAB equivalent of commands in Java. The author discusses the importance of breaking complex computing/programming projects into small, easily testable and changeable parts. He stresses the importance of making a function that does a single thing very well. The cardinal difference between MATLAB functions and Java commands is that MATLAB functions can be saved for future use in other programs, whereas Java commands are usually too specialized to a particular program to be that blatantly reusable. Section 7.1 discusses the files themselves. It explains how a function file should always start with a line in the form "function =funcname". This way, MATLAB will save and recall the function properly. The file name has to be the same as the function name, or else it won't work. Section 7.2 explains inputs and outputs of functions. The author explains that when a variable is used as an input value in a function, the variable cannot be changed by the function, but rather the function uses the value that the variable represented at the beginning of execution and carries that forward inside its own different variable. It also explains how the title of the output variable is merely a passing name because its only use is in the programming of the function. This is the case because the function does not pass the variable back to the program, just the value it represents. Section 7.3 is a brief bit about local workspaces. This is sort of an expansion on what I mentioned in the previous paragraph about the inputs not being affected by the function. The workspace of variables that a function deals with is entirely separate from the main workspace dealt with by scripts and the like, so one does not even need to worry about naming a variable in a function the same thing as a variable in a program that uses that function - they still remain entirely unaffected by each other. Section 7.4 addresses the usual MATLAB expansion from a singe value into a vector. It turns out this can be done with function outputs. If one takes the notation, "function =funcname", from earlier and merely changes it to "function [ ]=funcname", then the function will return the corresponding vector. Section 7.5 is essentially a reiteration of some of the stuff from section 7.1. The only new snippet it adds is that the 'help' command also applies for functions you have saved; if the help command is called for a function created by the user, it displays the comments following the function declaration. Section 7.6 shows three specific other forms of functions. The first is the idea of a subfunction. The way this works is that you will have a function that, in the code, calls other functions, which are declared and programmed below the code of the main function. The whole function will operate under the name of the function at the top and all the subfunctions will merely be dependent arms of the main function. Another form of function addressed in this section is a nested function. A nested function is the one way to bring the workspaces of multiple functions together. This is a way for one function to draw off the variables of another function in a more deep way than the standard. The way nesting is achieved is by using 'end' statements around the operations of each function. The last form of function addressed in this section is an anonymous function. An anonymous function is declared in one line in the form "funcname=@ ". This is a form for functions that essentially need only to do simple calculations, and allows for something of a shorthand if one type of calculation needs to be done fairly many times throughout a script.

In order to display the concepts described in my words and the author's words, I decided to make a few little functions. The first is a simple little anonymous function that finds the hypotenuse of a right triangle given two legs (basic Pythagorean Theorem stuff). The next example is a very basic example of nesting functions. Just a note, this program is only useful in calculating the energy of a 3 kg ball that you assumedly really like to use in experiments. Although the nesting would be pretty much unnecessary for this type of calculation, I didn't have any better ideas and this example gets the job done. Essentially, it shows the use of 'end' statements to separate the functions' execution but to also nest the functions for Potential Energy and Kinetic Energy inside the function for Mechanical Energy. So, the two nested functions are able to draw on the mass variable and the gravity variable declared in the parent function and bypass the normal rule that functions do not mess with each other's variables. Finally, I chose to illustrate the idea of a subfunction. I reused the concept of different types of Mechanical Energy with fixed mass. This is probably the more logical way to try the concept I started with in the nested function example. As we can see here, I was able to use the same output and input variables in the 'potential' and 'kinetic' functions, and they operated separately and did not bother each other - the big difference between subfunctions and nested functions. Also, this function illustrates the ability of functions to generate multiple outputs. With that, I am done with part one of the textbook. When I come back next week, it will be time to get down to business on GUIs.