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 4 ----------------------------------------------------- % Sec 2.1 Problem 8 % The Euler constant disp(' n E_n'); disp('-------------------------'); kMax = 100; step = 100; % Initialization total = 0; % Keep track of the summation for n=1:kMax*step % Calculate the sum total = total + 1/n; if rem(n,step) == 0 % Calculate E_n E_n = total - log(n); fprintf(' %5d %.8f\n', n, E_n) end end ----------------------------------------------------- % Sec 2.2 Problem 8 % Two sequences that approximate pi n = 0; a = 6/sqrt(3)*(-1)^n/3^n/(2*n+1); fprintf('a%d = %.7f\n',0, a) while abs(a - pi) > .000001 n = n + 1; a = a + 6/sqrt(3)*(-1)^n/3^n/(2*n+1); fprintf('a%d = %.7f\n',n, a) end n = 0; b = 16 * (-1)^n / 5^(2*n+1) / (2*n+1) - 4 * (-1)^n / 239^(2*n + 1); fprintf('\nb%d = %.7f\n',0, b) while abs(b - pi) > .000001 n = n + 1; b = b + 16 * (-1)^n / 5^(2*n+1) / (2*n+1) - 4 * (-1)^n / 239^(2*n + 1); fprintf('b%d = %.7f\n',n, b) end ----------------------------------------------------- % Section 3.1 Problem 8 % Draw ASCII figure % * % * * % * * % * * % * * % * * % * * % * * % * n= input('Input n, n>3: '); % Top half for r= 1:n % A general row has spaces, star, spaces, star, \n for c= 1:n-r fprintf(' '); end fprintf('*'); if (r > 1) for c = 1:2*(r-1)-1 fprintf(' '); end fprintf('*'); end fprintf('\n') end % Bottom half for r= n-1:-1:1 for c= 1:n-r fprintf(' '); end fprintf('*'); if (r > 1) for c = 1:2*(r-1)-1 fprintf(' '); end fprintf('*'); end fprintf('\n') end ----------------------------------------------------- %Section 3.1 Problem 10 %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 ----------------------------------------------------- % Section 3.2 Problem 7 % Print t_1 to t_26 disp(' n t_n') disp('--------------------') n = 1; while n<=26 % Initialization current = 1; k = n; % Calculate from inside to outside. % current is the value of the kth squre root while k>1 || (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