Info

์ด ์งˆ๋ฌธ์€ ๋งˆ๊ฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŽธ์ง‘ํ•˜๊ฑฐ๋‚˜ ๋‹ต๋ณ€์„ ์˜ฌ๋ฆฌ๋ ค๋ฉด ์งˆ๋ฌธ์„ ๋‹ค์‹œ ์—ฌ์‹ญ์‹œ์˜ค.

if Loop to print values into a variable

์กฐํšŒ ์ˆ˜: 1 (์ตœ๊ทผ 30์ผ)
chong kai sheng
chong kai sheng 2020๋…„ 6์›” 4์ผ
๋งˆ๊ฐ: MATLAB Answer Bot 2021๋…„ 8์›” 20์ผ
How do i run though the loop and print the valid numbers in the 'value' variable?
The condition is to determine the ๐‘Ÿ-value cases that achieve ๐ต โ‰ฅ 10 at ๐‘ก = 5.
colourmap = [228 26 28; 55 126 184; 77 175 74; ...
152 78 163; 255 127 0]/255;
%variables
r = [0.05 0.1 0.5 1 10];
t = (0:0.01:100);
k = 15
B0 = 1;
%
for i = 1: length(r)
dBdt =@(t,B) (r(i)).*B.*(1-(B./k));
[t,B] = ode45(dBdt,t,B0);
figure(6);
loglog(t,B,'color',colourmap(i,:))
grid on;
hold on;
if B>= 10
value = []
end
end
xlabel('Time');
ylabel('Bacteria growth');
title('B against t');
legend(num2str(r','r = %5.2f'),'location','northwest');

๋‹ต๋ณ€ (1๊ฐœ)

Star Strider
Star Strider 2020๋…„ 6์›” 4์ผ
Change the loop slightly, using the two new commented lines:
for i = 1: length(r)
dBdt =@(t,B) (r(i)).*B.*(1-(B./k));
[t,B] = ode45(dBdt,t,B0);
figure(6);
loglog(t,B,'color',colourmap(i,:))
grid on;
hold on;
idx = B>= 10; % Logic Index Vector
value{i,:} = [t(idx), B(idx)]; % Save To Cell Array
end
That ran without error whan I tested it just now.
  ๋Œ“๊ธ€ ์ˆ˜: 2
chong kai sheng
chong kai sheng 2020๋…„ 6์›” 4์ผ
Tks for the help.
However the ans i needed to achieve was r = 1 , 10
I initally though i had to loop though to find the values that meet the condition but i ran into a wall and got stuck.
Star Strider
Star Strider 2020๋…„ 6์›” 4์ผ
My pleasure.
I am not certain that I understand what you want to do.
Try this:
for i = 1: length(r)
dBdt =@(t,B) (r(i)).*B.*(1-(B./k));
[t,B] = ode45(dBdt,t,B0);
figure(6);
loglog(t,B,'color',colourmap(i,:))
grid on;
hold on;
Bmtx(:,i) = B; % Save To Matrix
end
t5idx = find(t >= 5, 1, 'first'); % Time Index
colidx = find(Bmtx(t5idx,:) >= 10, 1, 'first'); % Column Index
value = Bmtx(t5idx,colidx) % โ€˜valueโ€™
It retuns the column of โ€˜Bmtxโ€™ that has the first instance that satisfies the conditions, and the associated โ€˜valueโ€™.

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by