This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Assigned: October 2 Fall 2009 Due: October 14 E7 Laboratory Assignment 5 The purpose of this lab is to introduce you to cell arrays, structure arrays and machine precision. Note: You will use MATLAB’s publish command (accessible in the Editor window via File -> Publish to HTML ) to generate a printable document that contains your code and your answers. You must turn in the printed copy to the appropriate homework boxes in 1109 Etcheverry by 12:00 pm on WEDNESDAY October 14 . Your M-file used to generate the HTML document must be uploaded to bSpace, also by noon. Name this file lastname firstname lab04.m. For the simple Run each of the following commands exercises, please follow the new format. • Describe, in a comment after the command, what the command does. • Aim for 10 words or less (not a strict limit but rather a guideline). • After the comment itself, insert one, blank commented line, separating this comment / command pair from the next. • Although you will run the code without terminating semicolons (so you can see the output, and learn what is happening), when you are ready to publish the final copy, please place a semicolon after these commands in your m-file to suppress the output. This will shorten the length of your final paper. The readers will focus their attention to the (concise) comments you have written. IMPORTANT: Don’t forget to publish your separate M-files in your main script using type . 1. (a) Write a function with the header line function num = Day2Num(day) using the switch statement that takes a single string argument and returns the corresponding number of day , assuming that the week starts on Sunday (i.e. Sunday is 1, Monday is 2, etc.). Your function should be able to return the appropriate number regardless of the letter casing in day . If day is misspelled, return NaN . Test your function for day=’tUesDay’ . Solution: begin code 1 function num=Day2Num(day) 2 switch lower(day) 3 case ’sunday’ 4 num = 1; 5 case ’monday’ 6 num = 2; 7 case ’tuesday’ 8 num = 3; 1 of 22 Assigned: October 2 Fall 2009 Due: October 14 9 case ’wednesday’ 10 num = 4; 11 case ’thursday’ 12 num = 5; 13 case ’friday’ 14 num = 6; 15 case ’saturday’ 16 num = 7; 17 otherwise 18 num = NaN; 19 end 20 21 >> Day2Num(’tUesDay’) 22 ans = 23 3 end code (b) Write a function with the header line function totaldays = DaysInMonth(month,year) using if statements, relational operators, and strcmp that will take two arguments, month and year , and return the numbers of days in the month of that year. Your function should be able to compute leap years where the month of February will return 29 days. In addition, your function should be able to return the appropriate number regardless of the letter casing in month . If month is misspelled, return NaN . (Hint: look up leap year in Wikipedia to determine the formula that will calculate which years are leap years) Solution: begin code 1 % Solution 1 2 function totaldays=DaysInMonth(month, year)...
View Full Document
This note was uploaded on 08/14/2010 for the course E 7 taught by Professor Patzek during the Summer '08 term at Berkeley.
- Summer '08