How can I graph all the values of n in the while loop ? Theres my program.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
n=input('Enter a natural number');
cont=1:1:n;
while n>1
if rem(n,2)==0
n=n/2;
elseif rem(n,2)~=0
n=n*3+1;
end
plot(cont,n,'--rs');
end
0 commentaires
Réponses (1)
Mischa Kim
le 28 Oct 2016
Modifié(e) : Mischa Kim
le 28 Oct 2016
Something like this? Not quite sure what you want your x-axis to be.
n = input('Enter a natural number');
cont=1:1:n;
ii = 0;
while n>1
ii = ii + 1;
if rem(n(ii),2)==0
n(ii+1) = n(ii)/2;
elseif rem(n(ii),2)~=0
n(ii+1) = n(ii)*3+1;
end
end
plot(ii,n,'--rs');
4 commentaires
Mischa Kim
le 28 Oct 2016
jonathan, have you checked out/run the code that I posted above?
My second post contains another way of plotting the data you might want to try.
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!