Solutions to Prelim 1 Review Questions QUESTION 2 ------------------------------------------------------ "M Questions" from Insight. You can check these on your own using Matlab. Ask a course staff member if you have questions. QUESTION 3 ------------------------------------------------------ amt = input('Enter the Income: '); if (amt > 60000) tax = (25/100)*30000+(30/100)*30000+(40/100)*(amt-60000); elseif (amt > 30000) tax = (25/100)*30000+(30/100)*(amt-30000); else tax = (25/100)*(amt); end fprintf('The tax to be paid for an income of %d is %d',amt,tax); QUESTION 4a ----------------------------------------------------- %P2.2.9 %Print a sequence a(2), a(3), ..., a(n) where n is smallest integer %such that abs(a(n-1)-a(n))<=.01. % Initialization tol= 0.01; % Stopping tolerance n= 1; a_current= 0.5; % a(n), i.e., a(1) a_previous= 0; % a(n-1) while abs(a_previous - a_current) > tol % Update n= n+1; a_previous= a_current; % Calculate current term a(n), which is a summation a_current = 0; for j = 1:n^2 a_current = a_current + (n/(n^2+j^2)); end fprintf('%4d %10.6f\n', n, a_current) end QUESTION 4b ----------------------------------------------------- %P3.1.5 %Find the smallest integers p and q such that p/q approximates pi %to d decimal places d = input('Please enter d between 1 and 15: '); %p cannot be less than 4. If p is 1, 2, or 3, then there is no integral %value of q which gives an approximation of pi within 0.1, and since 1 is %the smallest possible value of d, the approximation must be within at %least 0.1. p = 4; q = floor(p/pi); % floor(p/pi) gives a better approximation than % ceil(p/pi) in the case p = 4 foundpq= 0; % have we found the appropriate p and q yet? (no) while (~foundpq) % only need to check two q values q_lo= floor(p/pi); q_hi= ceil(p/pi); err_lo= p/q_lo - pi; err_hi= pi - p/q_hi; % is tolerance met? if so, want the lower of the two q values if (err_lo <= 10^(-1*d)) foundpq= 1; q= q_lo; elseif (err_hi <= 10^(-1*d)) foundpq= 1; q= q_hi; end p = p+1; end fprintf('Smallest values of p and q such that \n') fprintf('p/q and pi differs by less than 10^(-d): \n') fprintf(' p: %d q: %d p/q: %.16f \n', p-1, q, (p-1)/q) %P3.1.5 *** ALTERNATE SOLUTION *** %Find the smallest integers p and q such that p/q approximates pi %to d decimal places d = input('Please enter d between 1 and 15:\n') %p cannot be less than 4. If p is 1, 2, or 3, then there is no integral %value of q which gives an approximation of pi within 0.1, and since 1 is %the smallest possible value of d, the approximation must be within at %least 0.1. p = 4; q = floor(p/pi); % floor(p/pi) gives a better approximation than % ceil(p/pi) in the case p = 4 %keep track of the best approximation we have seen so far best_p = p; best_q = q; best = p/q; while (abs(pi - best) > 10^(-1*d)) for q = floor(p/pi):ceil(p/pi) %we only need to check these 2 values if (abs(pi-(p/q)) < abs(pi - best)) %better approx found best = p/q; best_p = p; best_q = q; end end p = p+1; %increment p end % Strictly speaking, this solution does not pick the SMALLER q. % Consider the case where both q_lo and q_hi, i.e., floor(p/pi) % and ceil(p/pi), result in errors <=10^(-d). This solution % would pick the q that gives a smaller error, not the smaller q. fprintf('Smallest values of p and q such that \n') fprintf('p/q and pi differs by less than 10^(-d): \n') fprintf(' p: %d q: %d p/q: %.16f \n', best_p, best_q, best) QUESTION 4c ----------------------------------------------------- % P3.2.4 % k factorial (while-loop used here; you can use for-loop) k = input('Enter k:'); n=1; fact = 1; % fact = n! while n1 || (n==1 && k>0) % Update... current = sqrt(1+k*current); k = k-1; end fprintf(' %2d %10.8f \n',n,current) % Update... n = n+1; end QUESTION 5 ------------------------------------------------------ % Assume variables nBig and nSm are given. % Outer loop to iterate from nBig to nSm (while-loop used here; % you can use for loop). while nBig >= nSm % Check nBig to see if it is prime d=2; % divisor % Iterate until first proper divisor is found while (mod(nBig,d) ~= 0); d = d + 1; end if (d == nBig) fprintf('%d is a prime\n',nBig); else fprintf('%d\n',nBig); end nBig = nBig – 1; end QUESTION 6 ----------------------------------------------------- % Example program for computing the mode of a sequence of integers. disp('Determine mode of a set of nonnegative integers.') disp('Use a negative number to quit.') previous = -1; % Previous number seen count = 1; % Count of current number mode = -1; % Mode seen so far modeCount = 0; % Count for mode so far number = input('Give me a number: '); while number >= 0 % Quit when negative number is encountered if number == previous % same run, so increment count count = count + 1; else % new run, so reset count count = 1; end if count > modeCount mode = number; modeCount = count; end previous = number; number = input('Another number not smaller than the previous: '); end if mode == -1 disp('Mode is undefined') else fprintf('Mode is %d which occurred %d times.\n', mode, modeCount) end