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

Semester One 2017

Examination Period

Faculty of Engineering

ENG1060

COMPUTING FOR ENGINEERS - PAPER 1

PART A: ATTEMPT ALL QUESTIONS

Question A1  (6 marks)

Consider the following matrix:

= ,  6     1      ,   6 , =

Where X and Y are double types.

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

(a) Provide the syntax to extract the 2nd row of matrix X

(b) Provide the syntax to extract the 1st column of matrix Y

(c) Provide the output of transpose(X)

(d) Provide the output of X.^Y

(e) Provide the syntax to extract the 2x3 matrix comprised of elements common from the 2nd  and 3rd  rows, and the 1st , 2nd  and 5th  columns of X. You must not address individual elements.

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

(g) Provide the output of D = ones(size(X))

(h) Provide the output of [H1, H2]=size(X)

Question A2  (8 marks)

Answer the following short questions:

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

(a) Describe the function cosd(A) where A is a scalar

(b) Provide the output of B = linspace(4, 16, 5)

(c) Consider C1 = '246810'. Provide the output of C2 = str2num(C1)

(d) Provide the syntax to create a new figure window

(e) Provide the output of D = mod([8 6 3], 4)

(f) Describe the plot characteristics of the following command:

plot(x,y,'mro')

(g) Provide the syntax to extract the imaginary component of a complex number G

(h) Provide the syntax to compute the natural log of Y

(i)  Describe the difference between fprintf and sprintf

Question A3   (9 marks)

(a) Given the following code that calculates the height and surface area of different

cylinders (for a fixed cylinder volume), provide the MATLAB commands to obtain the height and radius which corresponds to the cylinder with minimum surface area

V = 10; %volume

r = 0.1:0.01:3; %radius

%height and surface area

h = 3*V./(pi*(r.^2)); %height

SA = pi*r.*sqrt((r.^2)+h.^2); %Surface area

[A, B] = min(SA) %minimum of SA

%The two MATLAB commands will begin from here

(b) The population of a newly-built town is governed by the following equations.


population =

time for ‘ < 。

time  for

A student has written code attempting to determine the population for time=0 to 10   with increments of 1, and then summing the population for all time values. However, the  following code is incorrect and produces a result of B = 55. Describe the fix to  the code that is needed such that the correct value of B = 145 is obtained. You do    not need to rewrite the correct code, just briefly describe the two fixes in the box on  the next page.

%non-working code

time = 0:10;

for i = 1:length(time)

if time < 5

population = time.^3;

else

population = time;

end

end

B = sum(population)

(c) The following code produces a figure with one curve. Provide the MATLAB

command to be added where the comment appears to produce a figure with two curves in it

(y vs. x and z vs. x).

x = 1:10;

y = x.^2;

z = sin(x);

%command to be provided here

plot(x,y,'bo')

plot(x,z,'ro')

(d) The volume V of liquid in a hollow horizontal cylinder of radius r and length L is related to the depth of the liquid h by

, = ! cos-! - - ℎ

The following code was written by a student who has not done ENG1060 and who doesn't know where to apply appropriate element-by-element operators and

therefore places the period character everywhere. Circle the incorrect uses of the element-by-element operators in the code below.

%variables

r = 3; % radius

l = 5; % length

h = 0:0.1:2*r; % empty to full cylinder

% volume

v = l.*( r.^2.*acos((r-h)./r) - (r-h).*sqrt(2.*r.*h -

h.^2) );

% 50% volume threshold

v50 = 0.5.*(max(v)-min(v)).*ones(size(h));

(e) Provide the output printed to the command window when the following code is run.

x = -3:0;

y = 3:6;

prod = x.*y;

fprintf('The product is %.0f\n', prod)

(f)  Complete the following for loop to replace the diagonal elements of matrix F with its corresponding row/column index. Write the for loop in the box provided below.  The body of the for loop should be a single line.

F=[78 39 42; 68 92 46; 1 0 77; 60 46 32]

[m,n] = size(F)

for

%complete this for loop with a single line

end

(g) The following code prompts the user for number which should be positive but not equal to 5.  Complete the while loop (including the “logical test”) so that the code continues to prompt the user for a new number if invalid numbers are provided to the input.

number = input('Input a positive number that is not 5: ')


while (“logical test”)

%complete this while loop with a single line

end

Question A4  (6 marks)

Consider the following MATLAB function:

function [miss, list] = expansion(planet,dance,check)

miss = planet./dance.^2;

list = sin(dance).*miss;

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 [miss, list] = expansion(1, 2, 3)

c)  Provide the output of [miss, list] = expansion([1 4], [2 5], [3 6])

d)  What is the output of [Y,Z] = expansion(9, 8, 7, 6)?

e)  Explain why it is not possible to "run" function files in general.

f)   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.

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

Question A5  (5 marks)

Write  a  MATLAB  script  which reproduces  the  figure  below.  The  figure  contains  two subplots  in a vertical arrangement. The vector t is comprised of 1000  equally  spaced points with limits of 0 to 4 (inclusive). The equations for distance d, and velocity v are expressed as:

= 4.219 -!.!"‘ - -!.!"‘

= 26.67。-!.!"‘ - 6.67。-!.!"‘

These two equations are provided to you in the MATLAB script as anonymous functions which must be used. The diamond marks the minimum value of v.

Ensure the following:

- Clear all variables at the top of the script.

- Close all windows at the top of the script.

- Clear the command window at the top of the script.

- Axis labels and title.

-    Appropriate line specifications for the plot. All lines and symbols are black.

Write MATLAB code below.

% anonymous functions provided. Use them!

x = @(t) 4.219*(exp(-1.58*t) - exp(-6.32*t));

v = @(t) 26.67*exp(-6.32*t) - 6.67*exp(-1.58*t);

Question A6  (6 marks)

Consider the following matrices:

20

92

42

98

54

13

0

1

0

49

34

95

74

27

。。 94 42

, =

30

70

67

70

74

18

100

17

3

, =

1

0

1

0

1

0

1

0

1

Where G, H and K are double types.

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

a)  Provide the output of A = (G == H)

b)  Provide the output of B = (G>50) & (K==1)

c)  Provide the output of C = (G>50) | (H<50)

d)  Provide the output of [D1, D2] = find(H==70)

e)  Provide the output of E = G(K) + H(K)

f)   Provide the output of F = ~logical(24*K)

PART B: ATTEMPT ALL QUESTIONS

Question B1 (15 Marks)

The following plot shows the function = + 5x +‘ over the range -2.8 ≤ ≤ 0.

Figure 1

(a) Using the Newton-Raphson method, calculate the root of the above equation to an accuracy of 0.01. Fill in the details of each iteration in the table below. You may not  need to use all rows.

Note: Workings for this part is not required.

Iteration Number

Initial Value

Xi

Estimated Root

Xi+1

1

-1.0

2

3

4

5

(b) Complete the given MATLAB function that uses the Modified-Secant method to locate the root off(x) and the number of iterations, by filling in the answer boxes.

function [root, iter] = modisecant(f, xi, pert, precision)

% General purpose algorithm for Secant Method

%

% INPUTS:

%  - f: function handle of the equation to be solved

%  - xi: the initial guess

%  - pert: a small perturbation

%  - precision: stopping criteria determined by the user

%

% VARIABLE:

%  - fxpx: function value at the estimated root xi + pert

%  - fxi: function value at the estimated root xi

%

% OUTPUT:

%  - root: the root of the equation

%  - iter: total iterations taken

% Calculate values for fxi and fxpx

% Initialize iteration count

% Iteration for Modified Secant method starts

while

% Update iteration value

% Calculate the new estimated root

xi = xi - pert*fxi/(fxpx-fxi);

% Update values for fxi, fxpx

end

% Set return values of root and iter

(c) With the aid of a sketch, illustrate how the modified-secant method works in finding the root for the above equation for the first two iterations.

(d) Assume now you are to use the bi-section method to solve for the root, with the initial brackets [Xl , Xu]. How will you check and update the sub-interval for the root?

%  - f: function handle of the equation to be solved

%  - xl: lower limit of the initial guess/bracket

%  - xu: upper limit of the initial guess/bracket

% - xr: root estimate

% Write the code to check the current interval

% bounds and determine how to change them

xr = (xl+xu)/2;

fxr = f(xr);

% Calculate

% Calculate

the midpoint

f(xr)

% checking subinterval for root

if

else

end

(e) With the aid of a sketch, illustrate how the bi-section method works in finding the root for the above equation for the first two iterations. [Xl = -2.8, Xu = 0].

Question B2 (15 marks)

During an experiment to estimate distance traveled, velocities at seven evenly spaced points in time were logged (Table 1). We can now attempt to estimate the distance

travelled, we know:

= dt

t

1.00

4.00

7.00

10.00

13.00

16.00

19.00

V(t)