MATLAB Answers

Hi! I need help with a graph in mathlab.

27 views (last 30 days)
I did a program that is about the Collatz Conjecture, and I did two graphs, but the problem is that when I put both in the same figure, the two graphs appears in the same side, one over the other, and appears like a spring, very small. Thanks for your help. I put the program below. And the Graphs like a attach file. Thanks a lot!
%
%Programa que calcula la secuencia de Collatz con un número dado
%
clc, clear, close all
disp('=========================================================================================')
disp('==================================COLLATZ SEQUENCE======================================')
disp('=========================================================================================')
disp(' ')
disp(' Definicion: un número natural es aquél que usamos para contar')
disp('NO entre el cero, numeros complejos, numeros irracionales (fraccionarios/decimales) ó negativos')
disp(' ')
n=input('entre el número natural: ');
seq=zeros(1, n);
seq(1) = n;
i = 2;
while seq(i-1) ~= 1
if mod(seq(i-1), 2) == 0
seq(i) = seq(i-1)/2;
else
seq(i) = 3*seq(i-1) + 1;
end
i = i+1;
end
p = length(seq);
%Gráfica #1
figure;
subplot(n, 1, 1);
plot(seq,'-m*','LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',5, 'Marker', 'diamond')
xlabel(['Términos: ',int2str(i-1)])
title(['Secuencia de Collatz ( n = ' num2str(n),' )'], 'FontSize',12)
ylim([0 100]);
xlim([ 0 30]);
grid on
subplot(n,1, 2)
semilogy(seq, '-m*', 'LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',5, 'Marker', 'diamond')
xlabel(['Términos: ',int2str(i-1)])
title(['Secuencia de Collatz ( n = ' num2str(n),' )'], 'FontSize',12)
ylim([0 100]);
xlim([ 0 30]);
grid on

Accepted Answer

Roche de Guzman
Roche de Guzman on 14 Oct 2016
%Programa que calcula la secuencia de Collatz con un número dado %
clc; clear; close all;
disp('=========================================================================================');
disp('==================================COLLATZ SEQUENCE======================================');
disp('=========================================================================================');
disp(' ');
disp(' Definicion: un número natural es aquél que usamos para contar');
disp(' NO entre el cero, numeros complejos, numeros irracionales (fraccionarios/decimales) ó negativos');
disp(' ');
n=input('entre el número natural: ');
seq=zeros(1, n);
seq(1) = n;
i = 2;
while seq(i-1) ~= 1
if mod(seq(i-1), 2) == 0
seq(i) = seq(i-1)/2;
else seq(i) = 3*seq(i-1) + 1;
end
i = i+1;
end
p = length(seq);
%Gráfica #1 figure;
subplot(2, 1, 1);
plot(seq,'-m*','LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',5, 'Marker', 'diamond');
xlabel(['Términos: ',int2str(i-1)]);
title(['Secuencia de Collatz ( n = ' num2str(n),' )'], 'FontSize',12);
grid on;
subplot(2,1, 2);
semilogy(seq, '-m*', 'LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',5, 'Marker', 'diamond');
xlabel(['Términos: ',int2str(i-1)]);
title(['Secuencia de Collatz ( n = ' num2str(n),' )'], 'FontSize',12);
grid on;

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by