Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Limiting output range; computing n log2 n
Replies: 6   Last Post: Feb 18, 2013 7:35 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 11
Registered: 7/6/08
Limiting output range; computing n log2 n
Posted: Feb 16, 2013 7:06 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


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(lg2 n)
O(n lg2 n)
O(n * n)
O(2 ^ 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?


grid on
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]);
title('\it{Comparative algorithm growth rates}','FontSize',16);

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.