QUESTION 2 Smith book pp110-111: 1. b=14 2. b 3. a. false b. true c. false 4. X=false, Y=true 5. ans = false; if (a > b) if (b > 100) ans = true; end end if ~(a < b) ans = true; end if ~(b < 100) ans = true; end 7. if (b > 30) & (a > b) | (b < 30) & (b > a) ans = 1; elseif ~(b > 30) & ~(b < 30) ans = 0; end % Note that in the question code, there are 3 possibilities: % ans=1, ans=0, or ans is never created % Therefore in our solution above we use if-elseif intead % of if-else. 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 %Problem 2.2.5 from FVL %initialization of variables a_sum_current = 0; a_sum_previous = 1; n = 2; while (abs(a_sum_previous - a_sum_current) > 0.01) a_sum_previous = a_sum_current; a_sum_current = 0; for i = 1:(n^2) a_sum_current = a_sum_current + (n/(n^2+i^2)); end disp(a_sum_current); n = n + 1; end QUESTION 4b %P3.1.4 %This script finds the smallest integers p and q such that p/q approximates %pi to d decimal places %get the value of d 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; found= 1; 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) %Problem 3.1.4 from FVL *** ALTERNATE SOLUTION *** %This script finds the smallest integers p and q such that p/q approximates %pi to d decimal places %get the value of d 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 % Problem 3.2.4 from FVL % k factorial k = input('Enter k:'); n=1; fact = 1; % fact = n! while(n= 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 % Write a fragment to plot the function % f(x) = 0.2*(x^3-x)/(1.1+cos(x)) for -2pi <= x <= 2pi. res = 1000; x = linspace(-2*pi, 2*pi, res); f = 0.2 * (x.^3 - x)./(1.1 + cos(x)); plot(x, f);