How to create an array from a while loop
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bryan Morrow
am 29 Mär. 2014
Kommentiert: Umesh Gautam
am 16 Jan. 2020
Alright so my professor gave me this prompt, and i normally do well in this class. However, this one totally has me stumped. I'll post the prompt and then my attempt and any advice or criticism would be much appreciated.
3. Experiment shows that bacteria reproduce once every 30 minutes. Assume that the experiment starts with three bacteria. Write a program that calculates the number of bacteria present at intervals of one half hour from the beginning of the experiment until 6 hours have elapsed. Your program must create two arrays namely; time and bact. Array time contains the time values and array bact contains the bacteria count. Plot number of bacteria versus time. Your plot must have axes labels, grid and a title. The title is “your name – Bacteria Growth”. Use while loop.
Here's what i have so far:
clc
clear
time=(0:0.5:6);
b=1.5;
n=0;
while n<13
b=b*2;
n=(n)+1;
fprintf('\n%2d',b);
end
bact=b(1:12);
fprintf('\n %5d',bact);
I am aware that i do have to graph this, i can do that with ease, i just want to make sure im producing the right numbers and the two appropriate arrays. The problem appears to arise when i try to draw upon the b values from the loop. Am i not able to do this?
Thank you.
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 29 Mär. 2014
Bearbeitet: Azzi Abdelmalek
am 29 Mär. 2014
clc
clear
time=(0:0.5:6);
b=1.5;
bact=zeros(1,12);
n=1;
while n<13
b=b*2;
bact(n)=b;
n=(n)+1;
fprintf('\n%2d',b);
end
bact
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!