Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Semester One 2018

Examination Period

Faculty of Engineering

ENG1060

COMPUTING FOR ENGINEERS - PAPER 1

PART A: ATTEMPT ALL QUESTIONS

Question A1 (7 marks)

Consider the following matrices:

A = [ 16    47    42]

B = [66      3      10]

C =  [68    76    74]

Where A, B and C are double types.

Note: If a MATLAB statement returns an error, write down "error".

(a) Provide the syntax to create A, B and C.

(b) Provide the output of X = B-C

(c) Provide the output of [a,b] = size(C)

(d) Provide a single-line syntax to create the following matrix by only addressing entire rows

(not individual elements) of A, B and C.

S = [66

80

3

76

10]


(e) Provide the output of T = transpose(B)

(f)  Provide the output of U = sum(A)

(g) Provide the output of V = find(A==B)

(h) Provide the syntax to add a 4th column to B which contains elements in the 1st column of B

raised to the power of 3.

Question A2 (6 marks)


Consider the following MATLAB function:

function [vr, reality] = helminth(sn,bt,dcp)

pre = sum([sn,bt,dcp]);

trans = sqrt(abs(bt – sn));

post = sn.*bt;

reality=0;

vr=0;

if  pre < 5

reality = floor(pre);

elseif pre > 15

reality = ceil(pre);

else reality = pre.^2;

vr = post.^3;

end

Note: If a MATLAB statement returns an error, write down error .

(a) What are the input and output variables in the function declaration above?

(b) Provide the name of the function and the extension format of the file?

(c) What is the output of the following command?

[vr, reality] = helminth(9,6,3)


(d) Consider x = [9, 9], y = [6, 6], and z = [3, 3]. What is the result of:

[a, b] = helminth(x, y, z)?

(e) Is it possible to convert the function provided at the start of this question to an anonymous

function? If yes, provide the syntax. If no, write "error" and explain why.

(f)  MATLAB provides two warnings for the function provided at the start of this question.

Describe ONE of these warnings.

Question A3 (6 marks)


Consider the following matrix defined by L = [ -4:3:8; 4:-2:-4 ] and K = [ 1:5 ; 6:10 ], and logicals A = 0 (false) and B = 1 (true). Write the results of the MATLAB statement as specified in each    question below .

Note: If a MATLAB statement returns an error, write down error .

(a) Provide the output of A | B

(b) Provide the output of A & B

(c) Provide the output of F = L > 0

(d) Provide the output of G = (~(L > 0) & (K < 4))

(e) Replace the (square) symbol in the following syntax H = K L so that it provides the

logical result:

H = [

0

0

0

0

1

0

0]

Provide the complete expression below .


(f)  Provide a single-line syntax to create a logical-type matrix which contains true

elements when the equivalent elements of L are odd.

(g) Describe why short-circuit operators are used.

Question A4 (8 marks)


Answer the following multiple-choice questions by writing the letter corresponding to the correct answer in the table provided below . Note: only one letter can be written in each box for each question. An example is provided below:

EXAMPLE: Which exam is this unit for?

A.  ENG1001

B.  ENG1002

C.  ENG1003

D.  ENG1005

E.  ENG1060

QE:

E

Q1:

Q2:

Q3:

Q4:

1.  Which one of the following is an invalid variable in MATLAB?

A.  Witchwood = 5+6;

B.  Hagatha witch = 3^2;

C.  Phantom9 = pi + [1 2 3];

D.  R0tt3n = [3, 5]

E.  Militia_shaw = [3 5; 6 7]

2.  What is the MATLAB function for finding the natural logarithm of x?

A.  log10(x)

B.  nlog(x)

C.  10log(x)

D.  log(x)

E.  None of the above

3.  Which of the following statements creates a logarithmically spaced vector from

100 to 105 (inclusive) with 60 points?

A.  logspace(10^0, 10^5,60)

B.  logspace(10^0,60,10^5)

C.  logspace(0,5,60)

D.  logspace(0,60,5)

E.  None of the above

4.  What are the plot characteristics of the following command? plot(t, d, ‘ro- ‘)

A.  Red circles, dashed line

B.  Red circles, continuous line

C.  Orange rectangles, dashed line

D.  Red, dashed-dot line

E.  Orange rectangles, continuous line

5.  A .txt file which contains only numerical data is imported using X=importdata(). Which of the following is true?

A.  X is a structure

B.  X is a string

C.  X is a double

D.  X is empty

E.  X is a character

6.   Using tline = fgetl at the end of an open file in MATLAB results in tline equal to which of the following?

A.  0 (logical)

B.  -1 (double)

C.  1 (double)

D.  1 (logical)

E.  -1 (string)

7.  Which of the following anonymous functions replicates the following function file?

function R = revs(x,y,z)

R = x.^2 + y./z

end

A.  R = @(x) x.^2 + y./z

B.  R @(x,y) = x.^2 + y./z

C.  R = @(x,y,z) x.^2 + y./z

D.  R = @(all) x.^2 + y./z

E.  None of the above

8.  Which of the following statements is true about the following code?

A=2; B = A + eps(A)/100

A.  A is equal to B

B.  A is less than B

C.  A is greater than B

D.  B is undefined

E.  Error



Question A5 (6 marks)


Write MATLAB code for the following scenarios, ensuring that the commented instructions are followed.

(a) Prompt the user for a value of x, and determine the value of y based on the following cases.


y(x) = {  cos(x)

for

for

for

x < −1

−1 ≤ x 5

x > 5

% prompt the user for x

% use if and elseif statements to

determine y

(b) The function primes(N) creates a vector containing prime numbers from 1 to N (inclusive). Given z=primes(1000), determine how many values in z are less than 500.

z = primes(1000)

counter = 0;

% use a for loop to go through each value of z

% use if statement to determine if counter should be incremented

(c) Starting with x=13.6, continue to double x until it is larger than 1337.

x=13.6;

% use a while loop to check if x is larger than 1337



Question A6 (7 marks)


The figure below shows plots y2 against x and y3 against x. where:

y2 = x 2 n2

y3 = x 3 n 3

Here, x is a vector of linearly spaced values from -3 to 3 (inclusive) with 300 points. The n variable is a vector of linearly spaced values from 1 to 4 (inclusive) with 300 points.

The line specifications for both plots are black continuous lines. The y3 plot has a maximum which is marked by a black circle.

Write MATLAB code in the following parts to reproduce the figure.

(a) This is the start of the m-file. Clear all variables, close all figure windows and clear the command window.

% start of m-file

(b) Create all relevant variables for plotting. Use  element-by-element  operators  where appropriate.

% variable creation


(c) Determine the maximum y3 value and the corresponding x value.

% max y3 and corresponding x value

(d) Plot y2 against x in the left panel of the subplot and label the plot accordingly. The line specification is a black continuous line.

% plot y2 against x

(e) Plot y3 against x in the right panel of the subplot and label the plot accordingly. The line

specification is a black continuous line. Also, mark the maximum y3 value with a black circle on the same plot. Refer to the figure at the start of this question.

% plot y3 against x

% mark the maximum y3 value

PART B: ATTEMPT ALL QUESTIONS

Question B1 (15 marks)

The average concentration of a substance c̅ (g/m3 ) in a lake can be computed by integration via:

c(z)A(z) dz

A(z) dz

where z is the depth below the surface in metres, the area A and concentration c vary with depth, and D is the maximum depth in metres. Here, D=16. The average concentration can be calculated based on the following data:

z (m)

0

4

8

12

16

c(z) (g/m3)

10

8.5

7.4

5.2

4.1

A(z) (m2)

9.8

5.1

1.9

0.4

0

c(z)A(z) (g/m)

98

43.35

14.06

2.08

0

(a) Use the Composite Simpson's 1/3 rule with 4 segments to calculate the numerator integral

term ( c(z)A(z) dz) in the average concentration equation over the entire depth of the lake.

Show ALL your working and provide answers to 4 decimal places.

Dc(z)A(z) dz =

(b) Use the Composite Trapezoidal rule with 4 segments to calculate the denominator integral

term (A(z) dz) in the average concentration equation over the entire depth of the lake. Show

ALL your working and provide answers to 4 decimal places.

DA(z) dz =

(c) Hence, calculate the average concentration to 4 decimal places.

c̅ =

Consider now that the substance will be transferred to another location via a channel. The average flow rate Q can be calculated as:

B

0

where B is the total channel width (11m), D is the depth (m) and y is the distance from the bank (m). The distance and corresponding velocity-depth product is provided in the following table.

y (m