Wednesday 17 February 2010

Why God Invented Energy Drinks (and Brain Food)

Today was a pretty unproductive day. It's not for lack of trying. It started with a bit of (non-programming) group work, which was handed in way too late. Me being the manager, I stayed up late, got two hours of sleep, and went right back into finishing the job.

Programming on two hours of sleep is not easy. In fact, if you ever have the choice, don't do it unless you're looking for frustration. In my lack of experience on this matter, I was happily back to work on my ISA Calculator (MATLAB), programming the GUI to display a table of values for a user-input height range.

I kept getting a run-time error along the lines of "array indexes must be real, positive numbers." I finally narrowed it down to one particular snippet that was causing the error: length(a).

If you know a little about programming, you probably know that length() functions simply determine the length of whatever's inside the parentheses. Basically every language has it built-in. MATLAB specifically uses the length() function to tell you how many different values there are in an array. In other words, it can't go wrong. I even confirmed that a was defined, had a value and a size. Length(a) simply wouldn't work.

Well, after an hour of head-scratching, I gave up. Hours later (still no sleep), I went back to it and again could find no solution. I then went to irc.freenode.net #matlab and asked for help. They agreed. There was nothing wrong with the code, and the length() function can't cause that error in the situation I put it in. (Thanks to CrimsonScythe for going all-out to track down my stupid mistake!)

Well, turns out it can. I used the string variable called length to store the name of units of length (meters). It was declared very early in development, and very early in my program, and basically got hidden away. This was the first nested function I wrote in my calculator, coded inside the overall function so that it has access to all the variables in the main function easily, and therefore it was the first one that noticed this oversight on my part. I now feel bad for MATLAB as I'm clueless as to what it was trying to do with my little variable-gone-function. Rather than a square peg in a round hole, I think it was more like replacing a screw with super glue. It might pretend to work, but, really, when it comes down to it, it's going to fail, and in a pretty catastrophic way.

So, that out of the way, my loop worked! I'm done! Right? No. Because the loop worked, sure, but the outcome wasn't what I needed. Why not? Because, after 20 minutes of searching, I realised I had this in my code:

for q=1:length(t)
    mu = t(q) * (constants);
end

Assume the math was right, as (constants) here is a bit more complicated in my code. Anyone catch my mistake?

for q=1:length(t)
    mu(q) = t(q) * (constants);
end

That'd be the right answer. MATLAB was happily doing the same equation multiple times over with different numbers, but it was storing it all to the same place. In other words, where my other variables had 11 values, mu had only 1. Fun stuff. Also, note that this kind of error is the thing that any book or teacher will tell you is a common beginner's mistake. I am a beginner, I admit. But I promise you, this is a common mistake in general. Small detail, easy to miss.

So that's fixed. My table works! Right? No, of course not. I'm still not sure why not. The first column works fine. The first row works fine. But columns 2 through 8 in any row after row 1 are blank, with the values instead inserted into new columns after column 8. No real reason I can see. Go figure. My buddies on IRC have gone to their own tasks and so I'm left clueless. I'm going to bed. Maybe when I'm rested this will make sense again.

There's a tip I heard very early on in programming: no matter how short your program is, there's bound to be a bug. In fact, my first "Hello, world!" program in Java utterly failed on me, though the reason is lost to the failings of my memory. This is just one of those situations. For the past week my MATLAB work had no real problems, so this is just everything catching up to me.

Tomorrow should be the final day of development for this MATLAB project, at least in terms of the official requirements (I do have some smaller features I'm interested in adding to it after the fact). It should be... eventful, but we'll see.

No comments: