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:
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?
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);