# Tag Archives: programming

## Analyze, hack, create

One of these days I’ll get back to blogging about the mathematics courses I teach, which make up the vast majority of my work, but the MATLAB course continues to be the place where I am working the hardest, struggling the most, learning the biggest lessons about teaching, and finally having the greatest sense of reward. This week was particularly rewarding because I think I finally figured out a winning formula for teaching a large portion of this stuff.

This was the last in a three-week series on introduction to programming. We had worked with FOR loops already. I had planned to look at WHILE loops in the same week as FOR loops, then have the students play around with branching structures in week 2, then have them apply it to writing programs to do numerical integration week 3 for use in their Calculus II class in which most of the class is currently enrolled. But the FOR loop stuff went very roughly. So I moved the numerical integration stuff up a week and saved the entire remainder of looping and branching structures — WHILE loops, the IF-ELSEIF-ELSE structure, and SWITCH — for week 3.

I approached it like this.

The majority of their homework consisted of watching three videos: one on general programming in MATLAB, another on MATLAB loop structures in general, and a third on IF and SWITCH statements. That’s about 20 minutes of straight viewing; I told the students to budget an hour for these, since they’ll want to pause and rewind and work alongside the videos. Then, the majority of their homework was this M-file:

%% Script M-file for April 5 Prep/HW for CMP 150.
%
% For each block of code below, write a clear, English paragraph that
% explains what the code does. You can play with each block of code by
% removing the comment symbols and running this file. (You can "uncomment"
% lines by deleting the percent symbol or by highlighting the code you want
% and selecting Text > Uncomment from the menu above.)

%% Code example 1

x = input('Please enter in a number less than 100: ');
while x < 100
disp(x)
x = 2*x - 1;
end

%% Code example 2

x = input('Please enter in a number: ');
if x>=0
y = sqrt(x)
else
y = exp(x) - 1
end

%% Code example 3

value = input('Please enter in a whole number between 1 and 20: ');
switch value
case {2, 3, 5, 7, 11, 13, 17, 19}
disp('Your input is a prime number.')
case {1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20}
disp('Your input is a composite number.')
otherwise
disp('I do not think you entered in a whole number between 1 and 20.')
end


In other words: Here’s a bunch of code. Write up a plain-English description of what everything is doing. That was their homework. (Here’s the full assignment.)

Then in class, we played a game. For each code sample, I asked, “What if I entered [fill in the blank] to this bit of code?” For example, for code sample #1, what would happen if I entered in 100? One student immediately said, “Nothing”. Another said “It would give you a ‘0’ because that’s what ‘100 < 100’ returns as in MATLAB.” Then I had them close their eyes. “How many say, ‘Nothing’?” Count the hands that go up. “How many say ‘0’?” Count those hands. Put the tally on the board. The result: 7 for “nothing”, 6 for “zero”. Instant discussion fodder.

(By the way, this would have been a perfect place for clickers. I’m working on that.)

Next I asked, “What would happen in code sample 1 if I put in a negative number?” One guy said: “I know! I did that, and the thing kept running and never stopped, and I had to unplug my computer!” So I showed them all Control-C; then asked, “Why did that [the failure of the program to stop] happen?” In no time at all — a high-order discussion about an important related topic to looping structures (avoiding infinite loops) that I had not even planned to bring up.

We played that game for 20 more minutes. Students were into it. They were coming up with their own cases. We tried entering in ‘Hi mom’ to code sample #1 and it actually gave something back. It was mysterious and entertaining and nerdy. They discovered that testing out extreme cases is not only important for understanding your code, it’s fun. And it was a lot better than lecturing.

The best thing is, when I got them finally into their lab problems, they were asking better questions. “I think I can do this program with a SWITCH statement, but could I make it better with an IF statement?” And: “I’ve got all the cases listed out here in my SWITCH statement, but I wonder if I could just use a vector or LINSPACE to list them out instead.

So that’s going to be my approach from here on:

1. Analyze: Look at someone else’s code and write out a complete, plain-English description of what every part of it is doing.
2. Hack: Take the same code and modify it, tweak it, rewrite it, throw extreme cases at it. This is the bridge between reading code and writing code.
3. Create: Write some code to do something new — now that you’ve learned the language from someone else’s use of it.

For all I know I could be totally reinventing 40-50 years of established best practices in computer science pedagogy. But it’s pretty exciting nonetheless.

Filed under Education, MATLAB, Problem Solving, Teaching, Technology

## Programming, lectures, and the inverted classroom

Image via Wikipedia

We started programming in the MATLAB course a couple of weeks ago. It’s been… interesting. Keep in mind that 75% of the students in the class have never written a program of any sort before; half the class rates themselves below a 6 out of 10 in “comfort level” in using computers at all. As with everything else in this course, the audience is everything.

I started this three-week unit last week with a minilecture on FOR loops. But wait, you say: I thought you were using an inverted classroom model for the MATLAB course, where students are assigned reading and viewing tasks outside of class, accompanied by homework assignments designed to help them extract the relevant information and then do simple applications of what they’ve learned. Well, yes, that’s been the plan, and the practice up until now.

But I decided to go with a minilecture/activity model for the programming unit for two reasons. One, as has been the case much of the time when putting this class together, I wasn’t finding any materials out there to give out to students on the absolute basics of MATLAB programming — by which I mean the concept of the function M-file and looping structures (we’re getting to branching structures later) — that is suitable for a total-novice audience. The Mathworks, Inc. tutorials, usually well-suited for this kind of thing, were just a little too advanced. And there just wasn’t much else out there on this subject that I’ve found that was free, online, and well-suited for this kind of thing. If you think that smells like a screencasting opportunity waiting to happen, you might be on the right track.

Secondly, the students have been struggling with this inverted classroom model even when the materials I give them are good and the subject (like curve fitting) is both familiar and pretty basic. The struggles have been mainly cultural and psychological, not intellectual. The students are plenty smart enough to pick up the basics on their own. But we’ve run into some linear combination of these issues:

• Not adopting an appropriate timetable for learning the material outside the classroom. That timetable is: get to work early on the stuff, spend small chunks of time on it, and make those chunks frequent. The ideal schedule probably comes out to: no more than 30-45 minutes at a sitting, done over the course of 5-6 days. That jives with the “2 hours outside of class for every hour inside of class” conversion factor that usually works for college. Instead, the average start time on the outside work has been less than 24 hours before the labs.
• An expectation that after watching the videos or doing the reading, we are now ready to write workable code. There has not been widespread recognition that programming, or just working with a computer system in general, is all about getting your hands dirty with stuff that is not assigned before there is a chance of doing correctly the things that are assigned. There is a certain amount of time between being exposed to information and being able to do something right with it, and that time has to be spent doing things wrong with it. The misunderstanding, common to this demographic, is that information = expertise. Get exposed to the information enough, and you’ll be good at whatever that info was about.
• A simple psychological block against the idea that basic acquisition of concepts can take place in the absence of an expert.

I asked students in a questionnaire what changes to the class would help them learn MATLAB better. Overwhelmingly (75%) the students said that a lecture before the lab activities would be helpful, even though this would mean drastically reduced time on the labs in class and therefore more time spent outside of class, which is something they (understandably) don’t want. When I asked a couple of students what they expected to get from a lecture that they can’t get through online materials, use of the class discussion board, office hours, or conversations with their lab partners, the answer was: We’d just feel better about things if you’d lecture.

So, as I said, I started the first programming class with a minilecture. I walked students through the creation of a function called sin_period:

function period = sin_period(p)
period = 2*pi/p;
x = linspace(-2*pi, 2*pi);
plot(x, sin(p*x))


So this is an unsophisticated example of how you can write a MATLAB function that does a couple of pretty different things at once. It calculates the period of $y = \sin(px)$ where the parameter $p$ is provided by the user, and then it generates a plot of this function. Lab activity #1 given to the students: Modify this function so that the plot contains exactly two periods of $y=\sin(px)$ and it puts a plot of $y=\sin(x)$ on the same plot, using a dashed red line, for reference. (I really sold this to the education majors, who make up half the class, as an example of where a MATLAB function could be a useful classroom demo tool for a trigonometry or precalculus class.)

The second demo in the minilecture was using a basic FOR loop:

for i=1:10
disp(['The square of ', num2str(i), ' is ', num2str(i^2)])
end


This was actually given out in homework during the week with the instructions to change stuff in this loop and see what happens. What is the disp command doing? What is that num2str command and why do we need it? What happens if we leave off the square brackets inside disp, and why do we get the error message we get? And above all, what does this code actually do? What would you change to get it to print out the squares of the first 20 positive integers? And so on.

I wanted this to be not so much a minilecture as a discussion among students of what they did, what they encountered, and what they learned during their code-tweaking. But it ended up being nothing but me lecturing. I’m not sure what happened. Certainly I didn’t insist on students sharing what they discovered; maybe I was getting nervous about taking up too much more lab time.

After this was over, I gave Lab activity #2: Write a function M-file called vector_diff that accepts a vector “x” as input and produces a vector “d” as output, whose entries are the differences in consecutive entries of “x”. For example, vector_diff([1 3 0 8]) should return [2 -3 8]. Of course the diff function in MATLAB already does this, and some students figured that out, but the point was to recreate that functionality from scratch to learn how a FOR loop works.

So, you ask, how did that lab turn out?

It was all over the map in terms of quality. With the sin_period activity, about half the students got it exactly right. The other half almost uniformly added in the plot of $\sin(x)$ with the correct color and linestyle but were unable to get the function to adjust the x-range so that two periods of $\sin(px)$ show up. In other words, they were able to add in the routine stuff (just a plot command) but not able to transfer the idea of local variables within functions to use the period (which we’d already calculated) to set the x values. (All you have to do is replace x = linspace(-2*pi,2*pi) with x = linspace(-period, period).)

The FOR loop had a lot more variation. Some groups got it almost exactly right (biggest obstacle: not knowing how to add new entries to a vector). Some groups wrote code that appeared to be trying to use the right ideas, but the syntax was way off, for example using the counter variable “i” as if it were a vector (for example, using i(1):i(n) as the range on the counter). Some groups turned in nothing at all; although it turned out later that those groups had something done, but because it wasn’t exactly correct they decided to turn in nothing.

This is not a total loss, and this week (which I’ll report on later) I think we improved a lot through some more basic discussion of how to program, in general. But I take several lessons away from the results of the week 1 lab, with its much-requested lecture component:

• Lectures are only helpful to the extent that they impel students to act on what they are hearing. If you want a lecture just because you’d like an expert to assuage your misgivings about a subject — you want a lecture merely to feel better and more centered about what you’re learning — then the lecture is going to accomplish nothing other than providing a false sense of security to the students. Is this helping the student? No.
• Students who are still less than one year out of high school, where largely the opposite of what we’re doing in this class is the norm, have no idea that the first point is the case. For most students in this concrete mode of thinking, teaching = lecture = learning. This doesn’t mean they aren’t smart or capable of becoming self-supporting learners, which is sort of an overall goal of my teaching in any class. It does mean that they have to be exposed to activities that enable or require self-sufficiency, early and often. Or else they will be totally unable to function after they graduate.
• Therefore I’m more convinced than ever that the inverted classroom model for this class really is the right way to go. Give students “lectures” outside of the class time; give them time to think, questions to answer, simple tasks to perform that are based on what they read and watch. If they do it, this will generate the right questions which they can then ask (themselves, their lab partners, or me). Then they have an entire class meeting in which to do some more complicated task.
• But this only works to the extent that students adopt the short/frequent/early schedule for working on the material. If students continue to operate on the assumption that 300 minutes applied in a single 2.5-hour block late on Sunday night is the same as 300 minutes applied in five 60-minute blocks across multiple days of the week, then of course they’ll choose what appears like the lowest-footprint model and go with the single big block. But this is a flawed assumption, especially in an activity which most students self-identify as being something with which they are totally unfamiliar and largely not all that comfortable.
• This means I have to do a better job of getting students to realize this. I don’t want to start FORCING students to work this way — for example, by having teams submit journal entries or blog posts or something every couple of days. I’d rather get them to see that learning a computer tool is much more fun when you give yourself time to play, time to make and correct mistakes — time to get used to the kinds of expert behaviors that computer people employ. Incentivize rather than require.

The story continues.

Filed under Critical thinking, Education, MATLAB, Teaching, Technology, Uncategorized

## Resources for the MATLAB class

Image via Wikipedia

We’ve had one full meeting of Computer Tools for Problem Solving (the MATLAB course I’ve blogged about). According to the survey I’m having students fill out on our Moodle site, it went pretty well, even if it was a little like drinking from a fire hose. This first meeting was a lengthy guided tour of all the core features of MATLAB, assuming no prior knowledge of computer algebra systems or programming. Subsequent meetings will be a lot more hands-on, with students working in groups on lab activities centered around a particular topic or problem. This next week it’s graphing, for instance, and students will be creating all kinds of different plots of data and functions.

Students prepare for these activities through out-of-class reading and viewing assignments and through homework assignments that are intended both to pull together the material they learned in week n, and to get them to teach themselves the basics for what they will need in week n+1. Here’s the homework I gave last week, for instance. Self-teaching is at the heart of the pedagogical design of the course. This past week I stressed the centrality of the doc and lookfor commands as the primary way they will learn what commands are available in MATLAB and the syntax for each, and I laid down the rule that I will never answer a question of the form, “How do you do this in MATLAB?” I want students to be self-feeders. (I will be fielding questions from the reading/viewing each week prior to the activity starting, so the students are not totally on their own.)

Which brings us to the question of resources. As I mentioned in a previous post, none of the wide array of entry-level MATLAB textbooks really hit the right notes for my student audience.  So I ended up going with no textbook at all. I realized that in learning MATLAB myself, I’d come across a wealth of resources on the web that were completely free, and if properly integrated into the course, they could serve just as well for students as they did for me.

I find myself gravitating towards three main sources:

• The page of tutorials for MATLAB at Mathworks.com. This contains two hours’ worth of professionally-produced, appropriately-pitched interactive video tutorials starting from the beginning and hitting all the important topics one would need to get started with MATLAB. There are very few places in these videos that are not excellent, and there are even built-in multiple choice quizzes to assess your learning of the material as you watch. For me, these videos are the main “textbook” for the course.
• Cleve Moler‘s free e-book, Experiments with MATLAB. When I first drew up this course, Moler’s book was going to be the textbook. It is said to be designed for high school students taking calculus or a science course. Unlike the bone-dry technical approach that most introductory MATLAB books seem to take, Moler teaches MATLAB via the problems he sets out. It’s a great idea. Unfortunately, the book quite often badly overestimates the math and computing background — and comfort level — of the typical college student, and it just doesn’t work as a primary source for my students. Nonetheless, it’s free, and it is well-written when the level of writing is audience-appropriate, and the problems are fun. We’ll be using 3-4 of the early chapters later in the course for activities.
• The MIT course “Introduction to Computer Science and Programming” available through MIT OpenCourseWare. This is actually a course on basic computer science using Python and has no MATLAB in it, but it’s so well-done and the problems so well-conceived that I am finding much that I can retrofit for the MATLAB course. When we get to MATLAB programming in week 5, I will be basing much of my approach to teaching the material on the outstanding teaching of the MIT profs, Eric Grimson and John Guttag. And many of the problems I’ll be giving are just appropriated from their problem sets with Python being replaced by MATLAB. (All OCW courses are under a Creative Commons license that allows this kind of remixing.)

The really nice thing about these three sources is that they are all free for students and me to use. Since students aren’t paying $60+ for a textbook, they can consider spending$100 to purchase the student version of MATLAB and not be tied to the campus network for their classwork.

1 Comment

Filed under MATLAB, Python, Teaching, Technology, Textbook-free, Textbooks

## MATLAB course details and update

We start classes this week, a bit later than most other folks thanks to our January term. That means the long-awaited MATLAB course will be formally kicking off. I’ve had a few people ask me about what we’re doing in this course, so here’s an update.

This has been a tricky course to plan, because the audience is definitely not the usual one for an introductory MATLAB course. Almost all the introductory textbooks and materials I reviewed for the course, and all the introductory MATLAB courses I looked at from other schools, have a particular student demographic in mind: they are all engineering majors; they are all freshmen or sophomores with either a semester of programming under their belts or at least a very high level of comfort with computers and the “guts” of programming; and they are all attending large universities in which the particular academic makeup of the institution plays little to no role in how the class is designed.

By contrast, of the 15 students enrolled right now in my course:

• 9 are freshmen; 5 are sophomores. (At least that demographic fits the profile.)
• 5 are Education majors; 3 of those are secondary education majors, the other 2 elementary education majors. There is one lone Pure Mathematics major. 9 have no declared major at all.
• Of the 9 undeclareds, two of those are students who are pursuing our dual-degree program in Engineering and just haven’t gotten around to filling out the paperwork yet.
• But that’s it: Two undocumented engineering majors; NO science majors. The plurality are Education majors, and once the undeclareds get around to declaring, this may become the majority.
• I don’t have data for this, but I am pretty sure less than half of the students have ever had any exposure to programming whatsoever. I wouldn’t be surprised to find out it’s more like 1/5 of the class with no programming experience.
• Again, no data, but I think a good portion of the students would not consider themselves comfortable around computers once we start talking about something besides basic office apps and web pages. For example, anything having to do with typing stuff into a command line. (Like much of MATLAB.)

So this is not the audience that is “supposed” to be taking a MATLAB course. They are not (for the most part) scientists and engineers, and if you start throwing the details of fprintf, memory addressing, floating point arithmetic, and so forth to them you will likely lose them.

And yet — and this has been the frustrating thing — almost all introductory MATLAB materials assume students are engineers and scientists who have no problem being thrown directly into learning about fprintf, memory addressing, and floating point arithmetic. I won’t name names or authors, and these are not bad books, but they are written with a particular assumption in mind about who is using them. And that assumption does not work for my class.

Therefore I have had to do a lot of remixing and retrofitting of existing materials in order to deliver what I think is a solid intro MATLAB course, one that I honestly think satisfies the same learning objectives as the course at our partner university (which uses one of those books I mentioned above, and it works for them because they have the “right” audience for those books), but also one that really works for the decidedly non-MATLAB-like group of students I will have.

I’ve tried to adhere to a few basic design principles when drawing up this course:

• Get students comfortable with the software and how it works before throwing them into programming. But: Don’t wait too long to begin programming.
• Connect use of the software back to the mathematics courses they know: namely Calculus I and Calculus II. (The course is a prerequisite for Calculus III and is usually taken alongside Calc II.) And take it very easy on any other kind of math in the course.
• Get them doing plots and working with data from Excel files early and often. Pictures and data: Students dig these.
• Make heavy use of the Symbolic Toolbox and get them using it early as well. And by “Symbolic Toolbox” I really mean MuPad, not the nearly-indecipherable symbolic manipulation done inside the MATLAB command line by calling the Symbolic Toolbox. This is actually quite different than how most intro MATLAB materials do it; if you see the Symbolic Toolbox at all, it’s the last chapter of the book and MuPad is never mentioned.
• Take it easy on the science content of the course and instead emphasize use of MATLAB on topics with which the majority of the class is familiar and comfortable. In the canonical audience, that would mean precisely that a lot of science ought to show up. For us, not so much.
• Have fun first, foremost, and throughout. Remember that many of the students in the course are not only unfamiliar with computers (aside from using them to check Facebook or use a word processor) but are actually scared of them — probably a greater portion of the class than will let on to it. Keep it light. If something looks more confusing than useful, it probably is; and unless there’s some compelling reason to hack through it, just drop it and cover something else.

I think that with these principles in mind, a lot of MATLAB purists would look at my course and sniff at it, thinking it’s not a “real” MATLAB course. It certainly will not walk or talk like the MATLAB course you’ll find at the typical Big Engineering School. But we’re not a big engineering school; we’re a liberal arts college, and the Liberal Arts informs what we do in every course, including a MATLAB course. I guess I am not trying to create a course that in turn creates more MATLAB purists. I am trying to create a course that shows students that programming is a great application and instantiation of critical thinking and problem solving. If they don’t know all the command line options to fprintf, but later on when confronted with a problem they first thing they think is to try out some MATLAB code to solve it, I think we’ve had a successful experience.

That’s an overview of the philosophy and design of the course. In another post, I’ll talk about the course schedule, assessments, and plans for what we’ll do in the class. This gets interesting because, as mentioned above, we’re not really using a book but rather just McGyvering a bunch of pre-existing resources to fit our particular needs.