MH1401_lab4(3)

4
MH1400 Algorithms & Computing I AY 2013 Ivica Nikoli´ c / Thomas Peyrin 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 files as requested. Once you are done with it, submit the file 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 file separately and not in a Zip file or similar. The submission system closes at the deadline. Hence after that, you will get no marks for your solu- tion. 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 file when the dimensions of the data comply, i.e. in each line of the file, 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 file 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 file. Then try to read the file 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 file 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 flowing in dependency of the angle of a receiver (no more details available). The results have been stored in the file ExperimentalData.txt , which you can download from edventure. The first 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. 1

description

sdfg

Transcript of MH1401_lab4(3)

Page 1: MH1401_lab4(3)

MH1400 Algorithms & Computing I AY 2013Ivica Nikolic / Thomas Peyrin

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 files as requested. Once you are done with it,submit the file 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 marksotherwise. Please name the scripts according to the requirements, and uploadeach file separately and not in a Zip file or similar. The submission systemcloses at the deadline. Hence after that, you will get no marks for your solu-tion.

1. Write a script PlotBar.m to create a vector x with values ranging from 7 to 77 insteps 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 file when the dimensions of the data comply, i.e.in each line of the file, 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 thematrix to a file called tester.dat. Then create a new matrix with integersfrom the same range, but with dimension 2 x 5. Append this matrix to theoriginal file. Then try to read the file in with load. What do you get?

(b) Repeat the above experiment, but this time, the second matrix has a dimension2 x 6. Can you read the file in this case?

3. A function can return a vector as a result. Write a function vecout.m that willreceive one integer argument and will return a vector that increments from the valueof 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 flowing in dependency ofthe angle of a receiver (no more details available). The results have been storedin the file ExperimentalData.txt , which you can download from edventure. Thefirst column represents the angle (in degrees) and the second column the current (inmA). Write a script PlotData.m that will load this data into a matrix, separate itinto vectors, and then plot the data. Make sure your plot looks exactly like the plotbelow (it is green color) including range of the axes, title and axis labels.

1

Page 2: MH1401_lab4(3)

35 40 45 50 55 60 65 70 75 800

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Angle (in degrees)

Curr

ent (in m

A)

Some Physics Experimental Data

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 thatwill 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 returnthe 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 exactamount of time, expressed in the format hours:minutes:seconds, that has elapsedbetween two points of time in a day. The script will do the following:

� prompt the user to input the two time points, where the first is smaller thanthe second

� compute the total amount of seconds (store in a variable totalSeconds) thathas elapsed between these two times

� call a function <YourMatricNo> Lab4 myHours to compute the value for thehours (store in amountOfHours), given totalseconds

� call a function <YourMatricNo> Lab4 myMinutes to compute the value for theminutes (store in amountOfMinutes), given totalSeconds, amountOfHours

� call a function <YourMatricNo> Lab4 mySeconds to compute the value for theseconds (store in amountOfSeconds), given totalSeconds, amountOfHours,

amountOfMinutes

� output these three values

2

Page 3: MH1401_lab4(3)

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 file, along with the three files 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 productat 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 returnthe 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 thevalue of n as an input argument, and calculates and returns the cost of manufactur-ing n units. Once you have confirmed that both the script and the function workproperly, 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 file is trivial with MATLAB. In one such file, calledsalaries.dat (can be downloaded from edveNTUre), are stored the salaries ofworking individuals along with their age and years of experience. The age of theperson is in the first 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 file and willplot the salary as a function of the age as well as a function of the experience. Moreprecisely, your script will do the following:

� Read the file salaries.dat and store the data into a matrix.

3

Page 4: MH1401_lab4(3)

� Output the number of rows of the matrix, in the form:>> plotsalaries

There are 17 salaries in the file.

� Plot in two different figures, the two functions – the first with solid green line,the second with dashed red line. In both of the figures, make sure you usexlabel, ylabel, title.

Important!!! It should not be assumed that the number of lines in thefile is known.

10. An approximation for a factorial can be found using Stirling’s formula:

n! =√

2πn(n/e)n

Write a function approxNFact to implement this, passing the value of n as an argu-ment.

11. If you have time left, type helpbrowser and read about Plots and Plotting Tools in/MATLAB/User’s guide/Graphics/Plots and Plotting Tools

4