Date: Feb 16, 2013 7:06 PM
Author: Ray
Subject: Limiting output range; computing n log2 n

Hello,

I'm not a Matlab user at all but I'm merely trying to use now it create a quick and dirty 2D plot to illustrate relative O-notation complexities to avoid drawing it by hand. I am using Matlab version R2010b and I have no additional toolkits. My question consists of two parts. I would like plots of the following cases on the same plot:

O(1)
O(lg2 n)
O(n)
O(n lg2 n)
O(n * n)
O(2 ^ n)
O(n!)

My code is at the end of this post.

Question 1: Although I have specified the range of values for n and my axis limits as I want them, obviously the value of n-squared and n-factorial will greatly exceed the desired y-axis range. When this happens it greatly compresses the y axis plot in order to accommodate that range of outputs. How can I specify that no values of y should be plotted past the desired range of the y axis itself?

Question 2: In my code below the expression n * log2(n) results in an error stating "Inner matrix dimensions must agree." I got the same error when I tried to compute n-squared by simply doing n * n, but I was able to resolve that by changing it to n.^2. What is the proper way to express n * log2(n) without additional toolkits?

Thanks,
Ray

grid on
axis([0,40,0,200]);
n = 0:1:40;

O_1 = 0;
O_log2_n = log2(n);
O_n = n;
O_n_log2_n = n * log2(n); % Doesn't work this way
O_log2_nSq = n.^2;
O_log2_nFact = factorial(n);

plot(n, O_1, n, O_log2_n, n, O_n, n, O_n_log2_n, n, O_log2_nSq, n, O_log2_nFact);

text(37, 37, ' O(n)', 'HorizontalAlignment','left','FontSize',12);
set(gca, 'XTick', [0, 10, 20, 30, 40]);
set(gca, 'YTick', [0, 50, 100, 150, 200]);
xlabel('n','FontSize',16);
ylabel('T(n)','FontSize',16);
title('\it{Comparative algorithm growth rates}','FontSize',16);