%---------------------------------------------------------------------------- % Exercise 1 % CS100M, due in lecture Tues 9/5 %---------------------------------------------------------------------------- % Follow the submission guidelines in the Exercises page on the % website. Remember that these exercises are intended for practice and % NOT points. %---------------------------------------------------------------------------- % Part 1 % ------ % Write an algorithm for tying shoe laces. Assume the "user" of your % algorithm is a very stupid robot "who" will do everything you command. % You may use only English words. You don't have to go beserk with "code," % but you do have to be extremely explicit. The best algorithm (style, % content, look, approach, etc) will win a prize -- an autographed % copy of my Unix textbook. However, I do ask that you allow your work % to become publically available if you win. %---------------------------------------------------------------------------- % Part 2 % ------ % Run the following code in MATLAB. You might discover some frustrating % behavior. See if you can fix the code. Resubmit the corrected code % with your changes highlighted. As part of the commentary, explain % why you made the change(s). Feel free to enhance the code to add % more features. Superior programs might earn bonus points. % % If you cannot spot the bug, don't worry. We'll give you full credit % if you try. Be sure to explain what you tried in commentary, even if % the approaches didn't work. %---------------------------------------------------------------------------- %%%%%%%%%%%%%%%%%%%% % Bisection method % % 8/31/2001 % % DIS % %%%%%%%%%%%%%%%%%%%% % Note: I'm kind of throwing in comments. % We'll show a better system for comment style later. % To run, % + set your path with the command window % + enter $bisection$ after the prompt >> % (don't enter the $-signs -- I'm using them to indicate % command entry) % The program will produce an answer printed in the command window. % starting interval right=5; left=0; % count on how many iterations to rech root iterations = 0; % coefficients for equation to solve: a2*x^2 + a1*x + a0 = 0 a0=4; a1=-4; a2=1; % value that's "close enough" to zero eps = 0.001; % need value of function to get iteration going f_mid = 1; % the following loops solves for the root while abs(f_mid - 0.0) > eps mid = (left + right)/2.0; f_left = a2*left^2 + a1*left + a0; f_right = a2*right^2 + a1*right + a0; f_mid = a2*mid^2 + a1*mid + a0; % Xm is to the left of root if f_mid * f_right < 0 left = mid; % Xm is to the right of the root elseif f_mid * f_left < 0 right = mid; % either finished or something is wrong else disp('done calculating') end % increment count of iterations iterations = iterations + 1; end % print computed root disp( ['the root is ' num2str(mid)] ) disp( ['done with ' num2str(iterations)] )