Problem using a counter in a for loop

The concentration of a drug in the body CP can be modeled by the equation:
Screen Shot 2019-04-16 at 8.19.19 PM.png
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."

Respuestas (1)

madhan ravi
madhan ravi el 17 de Abr. de 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 comentarios

madhan ravi
madhan ravi el 17 de Abr. de 2019
I suggest you to use logical indexing. I assume that your supposed to use loop because it’s an assignment.
EP
EP el 17 de Abr. de 2019
This worked somewhat. Now the error message says "Variable Cp must be of size [1 241]. It is currently of size [1 231]. Check where the variable is assigned a value." I'm not sure what to alter.
EP
EP el 17 de Abr. de 2019
Yes, it's an assignment. The for loop is required.
madhan ravi
madhan ravi el 17 de Abr. de 2019
I didn’t get any error message running the code. Use clear all at the beginning and try again.
EP
EP el 17 de Abr. de 2019
Same result.
Screen Shot 2019-04-16 at 8.01.46 PM.png
madhan ravi
madhan ravi el 17 de Abr. de 2019
Editada: madhan ravi el 17 de Abr. de 2019
Try changing the value of step size in t such that t has 241 values instead of 231.
EP
EP el 17 de Abr. de 2019
I'm sorry, I keep getting error messages. Now it just says Cp has an incorrect value. But I did upload the rest of the information given to me in the original problem. Maybe there's something in there that can help?
I really appreciate all of your assistance!!
madhan ravi
madhan ravi el 17 de Abr. de 2019
Editada: madhan ravi el 17 de Abr. de 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
EP el 17 de Abr. de 2019
I reuploaded the question with all of the original problem. I tried using the linspace for t, and still getting an error message that variable Cp has incorrect values.
Walter Roberson
Walter Roberson el 17 de Abr. de 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
EP el 17 de Abr. de 2019
Walter, I understand the changes in time you're suggesting. But what do you mean about adding the dosage? Do you mean a counter?
Walter Roberson
Walter Roberson el 17 de Abr. de 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
EP el 17 de Abr. de 2019
Are you suggesting a line inside the for loop Cp=Cp+Dg? That would add another dose after each loop. But it doesn't give the correct values.

Iniciar sesión para comentar.

Categorías

Más información sobre Historical Contests en Centro de ayuda y File Exchange.

Preguntada:

EP
el 17 de Abr. de 2019

Comentada:

EP
el 17 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by