Você está na página 1de 5

University of Edinburgh

School of Mathematics
Dr. Jörg Kalcsics

Computing for OR and Finance


Sheet 2 for Sept 26, 2016

Notes
• The questions marked with ∗ are challenging, hence you should try them
after attempting rest of the questions.

• If there are any questions, you can contact me: joerg.kalcsics@ed.ac.uk.

Exercise 2
2.1 Output formatting
Let a, b ∈ R be two numbers and A ∈ R2×3 be a matrix.

(1) Write an expression such that both numbers are printed with 4 decimal places
and a total width of at least 10.

(2) Write an expression such that both numbers are left aligned with a leading
sign and printed with 4 decimal places and a total width of at least 10.

(3) Use sprintf to convert the matrix A into a string. What happens?

(4) Using a single sprintf command, can you print the matrix in exactly the
same way as when typing disp(A) in the command window, but now with
the formatting options?
Hint: there exist special character codes that can modify the printout of a
string. For example, \n induces a line break in the string.

1
2.2 Simple logic
Let a, b ∈ R be two numbers.

(1) Write and test a logical expression that returns logical true, if a is
(a) strictly greater than 1
(b) less than or equal to −3
(c) in the open interval (1, 2).

(2) Write and test a logical expression that returns logical true, if
a) a or b is positive, or both
b) either a or b is positive (but not both).

2.3 Array logic and logical indexing


Use rand to create a 10-by-10 matrix A of uniformly distributed random numbers,
and use it to perform the following tasks:

(1) Using the function nnz, count how many entries of A are > 0.5.

(2) Use logical indexing to display all entries of A that are > 0.5 and use find
to display their indices.

(3) Use array logic to count how many elements of A are > 0.5 and < 0.9.

(4) * Use logical indexing to set all entries of A that are <= 0.5 to 0.

(5) * Create a vector with integers from 1 to 1000. Use the function mod and
array logic to count how many integers between 1 and 1000 are divisible by:
• 2 or 3;
• 2 and 3.

2.4 Loops and branching


(1) Write a for loop to display the word “hello” 10 times.

(2) Write and test a for loop that iterates the equation x = cos(cos(x) − x) one
thousand times, starting with x = 1.
Display the value of x with 15 positions after decimal point.

2
(3) Modify your loop by adding an if statement and a break statement, so that
the loop stops if | cos(cos(x) − x) − x| < 10−8 .

(4) Rewrite your code using a while loop to achieve the same thing.

(5) Write and test a switch block that asks the user to input an integer value n
between 1 and 7, and then displays the corresponding day of the week, i.e.,
Monday, if n = 1, Tuesday, if n = 2, etc. Write your code so that a warning
message is displayed if n does not correspond to any day of the week.

(6) Create a vector v of numbers from 1 to 100. Use a loops to set all entries of
v that are divisible by 2,3 and 5, except for 2,3 and 5 themselves, to zero.
How would you proceed from this point onwards, if you would like to delete
all non-primes from v?

2.5 * Output formatting revisited


Let A ∈ Rn×m be a matrix, n, m ≥ 1, with −100, 000 ≤ aij ≤ 100, 000, ∀ i, j.

(1) Write a Matlab code that displays the matrix on the screen such that:
• the width of each column is exactly 8
• columns are separated by: |
• the numbers in each column do not exceed the column width of 8
• the numbers are printed with four decimal places; if this is not feasible,
as many as possible should be printed.
For example, for  
3.2145 −71295.28
A=
67629.31651 0
the output should be
3.2145 | -71295.3
67629.32 | 0.0000

(2) Change your code such that the result is not printed on the screen but into
a file with the name “formatting.txt”.

3
2.6 * RSA encryption
Remember the application at the end of Lecture 1. By “chance”, you have recently
intercepted a message from Katja to Sebastian:

[3346 27 4464 4580 1478 1542 268 4322


27 1244 1478 1798 27 4464 1159 2692]

Sebastian’s public key is (e, n) = (43, 4699).

Decode the message.

Hints
During the de- and encryption, the numbers r d and r e are getting very big very
fast. Too big for atlab to handle them accurately.
However, as we are taking them modulo n, there is a work-around. If we do the
multiplication step by step, we can take the modulo after each multiplication to
avoid overly large number. For example

r 4 mod n = ((r 2 mod n) · r mod n) · r mod n .

2.7 * Travelling Salesman Problem (TSP)


Consider again the example of the travelling salesman introduced in Lecture 2.
For larger problems, it is prohibitive to iterate over all permutations of cities to
determine the optimal tour.
A very simple, yet reasonable heuristic to compute an approximate solution for
this problem is the Nearest Neighbor Heuristic:

1. Start in city 1.

2. Choose the next city to be visited as the one that is closest to the current
city and has not yet been visited.

3. If not all cities have been visited yet, go to Step 2.

4. Return to city 1.

4
Determine for the following TSP a tour with the Nearest Neighbor heuristic:
 
0 60 101 43 72 37 68 61 13 66
 60 0 134 98 108 44 118 82 56 72
 
101 134 0 79 20 82 44 45 86 54
 
 43 98 79 0 47 57 29 59 48 86
 
 72 108 20 47 0 61 24 34 64 50
D = (dij ) =   37

 44 82 57 61 0 62 32 21 44 
 68 118 44 29 24 62 0 39 68 61
 
 61 82 45 59 34 32 39 0 54 21
 
 13 56 86 48 64 21 68 54 0 59
66 72 54 86 50 44 61 21 59 0

How long is the tour?


Do we obtain a different and/or better tour, if we start in another city?

Você também pode gostar