%% Lecture 3 % % READING DUE NEXT WEEK: % % Reading corresponding to this lecture: Chapter 6, Gilat. % Today we are going to extend our analogy of matlab as "Gauss in a % Box". Specifically, we are going to try to go through the steps Gauss % does when you give him a command. So imagine a box with a slot that % says "In", a chalkboard, and a file cabinet. Guass sits inside this % box waiting for something to do. Some examples... % % You write on a strip of paper, "4+5" and put it in the slot. % Gauss looks at the strip of paper, figures out the answer is 9 and % shouts it out at you. % % You write on a strip of paper "4+5;" and put it in the slot. Gauss looks % at the strip of paper, figures out the answer is 9, but doesn't say % anything (because you told him to keep quite with the semi-colon). % % You write on a strip of paper "a = 4+5;" and put it in the slot. Gauss % looks at it, figures out the answer is 9, and writes on the % chalkboard "a = 9" a = 4+5; % If there was no semi-colon, Gauss would yell out "9" when he figures it % out and would then write "a = 9" on the board; % % You write on a strip of paper "tan(0.3)". Gauss looks at it and yells % "0.3093" (because he is really good at numbers). % % if you write on a strip of paper "tan(0.3);", he would figure out that % answer but not do anything with it. % % Now Gauss doesn't remember all possible functions. He knows some, and has % a big file cabinet telling him how to do others. If you type open tan % you will see place holder function file. At the end of it, it says % "this is a built in function"... those are the things Gauss knows % in his heart. % % You write on a strip of paper "cot(0.3)". Gauss doesn't know "cot" % (cotangent) in his heart of hearts. So he goes to his file cabinet % and looks to see if he has instructions for how to compute cot. % He does!, and if you type: open cot % you will see the function defined for cot. it says % % function y = cot(z) % % .. a bunch of comments. % y = 1./tan(z); % Gauss says "ok... cot(0.3)". % % First, Gauss pulls down a brand new chalkboard covering the old one % (This happens% *every* time Gauss starts to work with a function). % % Then, the first line of the function says "write down z = what was % passed into the function" % so Gauss goes over to a brand new chalkboard and writes: % "z = 0.3" % % then he goes down and says I need to compute "1./tan(z)". % I wonder what "z" is? % Hey look!, it's on my chalkboard. so I'll replace z with 0.3. % % Now, I need to compute "1./tan(0.3)". I wonder what tan(0.3) is? % oh, I KNOW that in my heart of hearts, tan(0.3) is 0.3093 % % So now I need to compute "1./0.3093" % that is 3.2327 % % and now I write that down on my chalkboard as y = 3.2327 % % Then Gauss looks to see if there are any more instructions. There are % not, so he looks back at the top line of the function, (function y = cot(z)), % This means "the answer is the value of y at the end of the function". % % He thinks I wonder what "y" is? % But then notices it on the board and so he knows that % cot(0.3) = 3.2327 % so he shouts "3.2327". % % then he erases his extra board and pulls back down the original one. % % So what happens if you give a slip of paper saying "z = cot(0.3);" to Gauss % % He pulls down the *new* chalkboard, writes % z = 0.3 % eventually writes y = 3.2327 % erases the chalkboard and pushes it up to get back to his original % chalkboard, and on *that* one writes z = 3.2327. % % all this happens when we write: z = cot(0.3); % What happens if we ask for cot(0.3,0.2)? % He looks up the "cot" function in the file cabinet. He finds it, but the % function only asks for 1 input so he yells out "somethings wrong". % He even tells you what it is!... try it: cot(0.2,0.3) % %%%%%% undefined functions. % What happens if we ask for cott(0.3)? % He looks for the cott function in the file cabinet. He doesn't find % it, so he yells out, somethings wrong! cott(0.3); % One thing I'd like to convince you of is that error messages are your % friends! These messages are Gauss, trying as best he can, to tell you % what is the problem. There is actually a fantastic wiki page devoted % to this subject: % http://en.wikibooks.org/wiki/MATLAB_Programming/Error_Messages % Let's explore some different error messages: % sin() sin(2,1,2,1) sin('three') cot(0) % i like this last one especially, it is rather like Gauss "grumbling". % "good" code should avoid both errors and warnings. % other problems you make for yourself, perhaps accidentally. If you % are working on a project of the relative merits of sleeping furniture, % assigning them scores, you might have code like: tatamiMat = 1; bunkBed = 2; cot = 2; queenSizeBed = 5; % Later, you need to compute something about the geometry of these % sleeping aparatus, so you try to calculate the cotangent of something. % So try: cot(0.3); % What happens here is due to a choice that matlab made. The syntax % cot(?); % could mean calculate the cotangent of the argument, or it could mean % give me the ?'th element of an array called cotangent. % % So which does Gauss choose? when given the slip of paper "cot(0.3)" % Gauss first looks at the chalkboard for any variable named "cot". If % he finds one, he assumes that you are trying to refer to that. So then % he thinks you are making the mistake in asking for the 0.3'rd element % of that array. % if you need to use cot again, you can type: clear cot % Which tell Gauss to erase "cot = 2" from the blackboard, so that the % next time you ask him cot(0.3), he won't find cot on the blackboard and % will go back and look in the file cabinet... %%%%%%%%%%%%%%%%%%%%% % Thinking about functions lets us explore two new features of matlab... % we've used these a little bit in the past, but not really talked about % it. % The first is a "for loop". This specifies that you want a piece of % code to be executed a number of times. So what happens if you give % Gauss a strip of paper that says: t = 0 for ix = 1:10 t = t + ix end % (hey, question, why shouldn't I use "i"? as the loop variable?)