Sunteți pe pagina 1din 4

MH1400 Algorithms & Computing I Ivica Nikoli c / Thomas Peyrin

AY 2013

Lab Questions: Fourth Lab Session


Deadline: 16.09.2013 12:00 SGT
Complete all assignments below. For those questions that are marked with an asterisk i.e. questions 6 and 7, create the script les as requested. Once you are done with it, submit the le via edveNTUre. Remember to put plenty of comments for all assignments, as this helps us to better understand your program (which might give you higher marks). Important!!! Make sure your scripts work properly, as we give 0 marks otherwise. Please name the scripts according to the requirements, and upload each le separately and not in a Zip le or similar. The submission system closes at the deadline. Hence after that, you will get no marks for your solution.
,

1. Write a script PlotBar.m to create a vector x with values ranging from 7 to 77 in steps of 2. Create a vector y which is the square root of each value in x. Plot these points using plot and bar. 2. We know that load can read from a le when the dimensions of the data comply, i.e. in each line of the le, there is the same number of elements. Test this on practice. (a) Create a 3 x 6 matrix of random integers from the interval [20, 70]. Write the matrix to a le called tester.dat. Then create a new matrix with integers from the same range, but with dimension 2 x 5. Append this matrix to the original le. Then try to read the le in with load. What do you get? (b) Repeat the above experiment, but this time, the second matrix has a dimension 2 x 6. Can you read the le in this case? 3. A function can return a vector as a result. Write a function vecout.m that will receive one integer argument and will return a vector that increments from the value of the input argument to its value plus 5, using the colon operator. For example, >> vecout(4) ans = 4 5 6 7 8 9 4. An experiment in Physics measures the current that is owing in dependency of the angle of a receiver (no more details available). The results have been stored in the le ExperimentalData.txt , which you can download from edventure. The rst column represents the angle (in degrees) and the second column the current (in mA). Write a script PlotData.m that will load this data into a matrix, separate it into vectors, and then plot the data. Make sure your plot looks exactly like the plot below (it is green color) including range of the axes, title and axis labels.

Some Physics Experimental Data 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 35

Current (in mA)

40

45

50

55 60 Angle (in degrees)

65

70

75

80

5. If a certain amount of money (called the principal P ) is invested in a bank account, earning an interest rate i compounded annually, the total amount of money Tn that will be in the account after n years is given by: Tn = P (1 + i)n Write a function that will receive input arguments for P, i, and n, and will return the total amount of money Tn . Also, give an example of calling the function. Hint: to get the required format, play with the function format. 6.

Write a script <YourMatricNo> Lab4 TimeDiff.m that will compute the exact amount of time, expressed in the format hours:minutes:seconds, that has elapsed between two points of time in a day. The script will do the following:
prompt the user to input the two time points, where the rst is smaller than the second compute the total amount of seconds (store in a variable totalSeconds) that has elapsed between these two times call a function <YourMatricNo> Lab4 myHours to compute the value for the hours (store in amountOfHours), given totalseconds call a function <YourMatricNo> Lab4 myMinutes to compute the value for the minutes (store in amountOfMinutes), given totalSeconds, amountOfHours call a function <YourMatricNo> Lab4 mySeconds to compute the value for the seconds (store in amountOfSeconds), given totalSeconds, amountOfHours, amountOfMinutes output these three values

Note, the values for the minutes and seconds cannot exceed 59. The output 13 : 23 : 70 is incorrect it should be 13 : 24 : 10. All three values should be integers. Submit your script le, along with the three les for the functions, to edveNTUre. Example: First point: Enter the hours:13 Enter the minutes:35 Enter the seconds:14 Second point: Enter the hours:15 Enter the minutes:25 Enter the seconds:4 The total number of seconds elapsed is 6590 The exact time is 01:49:50 7.

The cost of manufacturing n units (where n is an integer) of a particular product at a factory is given by the equation: C (n) = 5n2 44n + 11 Write a script <YourMatricNo> Lab4 mfgcost.m that will
prompt the user for the number of units n call a function <YourMatricNo> Lab4 costn.m that will calculate and return the cost of manufacturing n units print the result (the format must be exactly as shown below)

Next, write the function <YourMatricNo> Lab4 costn.m, which simply receives the value of n as an input argument, and calculates and returns the cost of manufacturing n units. Once you have conrmed that both the script and the function work properly, upload them to edveNTUre. Here is an example of executing the script: >> mfgcost Enter the number of units: 100 The cost for 100 units will be $45611.00 8. Write a function that is called pickone, which will receive one input argument x, which is a vector, and will return one random element from the vector. For example, >> pickone(4:7) ans = 5 9. Plotting data stored in a le is trivial with MATLAB. In one such le, called salaries.dat (can be downloaded from edveNTUre), are stored the salaries of working individuals along with their age and years of experience. The age of the person is in the rst column, the experience in the second, and the salary in the third. Write a script called plotsalaries.m that will read the data from the le and will plot the salary as a function of the age as well as a function of the experience. More precisely, your script will do the following:
Read the le salaries.dat and store the data into a matrix.

Output the number of rows of the matrix, in the form: >> plotsalaries There are 17 salaries in the file. Plot in two dierent gures, the two functions the rst with solid green line, the second with dashed red line. In both of the gures, make sure you use xlabel, ylabel, title.

Important!!! It should not be assumed that the number of lines in the le is known. 10. An approximation for a factorial can be found using Stirlings formula: n! = 2n(n/e)n Write a function approxNFact to implement this, passing the value of n as an argument. 11. If you have time left, type helpbrowser and read about Plots and Plotting Tools in /MATLAB/Users guide/Graphics/Plots and Plotting Tools

S-ar putea să vă placă și