Matlab/Octave Scatter Plot: Visualizing Collatz Conjecture for x=1 to 1000

  • Context: MATLAB 
  • Thread starter Thread starter TylerH
  • Start date Start date
  • Tags Tags
    Plot
Click For Summary

Discussion Overview

The discussion revolves around plotting the Collatz conjecture using scatter plots in Matlab and Octave, focusing on how to implement the function and visualize the results for integer values of x from 1 to 1000.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant inquires about how to plot the Collatz function for integers between 1 and 1000.
  • Another participant points out that the original code contains errors for Matlab, specifically regarding the "not equal" operator and the absence of division assignment or increment operators.
  • A participant acknowledges the differences between Matlab and Octave, indicating they are using Octave.
  • There is a question about how to display the plot in Octave, noting that running the script produces no output.
  • One participant suggests that the function and the script code should be in separate files, clarifying the structure of function files and script files in Matlab.
  • Another participant mentions that the presence of a semicolon after the scatter function call does not affect the output.

Areas of Agreement / Disagreement

Participants generally agree on the need for code corrections for Matlab and the distinction between Matlab and Octave. However, there are unresolved questions regarding the proper file structure and output display in Octave.

Contextual Notes

Limitations include potential misunderstandings about the differences in syntax and functionality between Matlab and Octave, as well as the need for clarity on how to properly structure function and script files.

Who May Find This Useful

This discussion may be useful for individuals interested in programming in Matlab or Octave, particularly those exploring the Collatz conjecture or seeking assistance with plotting functions.

TylerH
Messages
729
Reaction score
0
How do I plot (x, collatz(x)), where x in an integer between 1 and 1000 inclusive, on a scatter plot?

Code:
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
 
Physics news on Phys.org
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

Code:
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:
x = 1:1000;
y = arrayfun(@collatz, x);
scatter(x, y);
 
Last edited by a moderator:
Oh, I wasn't aware that Octave was so different from Matlab. I'm using the former, of course.

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

collatz.m
Code:
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!
 
The function and the last three lines should be in separate files. collatz.m is a function file, and it is not the place for separate script code that has a call to the function.

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

Also, it does not matter if you have a semicolon after "scatter(x,y)"
 
Last edited by a moderator:

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K