Professional Documents
Culture Documents
NATIONAL UNIVERSITY OF SINGAPORE
SCHOOL OF COMPUTING
QUIZ 2 FOR
Semester 1 AY2012/2013
IT1005 – Introduction to Programming with MATLAB
30 Oct 2012 Time Allowed: 95 Minutes
INSTRUCTIONS TO CANDIDATES:
1. This examination paper contains FIVE (5) questions and comprises NINE (9) printed pages,
including this page. The weightage for each question is as indicated, and total 50 marks.
2. Answer ALL questions within the spaces/boxes provided in this booklet.
3. This is an Open Book examination.
4. You are allowed and encouraged to use calculator.
However, any other form of electronic devices is NOT allowed to be used during examination.
5. Please write your matriculation number below. Do NOT write your name.
MATRICULATION NO: ________________________________
This portion is for examiner’s use only
1
IT1005
A Chemistry lab technician of an NUS Chemical engineering lab has just procured some items: He
bought x binocular microscope, y distillation apparatus, z burners, and he spent A SGD in total.
This lab technician told you the following riddle: Buying x binocular microscopes actually cost the
same as his new laptop, which is B SGD. He also told you that buying 2y distillation apparatus and
x binocular microscopes costs the same as buying five new iPhone 5, which is C SGD per iPhone 5.
Based on some “GIYF searches”, you know that one binocular microscope like the one bought by
the lab technician is 600 SGD, one distillation apparatus is 80 SGD, and one burner is 45 SGD.
a). Use the skill that you have learned in IT1005 to compute what is x, y, and z, if A = 4505 SGD,
B = 3000 SGD, and C = 824 SGD. Show all MATLAB commands that you need (4 marks).
This is just 3 unknowns in 3 linear equations; A clear Linear Algebra problem; best: use ‘\’, e.g.
[45 80 600; 0 2*80 600; 0 0 600] \ [4505; 5*824; 3000]
z=ans(1), y=ans(2), x=ans(3) % note that [z y x] = A\b causes error
Note 1: fsolve solution will only get 3 marks (may not be the best/most accurate solver in
general). If you know that your system of equations is linear, use ‘\’ operator that does not ‘guess’!
Note 2: Please use proper MATLAB syntax for answering this question. 3 marks otherwise.
b). However, your laptop is just spoilt , thus you do not have access to MATLAB in your laptop…
You are so curious and want to compute x, y, z MANUALLY using an algorithm that you know…
Show the full working of the required steps to get the marks (6 marks).
For clever manual solution, we need to rearrange the variables in the equations (x, y, z) to (z, y, x)
so that matrix A has lower triangular matrix all zeroes right from the start!!!
45 * z + 80 * y + 600 * x = 4505
0 * z + 2*80 * y + 600 * x = 5*824
0 * z + 0 * y + 600 * x = 3000
So, x = 5 (from 3000/600)
And therefore, y = 7 (from (5*824 – 3000) / (2*80))
And finally, z = 21 (from (4505 – 3000 – 80*7) / 45)
This is doable in less than 3‐4 minutes . A full simulation of Gaussian Elimination using the order
x, y, z as “implied” in the question will take ~15‐20 minutes (or more… depending on your speed).
2
IT1005
% you can use the entire page 3 if you need more space for question 1b
% make sure that your steps are as clear as possible
Actually, you do NOT need this page at all if you use clever manual solution above.
However, the full Gaussian Elimination steps using x, y, z ordering is shown below:
Initially:
600 80 45 | 4505
600 160 0 | 4120
600 0 0 | 3000
600 80 45 | 4505
0 80 -45 | -385
0 -80 -45 | -1505
600 80 45 | 4505
0 80 -45 | -385
0 0 -90 | -1890
Back substitution:
z = -1890 / -90 = 21
So:
y = (-385 + 45*21) / 80 = 560 / 80 = 7
Therefore:
x = (4505 - 80*7 - 45*21) / 600 = 3000 / 600 = 5
Some rough statistics: About ~90% of the cohort got z = 21, y = 7, and x = 5 correctly.
About ~50% do not use this blank box in page 3 (the quick method, creative thinking (or luck)).
About ~50% write a lot in this blank box in page 3 (the standard method as taught in class).
3
IT1005
Steven wants to give a memorable birthday present to his one year old daughter: Jane Angelina Halim
(24 October 2011-12). Last year, he told IT1005 students about his savings plan for Jane’s education needs
(which is still ongoing!). This year, he wants to give Jane a MATLAB cake as shown below.
Your task is to write several MATLAB User-Defined Functions to compose the cake.
This task has nothing to do with ChBE, but be a good student and create a birthday cake for Jane!
a). Function DrawCircle takes in three parameters, x, y, and r from user and returns nothing.
It then plots a red circle centered at coordinate (x, y) with radius r. Write this function below (2 marks).
function DrawCircle(x, y, r)
plot(x + r * sin(0:0.1:2*pi), y + r * cos(0:0.1:2*pi), 'r');
% already shown in Lecture 06 & lab 05
% marking scheme: 0 (blank or deemed as blank), 1 (wrong),
% or 2 (fully correct) marks => axis equal is marked in Q2c.
% common mistakes:
% -forgot to use red color for the circle border
% -wrong circle equation formula
% -forgot to shift the circle to be centered at (x,y)
b). Function DrawLine takes in four parameters x1, y1, x2, and y2 from user and returns nothing.
It then plots a blue line from (x1, y1) to (x2, y2). Write this function below (2 marks).
4
IT1005
c). Function DrawCake takes in one parameter age from user and returns nothing.
It then plots one big circle centered at coordinate (0, 0) with radius 3 units, then plots age copies of
“candles”. A candle is a combination of a straight line with 2 units of height plus a small ‘fire’ which is a
circle centered at the top of the line with radius 0.2 units. The base of a candle must be placed randomly
within the cake (i.e. the base of the candle must be strictly inside a circle centered at coordinate (0, 0)
with radius 3 units). It is OK for two candles to accidentally overlap (or too near to another candle) because
of random placement. Finally, write ‘Happy Xst/nd/th Birthday Jane’ as the title of the plot, but replace X
with age. See the screen shots below for DrawCake(3) and DrawCake(7).
function DrawCake(age)
clf; % not graded, but important for several calls of DrawCake
hold on; % two important steps, 1 mark if both steps exist
axis equal; % (this can also be written in Q2a)
DrawCircle(0, 0, 3); % call this, 1 easy mark
for i = 1:age % repeat 'age' times, 1 mark
x = 3; y = 3; % 1 mark for getting random (x, y) correctly
while x * x + y * y >= 9 % if outside circle (the cake)?
x = rand() * 6 - 3; y = rand() * 6 - 3; % try again
end
% SIMPLER ANSWER: you can just do this (not that 'random')
% x = rand(); y = rand();
% (x, y) will be a random coordinate between (0, 0) and (1, 1)
% which is guaranteed to be inside the cake ALL THE TIME!
% Other alternative, put the candles randomly around
% a smaller circle inside the cake
% angle = rand() * 2 * pi; % same angle for x and y
% x = 2 * sin(angle); y = 2 * cos(angle);
5
IT1005
You have just done a chemistry experiment in module CNABCD and got the “Quantity (Q) versus time (h)”
data stored in a 1D row vector named as result. This data is obtained after length(result) hours in
lab, one data point per hour. Your CNABCD lecturer told you that if the experiment is conducted perfectly,
you should get a linear fit: Q = A*t + B, where A and B are two constants. However, you were a bit sloppy
and your data looks like the figure below if plotted (example with 7 data points or 7 hours of experiment).
You immediately realize that you have done a serious measurement error on the 6th hour.
If you ‘delete’ the 6th data point, you will get a much better linear fit: Q = 2*t + 0, which is shown below:
(DISCLAIMER: DO NOT DO THIS IN YOUR ACTUAL CHEMISTRY EXPERIMENT!)
Your task is write a User Defined Function DetectOutlier that takes in a row vector result as defined above
and returns one number, the index of an element in row vector result which removal improves the linear fit of
row vector result in the most significant manner. You can only delete exactly one data point. It is guaranteed
that there is exactly one clear outlier in all test data used in this question.
You can use MATLAB’s polyfit as shown in lab08. The help message of polyfit is shown below.
P = POLYFIT(X, Y, N) finds the coefficients of a polynomial P(X) of degree N that fits the data Y
best in a least-squares sense. P is a row vector of length N+1 containing the polynomial coefficients in
descending powers, P(1)*X^N + P(2)*X^(N-1) + … + P(N)*X + P(N+1).
6
IT1005
% 3 more test cases other than the 4 listed above (unique answer):
% >> DetectOutlier([9 8 7 6 5 4 1])
% ans = 7, Q = -1*t + 10, to break notion that Q is always 2*t + 0
% >> DetectOutlier([1 1000 1 1 1 1 1 1 1 1]) % result = 1x10!!
% ans = 2, Q = 0*t + 1, to break notion that Q must have a gradient
% >> DetectOutlier([1050 3001 4999 7002 9001 10999 12999 15001])
% ans = 1, Q ~= 1999*t - 999, large gradient/coefficient, imprecise
7
IT1005
0.95 W
2
D 0.01V 2
Friction V
Lift
where D = drag force, = ratio of air density between flight altitude and sea level, W = weight, and V =
velocity. The two factors, ‘friction drag’ and ‘lift’ contributing to drag in the above formula are affected
differently as velocity increases; friction drag increases with velocity, while the drag due to lift decreases. The
combination of the two factors leads to a minimum drag.
(a) If = 0.6 and W = 16,000, use MATLAB to determine the minimum drag and the velocity at
which it occurs. Write down all the code/commands you need clearly (4 marks).
% In Command Window
[v_for_mindrag mindrag] = fminsearch(@(V) drag(V, 16000), 1)
(b) How does this minimum vary in the range of W = 12,000 to 20,000 with = 0.6? Use MATLAB to
answer this question, by plotting the minimum drag versus W in the above range. Write down all
MATLAB code you would need to do this. Make any assumptions that you deem fit, and state them
clearly (6 marks).
8
IT1005
The expression for the temperature T of a non-isothermal stirred tank reactor for a simple exothermic reaction
A B is given by the solution to the following equation:
k C P
0 C Af H (T f T )
1 k
where CAf = feed concentration of A, H = heat of reaction, = density of reaction mixture, CP = specific heat
of reaction mixture and Tf = feed temperature, all of which are given to you as parameters in the problem. The
reaction rate constant k can be written as k = k0 exp(-E/RT) where you are given the values of activation
energy E, universal gas constant R and pre-activation factor k0.
You are required to help a CN5020 student to plot the operating temperature of the reactor T, as a function of
residence time in the range of 100 s to 2500 s. How would you use your MATLAB expertise to do that?
9