Matlab Programming
Matlab Programming
Assignment 4
Due: Mon, 1/31/11, 11:59pm EST
Objective:The objective of this assignment is to familiarize yourself with MATLAB and dead reckoning. With the exception of problem 1, the following exercises are meant to be answered by either a script or a function m-le (you decide). The descriptions are complete but the nature of the input/output and display options is left to you. Also, some problems are much simpler than others. Suggestion: start simple and add complexity. What to turn in:In a single PDF le, submit your clearly labeled and commented MATLAB script, all supporting MATLAB functions you wrote, AND the outputs as displayed in the command window. In addition, place all the requested MATLAB script and functions in a separate zipped le and submit the zipped le with your single PDF le. 1. (10 pts)(By Darryl Morrell) Answer the following questions. (a) What will the code shown in Algorithm 1 print? Algorithm 1 Pseudocode for 1(a) p1 = 3.14; p2 = 3.14159; if p1 == p2 then disp(p1 and p2 are equal) else disp(p1 and p2 are not equal) end if (b) For what values of the variable a will the code shown in Algorithm 2 print Hello world? Algorithm 2 Pseudocode for 1(b) if a < 3 || a > 10 then disp(Hello world) else disp(Goodbye world) end if (c) For what values of the variable a will the code shown in Algorithm 3 print Hello world? (d) Write an if statement that will print a is very close to zero if the value of the variable a is between 0.01 and 0.01. Name your function close2zero user.m. Replace user with your drexel.edu e-mail username. For example, for my two functions, I would name my functions close2zero mah349.m
Algorithm 3 Pseudocode for 1(c) if a >= 0 && a < 7 then disp(Hello world) else disp(Goodbye world) end if Algorithm 4 Pseudocode for 2(a) n = 10; while n > 0 do disp(Hello World) n = n - 1; end while 2. (5 pts)(By Darryl Morrell) Answer the following questions. (a) How many times will the code shown in Algorithm 4 loop print Hello world? (b) How many times will the code shown in Algorithm 5 loop print Hello world? Algorithm 5 Pseudocode for 2(b) n = 1; while n > 0 do disp(Hello World) n = n + 1; end while (c) What values will the code shown in Algorithm 6 print? (d) What values will the code shown in Algorithm 7 print? 3. (10 pts)In this exercise you will write a program that converts a Roman numeral to its decimal equivalent. (a) Write a MATLAB that takes a Roman numeral and converts it to its decimal counterpart following the old style where the order of the symbols does not matter. In this case, IX and XI both mean 10 + 1 or 11. You should be able to handle the following conversion table: Roman I V X L C D M Decimal 1 5 10 50 100 500 1000
(b) Write a separate function that takes a Roman numeral and converts it to its decimal counterpart following the new style where the order of the symbols does matter. For example, IX is 2
Algorithm 6 Pseudocode for 2(c) a=1 while a < 100 do a = a*2 end while Algorithm 7 Pseudocode for 2(d) a = 1; n = 1; while a < 100 do a = a*n n = n + 1; end while 9 = (10 1), XC is 90 = (100 10). The conversion table given above still holds and you may assume for this case that the only instances of order you will encounter are IV (4), IX (9), XL (40), XC (90), CD (400) and CM (900). IMPORTANT: Name your functions roman2dec old user.m and roman2dec new user.m respectively. Replace user with your drexel.edu e-mail username. For example, for my two functions, I would name my functions roman2dec new mah349.m. 4. (10 pts)For this last problem you will code up the software for a vending machine in MATLAB . Assume the vending machine sells 10 dierent items all for the same price. The vending machine can be in any of four states: x1 - waiting for money, x2 - waiting for user to select item, x3 - vending item, and x4 - returning change. In each of these states, the machine has a set of actions that it needs to execute. For example, if the machine is in the x1 state, it needs to prompt the user for money. Once the user has put in enough money, the machine should ask the user to choose on of the available items. Once the user has selected the desired item, the machine should vend the item or let the user know that the selected item has run out. At this point, the machine can either return the money to the user or prompt the user to select a dierent item. Using what you have learned in lecture, select 10 dierent items for a vending machine. Now design the software that would run on the vending machine. Your main progam should be named vendingMachine user.m such that user is replaced with your Drexel e-mail username. You are highly encouraged to write separate functions so as to enable the vending machine to execute the various commands. The functions input and disp will be helpful here. Extra Credit: (5 points)Assume you have 10 dierent items such that some of are priced dierent from the others. Change your code to accomodate the dierent prices. You should name your main program vendingMachineEC user.m where user is replaced with your Drexel e-mail username.