# Matlab/Octave Scatter Plot

1. Jun 21, 2011

### TylerH

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. Jun 22, 2011

### MisterX

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
3. Jun 22, 2011

### TylerH

Oh, I wasn't aware that Octave was so different from Matlab. I'm using the former, of course.

4. Jun 22, 2011

### TylerH

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!

5. Jun 22, 2011

### MisterX

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