% script e10maple.m % % if you know about limits, you should be able to apply the limit test % (posted to newsgroup; you might want to use L'Hopital's Rule on E10.8). % % however, if you don't know about limits or have trouble computing a % limit, then often matlab can help. this script is to show you how % to use the limit test and how to use some features of the matlab's % symbolic math toolbox. % % note: the symbolic math toolbox might not be part of student versions % of matlab; if so, go to a public cit lab. % % matlab includes something called $maple$ (see $help symbolic$) that can do % *symbolic manipulation* (computation with mathematical expressions), % as distinct from *numerical computation*" (computation with numbers). % % this is bonus material for CS100M this semester, % but you might find it very helpful for CS100M and other courses! % % ============================================================================= % % f(n) % recall how to interpret the result of the limit test lim ---- % n->oo g(n) % for comparing O(f(n)) to O(g(n)): % % limit test diagnosis % ------- ----------------- % 0 O(f(n)) < O(g(n)) % ]0,inf[ O(f(n)) = O(g(n)) % inf O(f(n)) > O(g(n)) % does not % exist test is "inconclusive" (depends on how/why limit does not exist) % % let's apply the test to E10.4 - E10.8 % % note: matlab tries to simplify fractions, % so f(n)/g(n) won't always look as you might expect. % help e10maple % print out comments above disp('press any key to run the tests...'); pause clear syms n % declare $n$ to be a "symbolic variable" sep(1:70) = '*'; sep = sprintf('%s\n\n', sep); label = sprintf('\n\nlim n --> oo of'); disp([sep 'E10.4. O(100 n^2 + 5n + 1) versus O(n^2 + 5n + 100)']) ratio = [100 * n^2 + 5*n + 1] / [n^2 + 5*n + 100]; disp(label), pretty(ratio), disp('is'), limit(ratio, n, inf) disp([sep 'E10.5. O(10 n^2) versus O(2 n^10)']) ratio = [10 * n^2] / [2 * n^10]; disp(label), pretty(ratio), disp('is'), limit(ratio, n, inf) disp([sep 'E10.6. O(10) versus O(1)']) ratio = [10] / [1]; disp(label), '10/1', disp('is'), limit(ratio, n, inf) disp([sep 'E10.7. O(1 + 1/n) versus O(2)']) ratio = [1 + 1/n] / [2]; disp(label), '(1+1/n) / 2', disp('is'), limit(ratio, n, inf) disp([sep 'E10.8. O(n+log(n)) versus O(n)']) ratio = [n + log(n)] / [n]; disp(label), pretty(ratio), disp('is'), limit(ratio, n, inf) disp([sep 'example from newsgroup: 2 - (-1)^n versus 2 + (-1)^n']) ratio = [2 - (-1)^n] / [2 + (-1)^n]; disp(label), pretty(ratio), disp('is'), limit(ratio, n, inf) disp('note: the 1/3 .. 3 means maple/matlab found 2 possible answers for') disp('the limit --namely 1/3 and 3-- which means the limit does not exist')