

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Unknown 2008;
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS100M Lab Exercise 14
for n=100:50: theta = rand(n,1)2pi; % Angle of random pts on the unit circle % Determine how long it takes to compute the largest % possible triangle obtained by selecting vertices from the % points represented by theta A = 0; for i=1:n for j=1:n for k=1:n % theta --> Cartesian c1 = cos(theta(i)); s1 = sin(theta(i)); c2 = cos(theta(j)); s2 = sin(theta(j)); c3 = cos(theta(k)); s3 = sin(theta(k)); % Area using Heron’s Formula a = sqrt((c1-c2)^2 + (s1-s2)^2); b = sqrt((c1-c3)^2 + (s1-s3)^2); c = sqrt((c2-c3)^2 + (s2-s3)^2); s = (a+b+c)/2; Aijk = sqrt((s-a)(s-b)(s-c)*s); A = max(A,Aijk); end end end end
Notice that there are several levels of inefficiency. The area for each different triangle is computed 6 times. Modify the loop ranges to eliminate this redundancy. Also, there are a lot of redundant sine and cosine evaluations. Address this issue by moving the c1, s1, c2 and s2 assignments. In Part 2, store the time taken to do the computation in vector t2 such that t2(i) corresponds to n(i). How much speed-up did you get?
Even with the change in where we compute c1, s1, c2 and s2, we are still doing more sine and cosine evaluations than necessary—given n values of theta we should only need to make n sine evaluations and n cosine evaluations. This suggests that we can reduce the time further by precomputing the sine and cosine of theta. We will combine this insight with another improvement in Part 3 below.
Final note. The speed-up that we get isn’t all “free.” The speed-up that we gain from precomputation has a cost in computer memory—from version 1 to version 3, the major memory requirement increases from n (length of theta) to n^2 (dimension of D). The problem at hand, the language, and the hardware are all considerations in the trade-off between speed and memory.
Please delete your files from the computer before you leave the lab.