SUNY Geneseo Department of Computer Science
CSci 120, Spring 2014
Prof. Doug Baldwin
Complete by Friday, February 14
Although coming up with a series of Matlab statements to solve some problem, as you have done in previous lessons, involves the key elements of programming, most people wouldn’t call a set of statements that has to be executed by hand a “program.” The word “program” means a set of instructions stored in a file, so that those instructions can be executed and re-executed as often as desired simply by telling a computer to read and execute what it finds in the file. But note that once you have devised an algorithm to solve a problem, and figured out how to express that algorithm in the statements of some programming language, writing a program is just a matter of putting the statements into a file—so solving problems by interactively entering statements as we have been doing embodies all the really hard parts of programming.
This lesson teaches you how to write two forms of Matlab programs: “scripts” and “functions.” The relevant readings and online mini-lectures are:
Write the following…
Write a Matlab script that prompts its user for their name, reads an answer from the user, and then prints the message “Hello, UserName, I hope you have a good day!” where UserName is replaced with whatever name the user provided. For example, a typical run of your script might interact with the user thus (with this typeface indicating program output, and this one for user input):
What is your name? Doug Hello, Doug, I hope you have a good day
In Lesson 3 you wrote a set of Matlab statements to calculate the frequency of a sampled function. Repackage these statements as a Matlab function. This function should take the upper bound on the interval of x values over which to sample as its only argument, and should return the frequency of the function sin(6x) as estimated by sampling that function at 60 evenly spaced x values between 0 and the argument.
For example, if your function were called nyquist
, you could do the part of Lesson 3 that asked you to estimate the frequency of sin(6x) over intervals from 0 to 2π, 0 to 4π, 0 to 8π, and 0 to 16π via the following interaction with Matlab:
nyquist( 2 * pi ) ans = 0.9549 nyquist( 4 * pi ) ans = 0.9549 nyquist( 8 * pi ) ans = 0.9549 nyquist( 16 * pi ) ans = 0.2188
Bonus (doing some or all of these on top of correct solutions to the above would be one way of exceeding my expectations for this exercise): make your Matlab function take the number of samples as a second argument. Then make the Matlab function take the function that it should sample as a third argument (Matlab functions can take other Matlab functions as arguments, although nothing in the readings or mini-lectures for this lesson talks about how). Finally, make your function return both the frequency and the number of cycles it counted (Matlab functions can have multiple return values, although this again is something the current readings and mini-lectures don’t talk about).
In your study group’s first face-to-face meeting with me following the “Complete By” date above, I will look over your solutions to the problems, ask you any further questions I have, and answer any questions from you. I may also ask you to run your script or function in Matlab. Please bring the m-files you write to the meeting, on a computer that can run Matlab (each group only needs to bring one set of m-files and computer).
If you aren’t already signed up for a study group meeting during the week following this recitation, please sign up. Make the meeting 15 minutes long, and try to make it at a time all members of your study group can attend. If no such time exists, make it at a time the greatest number of members can attend.