Problem using a counter in a for loop
Show older comments
The concentration of a drug in the body CP can be modeled by the equation:

where DG is the dose administered (mg), Vd is the volume of distribution (L), ka is the absorption rate constant (h-1), ke is the elimination rate constant (h-1), and t is the time (h) since the drug was administered. For a certain drug, the following quantities are given: DG = 150 mg, Vd = 50 L, ka = 1.6 h-1, ke = 0.4 h-1.
Assume a first dose is administered at t = 0, and subsequently four more doses are administered at intervals of 4 hours (i.e. at t = 4, 8, 12, 16). Write a MATLAB program to calculate and plot Cp versus t for 24 hours in increments of 0.1 hours. Use a for loop to calculate Cp for each time increment and add drug dosages at the appropriate times. Consider using a loop counter (e.g. k=k+1) to address the Cp array at each time increment (i.e. Cp(k)). To plot Cp versus t, you may need to create an array for the x-axis values (or time values).
Dg = 150;%mg
Vd = 50;%L
ka = 1.6;%h^-1
ke = 0.4;%h-1
This is what I've done so far
t=0;
for t=1:0.1:24 % loop for time = 1 to 24 hours every 0.1 hour
if mod(t,6)==0
Cp = (Dg/Vd)*(ka/(ka-ke))*(exp(-ke * t)-exp(-ka * t))
t=t+1;
end
end
plot(t,Cp)
title('Concentration over Time')
xlabel('Time (hr)')
ylabel('Concentration')
I have to use a for loop and I have to use a conditional. The error message I'm getting says "Variable Cp must be of size [1 241]. It is currentnly of size [1 1]. Check where the variable is assigned a value."
Answers (1)
madhan ravi
on 17 Apr 2019
Dg = 150;%mg
Vd = 50;%L
ka = 1.6;%h^-1
ke = 0.4;%h-1
t=1:0.1:24;
C=zeros(size(t));
for k=1:numel(t) % loop for time = 1 to 24 hours every 0.1 hour
if ~mod(t(k),6)
Cp(k) = (Dg/Vd)*(ka/(ka-ke))*(exp(-ke * t(k))-exp(-ka * t(k)));
end
end
plot(t,Cp)
title('Concentration over Time')
xlabel('Time (hr)')
ylabel('Concentration')
13 Comments
madhan ravi
on 17 Apr 2019
I suggest you to use logical indexing. I assume that your supposed to use loop because it’s an assignment.
EP
on 17 Apr 2019
EP
on 17 Apr 2019
madhan ravi
on 17 Apr 2019
I didn’t get any error message running the code. Use clear all at the beginning and try again.
EP
on 17 Apr 2019
madhan ravi
on 17 Apr 2019
Edited: madhan ravi
on 17 Apr 2019
Try changing the value of step size in t such that t has 241 values instead of 231.
EP
on 17 Apr 2019
madhan ravi
on 17 Apr 2019
Edited: madhan ravi
on 17 Apr 2019
Use linspace() to specify number of interval for t such that it has 241 elements. I am not really sure how your teacher has set an algorithm for this problem.
EP
on 17 Apr 2019
Walter Roberson
on 17 Apr 2019
Time should start from 0 in the for loop, not from 1. You are missing entries for 0.1, 0.2, .. 0.9
Why are you taking mod 6 when the drugs are to be administered every 4 hours?
Where are you adding the dosage to what is currently in the body?
EP
on 17 Apr 2019
Walter Roberson
on 17 Apr 2019
"four more doses are administered at intervals of 4 hours".
When a dose is given, it does not replace the current amount of drug in the body: it adds to the current amount of drug in the body. If, after 4 hours, (say) 3/4 of the first dose of drug has been metabolized, then upon the second dose, the body is not starting with 0 again: it would be starting from (e.g.) 1/4 dose, leading to (say) 5/4 of the dose in the body at the end of the injection. 4 hours later, 3/4 of that gets used, 5/16 of one dose left in body, add a full dose. 1+5/16 now in body. And so on.
Therefore every 4 hours, you need to add D_G to what is currently in the body.
EP
on 17 Apr 2019
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!