Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab/Octave Scatter Plot

  1. Jun 21, 2011 #1
    How do I plot (x, collatz(x)), where x in an integer between 1 and 1000 inclusive, on a scatter plot?

    Code (Text):
    function c = collatz(n)
        c = 0;
        while(n != 1)
            if(mod(n,2) == 0)
                n /= 2;
            else
                n = 3 * n + 1;
            end
            c++;
        end
    end
     
  2. jcsd
  3. Jun 22, 2011 #2
    The code you posted above will not work with MatLab. MatLab's "not equal" operator is ~=. There is no division assignment or increment operator for MatLab.

    http://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.html [Broken]

    Code (Text):
    function [c] = collatz(n)
        c = 0;
        while n ~= 1
            if mod(n,2) == 0
                n = n/2;
            else
                n = 3 * n + 1;
            end
            c = c + 1 ;
        end
    end
    Code (Text):

    x = 1:1000;
    y = arrayfun(@collatz, x);
    scatter(x, y);
     
     
    Last edited by a moderator: May 5, 2017
  4. Jun 22, 2011 #3
    Oh, I wasn't aware that Octave was so different from Matlab. I'm using the former, of course.

    Thanks for your help.
     
  5. Jun 22, 2011 #4
    How do I get Octave to show the plot? When I run octave collatz.m, it has no output.

    collatz.m
    Code (Text):
    function [c] = collatz(n)
           c = 0;
           while n ~= 1
                   if mod(n,2) == 0
                           n = n/2;
                   else
                           n = 3 * n + 1;
                   end
                   c = c + 1 ;
           end
    end

    x = 1:10;
    y = arrayfun(@collatz, x);
    scatter(x, y) % Notice the purposfully omitted semicolon!
     
  6. Jun 22, 2011 #5
    The function and the last three lines should be in seperate files. collatz.m is a function file, and it is not the place for seperate script code that has a call to the function.

    http://www.mathworks.com/help/techdoc/matlab_prog/f7-38085.html [Broken]

    Also, it does not matter if you have a semicolon after "scatter(x,y)"
     
    Last edited by a moderator: May 5, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab/Octave Scatter Plot
  1. Octave/ Matlab (Replies: 2)

  2. Matlab 3D Scatter Plot (Replies: 4)

Loading...