Docsity

ME 2004: Test 1 - MATLAB Exercises and Questions - Prof. Brian Vick, Exams of Mechanical Engineering

Toggle sidebar
bg1
Name__________________________ Fall 2006
ME 2004: Test 1
Test rules: open book, open notes, computers not permitted.
Problem 1: 15%
Consider the step function step(x) defined as:

0x,0
0x,1
)x(step
Write a function m-file that evaluates and returns the value of step(x) for any scalar value x. No
documentation is necessary.
function y = step(x)
if x < 0
y = 0;
else
y = 1;
end
Problem 2: 20%
What output does MATLAB produce from the following operations?
Matlab Statements Matlab Output
for i=1:2:5
x = i^2;
end
x
x = 25
if 2 < 1
disp('Yes')
else
disp('No')
end
No
x = 0.3;
while x < 1
x = 2*x;
end
a = x
a = 1.2
x = 1
while x > 0
x = x + 1
end
x = 1
x = 2
x = 3
…
caught in an endless loop
1
pf3
pf4
pf5

Partial preview of the text

Download ME 2004: Test 1 - MATLAB Exercises and Questions - Prof. Brian Vick and more Exams Mechanical Engineering in PDF only on Docsity!

Name__________________________ Fall 2006

ME 2004: Test 1

Test rules: open book, open notes, computers not permitted.

Problem 1: 15%

Consider the step function step ( x ) defined as:

  0 , x 0

1 , x 0 step(x )

Write a function m -file that evaluates and returns the value of step(x ) for any scalar value x. No

documentation is necessary.

function y = step(x)

if x < 0

y = 0;

else

y = 1;

end

Problem 2: 20%

What output does MATLAB produce from the following operations?

Matlab Statements Matlab Output

for i=1:2:

x = i^2;

end

x

x = 25

if 2 < 1

disp('Yes')

else

disp('No')

end

No

x = 0.3;

while x < 1

x = 2*x;

end

a = x

a = 1.

x = 1

while x > 0

x = x + 1

end

x = 1

x = 2

x = 3

caught in an endless loop

Problem 3: 15%

Consider the matrix

1 2 3 4

1 2 3 4

1 2 3 4

a a a a

A b b b b

c c c c

. Suppose a 1^ ,^ a 2^ ,^ b 4^ , and c 3^ are positive numbers and

all the other entries in A are negative numbers.

(a) Let B denote the matrix defined by the MATLAB statement B = A(1:2,3:4). List the

matrix B.

 

 

 

3 4

3 4

b b

a a B

(b) Let C denote the matrix defined by the MATLAB statement C = A([1,3],[1,3,4]).

List the matrix C.

 

 

 

1 3 4

1 3 4

c c c

a a a C

(c) Let P denote the matrix defined by the MATLAB statement P^ (A^ ^ 0 ). List the matrix P.

0 0 1 0

0 0 0 1

1 1 0 0

P

Problem 5: 25%

A zero of the function f(x ) shown below is to be found using the Newton-Raphson method.

(a) Starting with an initial guess of x 0 = 3, what is the next estimate, x 1? Show this step

graphical on the above figure.

x 1 = 5.

(b) What value will the Newton-Raphson method converge to using an initial guess of x 0 =3?

xr = 4.

(c) What value will the Newton-Raphson method converge to using an initial guess of x 0 =8?

xr = 9.

(d) Briefly describe what will happen if we start with an initial guess where f^ '^ ( x )^0.

Demonstrate your answer on the above graph.

The slope is zero, thus, the tangent is zero and never crosses the x-axis. For instance, the tangent

at the point around 6.5 is shown on the graph.

Also, the iteration formula '( )

1 i

i i i f x

f x xx   results in division by zero.