# Some final exam solutions

**Posted:**May 27, 2011

**Filed under:**Debrief, Final exam, How-To, Programming, Worked examples |

**Tags:**Final exam, Function, Programming Leave a comment

Now that the class is completely done, I thought I’d post up some sample solutions to some of the exam problems.

Here’s some code that implements the divisors function that was specified in Problem 1 of the Programming area:

function d = divisors(n) d = [1]; for i=2:n if mod(n,i) == 0 d = [d i]; end end

In English, here’s what this function does. We’re going to create a vector (think of it as a list) that holds all the divisors of n. That vector will be called “d”, and that’s specified in the first line of the function where we set up the standard starting code. Well, the number 1 is a divisor of n no matter what, so we start off with d = [ 1 ]. Then the way the function works is that it just checks each number between 2 and n (the number that the function was given by the user) to see if that number divides n. How do we know if a number divides n? That’s where the MOD function comes in. If you played with the MOD function and took the hint given on the final, you’ll quickly realize that a number x divides n if and only if mod(n,x) = 0 — that is, if x divides n with a remainder of 0.

So for each number i between 2 and n, check to see if mod(n,i) == 0. If so, add i to the list, because it’s a divisor. Otherwise, just move on to the next number and stop when we reach n.

Here’s the other programming problem, the bigratio function:

function c = bigratio(v) c = 0; for i=2:length(v) if v(i)/v(i-1) > 2 c = c + 1; end end

This program works similarly to divisors. We are trying to count the number of times the ratio of successive entries of the vector v is bigger than 2. We initialize that count at 0 for starters. Then, for each entry in v, divide v(i) by v(i-1). Up the count by 1 if that fraction is bigger than 2; iterate to the next counter value otherwise, and stop when you reach the last two elements in the vector.

# Final exam for CMP 150, Spring 2011

**Posted:**May 22, 2011

**Filed under:**Uncategorized Leave a comment

Later, I will post some of the solutions to this exam.

# Sample M-file for you to examine (for projects)

**Posted:**May 5, 2011

**Filed under:**How-To, Information, Projects, Worked examples Leave a comment

Here’s the M-file used by Mike Garfield in his presentation on Wednesday. I am posting this with his permission. I thought his was a very good example of formatting style, interweaving well-written text with MATLAB code and mathematics and done up in a nice, clickable format. The raw MATLAB code is below. Copy and paste it into an editor, and then publish to HTML for the full effect. You may have to uncomment some of the lines to get the code to run.

Thanks Michael!

Remember, you do NOT need to turn in an HTML file for your projects — just an M-file that I will eventually publish as HTML myself.

%% CMP 150 Demo Project: Regular Expressions % By: Michael Garfield %% What is a regular expression? %% % A Regular Expression is a string of characters that defines a certain % pattern %% % For example, %% % We could search a document for the string '(Will\w)', which defines a % particular pattern of letters. The syntax reads, the pattern starts with % W-i-l-l and ends with (\w) any number of word characters (letters). So Matlab % would search a document for strings matching this pattern. It could % return the results of: %% % Will, William, Willy %% Why is Regular Expression Useful? %% % The Regular Expression tool could be useful by it is a more efficient way % to search for patterns within a text without knowing exactly what form the % word takes. This allows the user to find all forms of the pattern they % are searching for instead of only one form. %% % A simple example would be, %% % There are two differant ways to express kilometers: %% % km % kilometers %% % If you were searching through the text of a math problem and wanted to % know what form of kilometer was used within the text you would simply: %Enter the text you are wanting to search text = ['The ship set sail and traveled 350 km south ',... 'and then would sail east at 50 miles/hour ',... 'then it would dock before sailing 400 kilometers north.']; %Create a variable for what you are searching for pattern = 'k(ilo)?m(eters)?'; %Then use the regex command regexp(text, pattern, 'match') %It should output in the command window: %% Another Example %% % Here is another example, but a slightly more complicated form of the % previous example. %% % Suppose we wanted to search for kilometers/hour in a similar text document. %% % There are many different forms kilometers/hour could take %% % km/h % km/hr % km/hour % kilometers/hour % kilometers per hour %% % You could try and search for all five of these separately by using five % distinct search commands, but you could search for any form using the % regexp command. %% % Like the previous example, need to set a variable to show what to look % for. %This is telling Matlab to look for a string that has at a minimum k-m-/-h, %plus any extra characters that may follow each letter. Pattern1='k(ilo)?m(eters)?(/|\sper\s)h(r|our)?'; %Now we must input the text for the program to search through. Text1 = ['The high-speed train traveled at 250 ', ... 'kilometers per hour alongside the automobile ', ... 'travelling at 120 km/h.']; %Now we call the regexp command to search for the parameters given above. regexp(Text1,Pattern1, 'match') %After running the command, the output should be:

# Information and grading rubrics for demo projects

**Posted:**April 28, 2011

**Filed under:**Assignments, How-To, Information, Projects |

**Tags:**Assignment, project Leave a comment

We will begin the first round of project presentations **NEXT Wednesday, May 4** with Coen, Michael (Garfield), Liz, and Linda. Again, if you would also like to go on May 4, please let me know — I could fit in up to four more people on that date.

You have already claimed your project and submitted your proposals. The only thing left grade-wise in the project is the presentation and the M-file that contains your writeup. Here is information and grading rubrics for each of those two pieces.

The presentation of your demo is worth 35 points. The presentation will consist of a brief but informative demonstration of the MATLAB functionality described in the topic you chose. **You should aim for no more than 5 minutes in length for your demo** — any more than that, and we will run out of time in the class period. So your demo should be well-planned, concise, to the point, and smoothly conducted. The demo should be done entirely from MATLAB — no PowerPoint slides or boardwork, unless these are essential to your presentation.

Once you present your demo, the rest of the class will be given an opportunity to rate your presentation on a scale of 0–5 (0 = Poor, 5 = Excellent) using clickers. There may be follow-up questions as well, so you should be prepared to answer these about your topic.

Five of the 35 points for the presentation will come from the peer grading. The other 30 will be given using three criteria, assessed on a scale from 0 to 10:

- Organization
- Content
- Presentation

Each criteria will be assessed using some questions pertaining to the criterion. You can ask yourselves these questions while you are preparing your presentations to ensure the best grade possible. This form is posted here: Demo project presentation grade sheet CMP 150.

The M-file you are preparing for your demo is described in the original blog post announcing projects. It is to be an M-file, formatted like a professional document, and exported to HTML for posting on the internet. The M-file must:

- Be professionally formatted with sections, section and subsection headings, and links to sections;
- Have text interspersed with calculations;
- Use correct English spelling and grammar, including complete sentences throughout; and
- Look professional when published in HTML format.

To clarify: You will only be submitting the M-file, not the HTML version of it when published.

We have covered how to format M-files for HTML publishing earlier in the course. Here’s the official MATLAB documentation file on formatting your M-files for publication, and it contains some HTML examples worth studying. Here is another web page about HTML publishing, and be sure to check out the self-test with the sample code at the end. If you need more help than this, please ask for it — I can provide you with print resources and other kinds of help on this.

The M-file you submit will be graded on the basis of two criteria, each graded from 0 to 10:

- Appearance — Does the M-file publish to HTML in a way that is attractive, usable, and professional?
- Content — Does the M-file correspond to the oral presentation, and does it present the demo in a clear, organized, logical fashion?

# Lab Problem Set for Apri 27

**Posted:**April 27, 2011

**Filed under:**Assignments, Lab Problem Sets |

**Tags:**Lab problem sets, mupad Leave a comment

Note the formatting specifications in the problem set especially. You’re turning in ONE MuPad notebook with all your work in it.

# Guided Practice for April 27

**Posted:**April 20, 2011

**Filed under:**Assignments, Guided Practice |

**Tags:**Assignment, Guided Practice 1 Comment

I’ve decided to change up the topics for April 27 and May 4. In both of these weeks, we’ll be shifting focus to MATLAB’s capabilities with *symbolic* calculations rather than numerical ones.

## Overview

MATLAB obviously has lots and lots of features, but there are also things called toolboxes that you can purchase that add even more functionality to the software. For example, there are toolboxes for advanced statistical analysis, financial mathematics, bioinformatics, and so on. One of those toolboxes is the Symbolic Math Toolbox, which extends MATLAB’s capabilities to include symbolic operations such as differential and integral calculus, solving equations, and so forth. Franklin College purchased a license for the Symbolic Math Toolbox when we purchased our MATLAB license, and the Symbolic Math Toolbox comes with the student version of MATLAB. This week will be the first of two weeks, our last two weeks of work in the course, in which we’ll explore what the Symbolic Math Toolbox can do. We will begin by learning about MuPad, a program within MATLAB that gives a simple, user-friendly interface to working with all kinds of symbolic operations. Hopefully you can use MuPad right away in your current math courses to do calculus, algebra, plotting, and other things.

Click the link to proceed. Read the rest of this entry »

# Lab Problem Set for April 13

**Posted:**April 13, 2011

**Filed under:**Assignments, Lab Problem Sets |

**Tags:**Lab problem sets Leave a comment

You get until Friday of this week to finish this up, since the photo shoot today might be a bit disruptive.