# lab14 - CS100M Lab Exercise 14 Writing efficient code 1...

• Lab Report
• 2

This preview shows pages 1–2. Sign up to view the full content.

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 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 2. We now start to make the computation more efficient. Append

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Summer '07
• FAN/VANLOAN
• Trigonometry, cosine evaluations

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern