Docsity

Exam 1: Introduction to Engineering Computing - Fall Term, 2003 (04-1) - MATLAB, Summaries of Engineering

Toggle sidebar
bg1
Exam 1 Page 1 of 7
Engineering 0012: Introduction to Engineering Computing
Fall Term, 2003 (04-1)
Exam 1 - MATLAB
Name: ___Answer Key_____
1) (6 points) How many times will the display command in the following script be executed?
x = 3;
while (x < 8)
disp('Am I done yet?')
x = x + 2.5;
end
Answer: _____2___________
2) (12 points) Given the function prototype
function [out] = examfcn(in)
Write four (4) different calls to the function, each of which uses a distinctly different method to
provide a value for the required parameter. Assume any variables that you need are available.
a)
hardwire: x = examfcn(3)
b)
variable: x = examfcn(y)
c)
arith expr: x = examfcn(2*y/3)
d)
another fcn: x = examfcn(cos(y))
pf3
pf4
pf5

Partial preview of the text

Download Exam 1: Introduction to Engineering Computing - Fall Term, 2003 (04-1) - MATLAB and more Summaries Engineering in PDF only on Docsity!

Engineering 0012: Introduction to Engineering Computing Fall Term, 2003 (04-1)

Exam 1 - MATLAB

Name : ___ Answer Key _____

1) ( 6 points ) How many times will the display command in the following script be executed?

x = 3; while (x < 8) disp('Am I done yet?') x = x + 2.5; end

Answer: _____ 2 ___________

2) ( 12 points ) Given the function prototype

function [out] = examfcn(in)

Write four (4) different calls to the function, each of which uses a distinctly different method to

provide a value for the required parameter. Assume any variables that you need are available.

a) hardwire: x = examfcn(3)

b) variable: x = examfcn(y)

c) arith expr: x = examfcn(2*y/3)

d) another fcn: x = examfcn(cos(y))

Engineering 0012: Introduction to Engineering Computing Fall Term, 2003 (04-1)

3) ( 10 points ) Answer the following questions about data analysis using MATLAB.

Given the MATLAB variables (vectors)

x : independent variable

y : dependent variable

a) You decide to investigate a power-law fit to the data.

Write the MATLAB command(s) to display the data on logarithmically-scaled axes

Answer: loglog(x,y,โ€™r*โ€™)

Write the MATLAB command(s) to find a least-squares fit to the data

Answer: coeff = polyfit(log(x),log(y),1)

b) You are now investigating an exponential fit to the data.

The MATLAB polyfit command

coeff = polyfit(x,log10(y),1)

returned the following

coeff = 3 2

Write the corresponding equation in (x,log 10 (y)) notation

Answer: log 10 (y) = 3x+

Write the corresponding equation in (x, y) notation

Answer: y = 10 (3x+2) = 100( 3x )

Engineering 0012: Introduction to Engineering Computing Fall Term, 2003 (04-1)

6) ( 10 points ) Answer the following questions about functional analysis using MATLAB.

a) fcn_name has been declared as an inline function.

Write the MATLAB command(s) to find a zero near x = 1.

Answer: xzero = fzero(fcn_name,1.5)

Write the MATLAB command(s) to find a minimum between -1 < x < 1.

Answer: xmin = fminbnd(fcn_name,-1,1.5)

b) fcn_name has been declared as the string: '2*x./(x.^4+2)'.

Write the MATLAB command(s) to find a zero near x = 1.

Answer: xzero = fzero(fcn_name,1.5)

Write the MATLAB command(s) to find a minimum between -1 < x < 1.

Answer: xmin = fminbnd(fcn_name,-1,1.5)

c) fcn_name has been assigned the path\filename of MATLAB m-file.

Write the MATLAB command(s) to find a zero near x = 1.

Answer: xzero = fzero(fcn_name,1.5)

Engineering 0012: Introduction to Engineering Computing Fall Term, 2003 (04-1)

7) (20 points) Write the screen display for the following script and associated functions in the space

provided. Credit only given for work shown.

% script (^) Display

Display 1 x = 4 2 y = 3 3 out2 = 0 0 0 0 0 4 out1 = 12 (^5) out1 = 8 6 back 1 = 12 0 0 8 0 7 out1 = 7 8 back2 = 7 9 done 10 11 12 13 14 15 x = 4 y = 3 z = [2 4 5 3 1]; for i = 1:2: if (i <= 2) back1 = fcn1(i,x,y,z) elseif (i > 3) disp('done') else back2 = fcn2(i,y,x,z) end end function [out2] = fcn1 (j,y,x,k) out2 = [0 0 0 0 0] for i=1:3: out2(i) = fcn2(i,x,y,k); end function [out1] = fcn2 (j,d,s,k) switch (k(j)) case {1,2} out1 = d+ case {3,4} out1 = 2*s otherwise out1 = d+s end

Engineering 0012: Introduction to Engineering Computing Fall Term, 2003 (04-1)

(c) ??? Index exceeds matrix dimensions.

Error in ==> C:\temp\eqnsolver.m On line 14 ==> rhs = augmat(:,Ncol); (1) Offending line of code, Line #: 12 (2) Correction: [Nrow, Ncol] = size(augmat);

(d) ??? Error using ==> /

Matrix dimensions must agree. Error in ==> C:\temp\eqnsolver.m On line 17 ==> soln = Amat/rhs; (1) Offending line of code, Line #: 17 (2) Correction: soln = Amat\rhs; soln = inv(Amat)rhs; soln = (Amat^(-1))rhs;

(e) ??? Error: File: C:\temp\eqnsolver.m Line: 21 Column: 14

")" expected, "identifier" found. (1) Offending line of code, Line #: 21 (2) Correction: disp('The solution vector is')

(f) What will be displayed if help eqnsolver is entered at the command window prompt?

script to solve linear set of equations A*x = b requires augmented coefficient matrix file