This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS100M Lab Exercise 14 Writing efficient code 1. Download the script LargestTriangle from the Section Exercises page. The script (also shown below) is a first attempt at finding the largest triangle that can be formed from n points on a unit circle. Add code ( tic , toc ) to the script to determine how long it takes to find the answer for n = 100 , 150 , 200. Store the results (time) in vector t1 such that t1(i) corresponds to n ( i ), i = 1 , 2 , 3. for n=100:50:200 theta = rand(n,1)*2*pi; % 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 Herons 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;...
View Full Document
- Fall '07