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

  • MATLAB
  • Thread starter TylerH
  • Start date
  • Tags
    Plot
In summary: If you do not put a semicolon, the output will be displayed, but if you do put a semicolon, it will not be displayed but still saved in the workspace. In summary, the conversation discusses how to plot (x, collatz(x)) on a scatter plot using Octave. The code for the collatz function and scatter plot is provided, along with a clarification on the syntax differences between Octave and Matlab. The importance of separating the function and script code into different files is also mentioned.
  • #1
TylerH
729
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
  • #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

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:
  • #3
Oh, I wasn't aware that Octave was so different from Matlab. I'm using the former, of course.

Thanks for your help.
 
  • #4
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!
 
  • #5
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:

1. What is the Collatz Conjecture?

The Collatz Conjecture, also known as the 3n+1 problem, is a mathematical conjecture that states that for any positive integer, if you repeatedly apply the following steps, you will eventually reach 1: if the number is even, divide it by 2; if the number is odd, multiply it by 3 and add 1.

2. How does the Scatter Plot visualize the Collatz Conjecture?

The Scatter Plot uses x=1 to 1000 as the input values for the Collatz Conjecture. These input values are plotted on the x-axis, while the corresponding output values (resulting from repeatedly applying the steps mentioned above) are plotted on the y-axis. This provides a visual representation of the pattern and behavior of the Collatz Conjecture for the given input range.

3. What is the significance of visualizing the Collatz Conjecture using a Scatter Plot?

The Scatter Plot allows us to see the behavior of the Collatz Conjecture in a more intuitive and visual way. It can help us identify any patterns or trends in the resulting output values, which can provide insights into the behavior of the conjecture and potentially lead to a better understanding of its properties.

4. Can the Scatter Plot be used to prove or disprove the Collatz Conjecture?

No, the Scatter Plot is simply a visualization tool and cannot be used as a proof for or against the Collatz Conjecture. It can provide insights and observations, but a formal proof or disproof of the conjecture would require a rigorous mathematical approach.

5. Can I use Matlab/Octave to create a Scatter Plot for other mathematical conjectures or problems?

Yes, Matlab/Octave can be used to create Scatter Plots for a variety of mathematical problems and conjectures. It is a versatile tool for data visualization and can be applied to a wide range of mathematical concepts and theories.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
883
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
618
Back
Top