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

Semester Two 2017

Examination Period

Faculty of Engineering

ENG1060

COMPUTING FOR ENGINEERS - PAPER 1

PART A: ATTEMPT ALL QUESTIONS Question A1 (7 marks)

Consider the following matrices:


S =

8

0

4

7

9

6

1

6

3

5

11

2

9

2

9

16

|

,

T =

0

1

0

0

0

1

|,

U = |

Where S, T and U are double types.

Note: if an output returns an error, write down “error” .

(a) Provide the syntax to create the U vector

(b) Provide the syntax to extract the 3rd column of matrix S

(c) Provide the output of transpose(T)

(d) Provide the output of C = [T; U]

(e) Provide the output of D = eye(size(S))

(f)  Provide the outputs of [F1, F2] = max(S)

(g) Provide a one-line syntax that modifies T to the following matrix without addressing

individual elements:

T =

0

1

1

0

0

1

0(0)|

(h) Provide the syntax to extract the 3x3 matrix comprised of elements common from the 1st, 3rd and 4th rows, and the 1st, 2nd and 4th columns of S. You must not address individual elements and you must complete it in a single line.

(i)  Provide the output of K=sum(S,2)

Question A2 (8 marks)

Answer the following short questions:

Note: if an output returns an error, write down “error” .

(a) Consider result = [1 5 7 9]. Provide the output of result = [result 7].

(b) Consider D = 4:3:571. Provide the output of B = D(91) D(85)

(c) Provide the function that computes the logarithmic value of X to the base 10

(d) Provide the output of imag(5 + 9i)

(e) Consider x, y and z to be vectors of the same size. State which of the following variables [A, B, C, D] contain unnecessary element-by-element operation(s):

A = x.*y +5*sin(z);

B = z./y - 7.*pi;

C = 10.^x + 4./y

D = exp(z./y) + 2/9 – cos(5.*x)

(f)  Describe the line specifications of the following function:

plot(k,m,'px')

(g) Describe the eps built-in variable in MATLAB

(h) Describe why the following line produces an error:

1st_line = 1:100;

(i)  Write a single MATLAB command that creates a vector that is logarithmically spaced between 100 to 105 (inclusive) using 700 points.

(j)  Describe the primary difference between while loops and if statements

Question A3 (6 marks)

The distance between two points in polar coordinates can be calculated using the Law of

Cosines equation          d = rA2 + rB2 rArB cos(eA eB )        where rA, rB, θA and θB are

defined in the following figure:

(a) Provide  MATLAB syntax to create a function file which calculates the distance (d) between two points in polar coordinates given the radii (rA and rB) and angles (θA and θB) of the points. You are not required to document the function and all inputs should be considered as scalars. It should NOT work with vectors. Use the following information for your function file:

Function name: LOC

Input variables: ra, rb, tha, thb

Output variables: d

(b) You  are  now working  in  a  separate  m-file.  Provide  MATLAB  code  to  calculate the distance between point A(3, π/3) and point B(4, 3π/4).

(c) Provide    the    syntax    to    print    out    the    following    statement    using    fprintf.

The distance between points A and B is <value>

where <value> represents the value contained in the variable d printed in fixed point notation with a precision of 5 decimal places.

(d) The variable d is classified as "short" if its value is less than or equal to 5, and classified as "long" if it is greater than 5. Provide MATLAB code to assign the strings 'short' or 'long' to the variable L using if statements.

(e) Assume that you are now provided row vectors for the variables ra, rb, tha, and thb. These vectors  are  of  length  n. You  are  unable to  provide these  inputs to the function file you wrote in part (a) because they are vectors. Instead, provide the syntax to compute the corresponding distances d, as a vector using a for loop.

Question A4 (8 marks)

Consider the following MATLAB function:

function [souls, HF] = DS3(nameless,king,ng)

king = round(king);

if ng < 5

HF = nameless + ng - king;

else

HF = nameless - ng + king;

end

souls = nameless .*(3/2) + ng ./king;

Gael = sin(souls) .^5;

end

Note: if an output returns an error, write down “error” .

(a) Provide the name and extension format of this function file

(b) Provide the output of [souls, HF] = DS3(1, 3.5, 5)

(c) Provide the output of [in, valid] = DS3([1 1], [4.3 4.8], [4 6])

(d) Provide the output of Y = DS3(1, 2, 3).

(e) Is  it  possible  to  turn  the  function  provided  at  the  start  of  this  question  into  an anonymous function?  If yes, provide the syntax for the anonymous function.  If no, explain why it is not possible.

(f)  MATLAB provides a single warning for the function file. Identify and describe what the warning is.

(g) Describe one difference between sub functions and nested functions in relation to the inputs of the parent function.

Question A5 (6 marks)

The figure below depicts a gear represented by an x,y curve and the location of its defects as represented by the asterisk markers. The equations for x and y are given as:

= T cos(t)

y = T sin(t)

where

T = a + tanh(b sin(nt))

with a=1, b=10, n=9 and t is a vector from 0 to (inclusive) with 7000 points in total. The coordinates of the defects correspond to the first (x,y) point and then every 600th

(x,y) point thereafter, i.e. defects occur at the 1st , 601st , 1201st , 1801st , … points.

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

Figure 1: Gear with defect locations.

(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) Plot y against x and  label  the  plot  accordingly.  The  line  specification  is  a  black continuous line.

% plotting y against x

(d) Create variables which define the x and y coordinates of the defect locations.

% defect locations

(e) Plot the defect locations on the same figure produced in part c. The  line specification is a black asterisk marker. Ensure you include the legend.

% plotting the defect locations

Question A6 (5 marks)

Consider the following matrices:

X = 3

4

9

82

69

31

95

43 ,

= 79

48

44

64

70

75

27

Where X, Y and Z are double types. Note: if an output returns an error, write down error” .

65 ,

z = 1

1

0

1

0

1

1

1

(a) Provide the output of A = (X > 50)

(b) Provide the output of B = (X == 95) | (Y < 50)

(c) Provide the output of C = (Z == 1) & (X < Y)

(d) Provide the output of [D1, D2] = find(Y==75)

(e) Provide the output of E = X(Z)

(f)  Provide the output of F = ~logical(-Z)

PART B: ATTEMPT ALL QUESTIONS

Question B1 (15 marks)

Figure 2 shows a plot of the function  Y = 3 X5 − 12.5 X3  + 8 X + 12  over the range of

0 X ≤  2.

Figure 2

(a) Write the equation whose root must be found to determine the value of X which has a Y- value equal to 10.

(b) Use the method of False Position to calculate X  at Y = 10. You should use a precision of 0.05 and employ initial guesses for the lower limit to be 0 and the upper limit to be

1.5.  Fill in the details of each iteration in the table below, provide 3 decimal places. Note: You might need less rows, but should not need more. Workings for this part are not required)

Iteration

number

Lower Limit

Xl

Upper Limit

Xu

Estimated Root

Xr

f(Xr)


(c) Write an M-file that uses the Newton-Raphson method to locate the local maximum seen  in  Figure  2.  Use  a  precision  of  10-6   and  determine  an  initial  guess  that  will converge to the local maximum of the function (but NOT the local minimum seen Figure 2).    Complete the M-file below by filling in the answer boxes with the missing code, making sure to follow the instructions in the comments.

%  Define the anonymous functions to find the maximum of the function  using the Newton-Raphson method

g =

dg =

% Define initial guess and precision

xi =

precision =

% Calculate initial values for the functions

gxi