MCD 4140 Computing for Engineers Self Study Exercise 4
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
MCD 4140 Computing for Engineers
Self Study Exercise 4
Note: The debugging technique discussed in Lecture 10 can be very useful while solving the problems below.
Task 1
The machine Epsilon (ε) is the distance from 1 to the next larger number. This means that computing 1 + ε/2 will produce a result of 1.
Write MATLAB code that finds ε using the following steps:
1) Set ε = 1
2) If 1 + ε is greater than 1, do Steps 3 and 4. Otherwise go to Step 5
3) ε = ε / 2
4) Return to Step 2
5) ε = ε * 2
Check your answer against MATLAB’s eps() function.
Task 2
Write a MATLAB program to evaluate the function
y(x) = ln
for any user-specified value of x, where ln is the natural logarithm (logarithm to the base e). Write the program with a while loop, so that the program repeats the calculation for each legal value of x entered into the program. When an illegal value of x is entered, terminate the program.
Task 3
Consider the following MATLAB code, ignoring the comments in green (%):
% fprintf('%3s\t%3s\t%3s\t%3s\n', 'k', 'b', 'x', 'y');
k = 1; b = 1; x = -1; y = -2;
while k <= 3
% fprintf('%3d\t%3d\t%3d\t%3d\n', k, b, x, y);
y = x^2 - 2;
if b >= y
for n = 1:k
b = b + 1;
end
end
x = x + 2;
k = k + 1;
end
% fprintf('%3d\t%3d\t%3d\t%3d\n', k, b, x, y);
Complete the following table with the values in the variables immediately after the while k <= 3 line in the M-file. Do this for every pass of the while loop. You may need fewer or more rows than provided in the table.
|
k |
b |
x |
y |
While Pass 1 |
1 |
1 |
-1 |
-2 |
While Pass 2 |
|
|
|
|
While Pass 3 |
|
|
|
|
While Pass 4 |
|
|
|
|
End of M-file |
|
|
|
|
Check your results by running the code above in MATLAB after uncommenting the fprintf commands. Only do this after completing the table above.
Task 4
For the case where the user enters the value 8.217 and another case 126, trace the sample MATLAB code below and determine what is printed to the screen.
x = input( 'Enter a positive number: ');
while (x ~= int8(0.0) & ~mod(x,1) & x <= int8(128))
x = x + 1;
disp(x)
end
disp(x)
Task 5
Convert the following FOR loop into a WHILE loop. Note: There is more than one way to do so, but the results in the t array and y array should be the same.
t=1:0.5:20;
for i=1:length(t)
y(i)=1/t(i)^2+4*t(i)*sin(t(i));
end
Task 6
Write a MATLAB program to determine if an integer number is prime using FOR loop. Your program should get an integer from the user and store it in variable n. your program should then search through all integers between 2 and n-1. If n is divisible by any of these numbers, it is not prime. Otherwise, n is a prime number. A flow chart is provided below to enhance your understanding.
START |
Get Integer ‘n’ |
Set ‘Counter’ = 1 |
Set ‘Prime’ = True |
STOP |
Task 7
A point in a 2-dimensional array is said to be a relative maximum if it is higher than any of the 8 points surrounding it. For example, the element at position (2,2) in the array shown below is a relative maximum, since it is larger than any of the surrounding points.
「11 −7 L 2 |
7 14 − 3 |
−2] 3 5 」 |
Write a program to scan for all relative maxima within the matrix shown below. The program should only consider interior points within the matrix, since any point along an edge of the matrix cannot be completely surrounded by points lower than itself. Test your program by finding all of the relative maxima in the following matrix.
「 2 | | −3 | |−4.5 A = |−3.5 | −9 | 7 |L −6 |
− 1 0 −3 −4 −3 −6 −4 −5 |
−2 −2.5 −3 −7 −5 −5 −5 −5 |
1 5 3 6 0 −3 −3 −2 |
3 −2 0 1 4 1 2 0 |
−5 2 0 −3 17 2 4 1 |
2 1 − 1 0 11 0 3 2 |
1 ] | | | | | | | | | | | |
Task 8
rms average =
Write a single program that calculates the arithmetic mean (average) and the rms average (equation given above) for a set of positive numbers. Use any method that you desire to read in the input values from user. Compare these values for each of the following sets of numbers.
a. 4, 4, 4, 4, 4, 4, 4
b. 4, 3, 4, 5, 4, 3, 5
c. 4, 1, 4, 7, 4, 1, 7
d. 1, 2, 3, 4, 5, 6, 7
Task 9
Write an M-file that uses FOR loop to calculate the sum of all data in each row and each column in an array. The size of the array is fixed to 5 by 5. This array is to be defined in your M-file as shown below:
array = [ 33.0 -12.0 16.0
-6.0 -14.0 3.5
4.4 1.1 -7.1
0.3 6.2 -9.9
1.8 2.2 1.1
0.5
11.0
9.3
-12.0
7.3
-1.9
2.1
-16.1
6.8
26.0];
Display the output in the format shown below:
Sum |
of |
row 1 = 35.6 |
Sum |
of |
row 2 = -3.4 |
Sum |
of |
row 3 = -8.4 |
Sum |
of |
row 4 = -8.6 |
Sum |
of |
row 5 = 38.4 |
Sum |
of |
col 1 = 33.5 |
Sum |
of |
col 2 = -16.5 |
Sum |
of |
col 3 = 3.6 |
Sum |
of |
col 4 = 16.1 |
Sum |
of |
col 5 = 16.9 |
Task 10
Convert the following for loops into while loops
(a) ires = 0;
for index = -10:10
ires = ires + 1;
end
(b) ires = 0;
for index1 = 10:-2:4
for index2 = 2:2:index1
if index2 > 6
break;
end
ires = ires + index2;
end
end
Task 11
Examine the following while loops and determine the value of ires at the end of each of the loops, and the number of times each loop executes. (a) ires = 1;
while mod(ires,10) ~= 0
ires = ires + 1;
end
(b) ires = 2;
while ires <= 200
ires = ires^2;
end
(c) ires = 2;
while ires
2022-12-01