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

MCD4140 Computing for Engineers

Self Study Exercise 3

Task 1

Suppose that x = 6. Find the results of the following operations by hand and use MATLAB to check your results.

1) z = (x<10)

2) z = (x==10)

3) z = (x>=4)

4) z = (x~=7)

Task 2

Suppose that x = [10, -2, 6, 5, -3] and y = [9, -3, 2, 5, -1]. Find the results of the following operations by hand and use MATLAB to check your results.

1) z = (x<6)

2) z = (x<=y)

3) z = (x==y)

4) z = (x~=y)

Task 3

Assume that the following variables are initialized with the values shown, and calculate the result of the specified expression.

value1 = 1;    value2 = -10;  value3 = 0;

1) ~value1

2) value1 | value3

3) value1 & value3

4) value1 & value2 | value3

5) value1 & (value2 | value3)

6) ~(value1 & value3)

7) value1 + value2 < ~value3 + value3

Task 4

Suppose that the variables a, b, c, b1, b2, b3 contain the values -10, 0. 1, 2.1, true, false and false. What will the result be if the following lines are executed?

1) a > b | b > c

2) (~a) | b1

3) b1 & ~b2

4) a < b == b < c

5) b1 | b2 & b3

6) b1 & b2 | b3

Task 5

Rewrite the following statements to use only one if statement.

if x < y

if z < 10

w = x*y*z

end

end

Task 6

The figure below shows a cylindrical tank with a conical base. If the liquid level is quite low, in the conical part only, the volume is simply the volume of the immersed part of the cone. Once the liquid has filled the entire cone the cylindrical part begins to fill. The radius of the cylindrical part is R.

Use IF statement to write an M-file to compute the liquid’s volume as a function of dimensions R and d. Design the IF statement so that it returns the volume for all cases where the depth is less than 3R. Return an error message “over top” if you overfill the tank, that is, if d > 3R. (Hint: When d

< R, assume that the radius of the cone is equal to d.)

Test it with the following data:

R [m]

1

1

1

1

d [m]

0.5

1.2

3.0

3.1

Task 7

Write  an  M-file  using  conditional  statements  to  evaluate  the  following function, assuming that the scalar variable x has a value. The function is

y = ex+1  . for x < −1 ,  y = 2 + cos(πx) ; for −1 ≤ x < 5  and  y = 10 (x − 5) +1 ; for

x ≥ 5 . Use your file to evaluate y for x = −5 , x = 3 and x = 15 .

Task 8

Write a MATLAB program to evaluate the function y(x) = ln    1     For any

1 − x .

user-specified value of x, where x is a number <  1 (note that ln is the natural logarithm, the logarithm to the base e).  Use an  IF statement to verify that the value passed to the program is legal. If the value of x is legal, calculate y(x). If not, write a suitable error message and quit.

Task 9

The type of path taken by a satellite is determined from the value of the eccentricity of the conic section,

e=0, free-flight trajectory is a circle;

e=1, free-flight trajectory is a parabola;

e<1, free-flight trajectory is a ellipse;

e>1, free-flight trajectory is a hyperbola.

Write a function named trajectory” where e is the input and trajectory type is the output. Use an IF structure to determine the type of trajectory base on the value of e.

Task 10

Write a MATLAB program to evaluate a mathematical function f(x,y) for any two user specified values x and y. The function f(x,y) is defined as follows:

 x(x)y2y

f (x, y) =    2

x2x y2y

x ≥ 0 and y ≥ 0

x ≥ 0 and y < 0

x < 0 and y ≥ 0

x < 0 and y < 0

Example output of this program is shown below:

>> funcxy

 

Enter the

x value: 2

Enter the

y value: 3

The value

>> funcxy

of the function is 5

Enter the

x value: 2

Enter the

y value: -3

The value

>> funcxy

of the function is 11

Enter the

x value: -2

Enter the

y value: 3

The value

>> funcxy

of the function is 7

Enter the

x value: -2

Enter the

y value: -3

The value

of the function is 13