MATLAB Answers

## Problem using a counter in a for loop

Asked by Esther Pavao

### Esther Pavao (view profile)

on 17 Apr 2019
Latest activity Commented on by Esther Pavao

### Esther Pavao (view profile)

on 17 Apr 2019
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."

#### 0 Comments

Sign in to comment.

## 1 Answer

Answer by madhan ravi

### madhan ravi (view profile)

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')

Esther Pavao

### Esther Pavao (view profile)

on 17 Apr 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 (view profile)

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.
Esther Pavao

### Esther Pavao (view profile)

on 17 Apr 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.

Sign in to comment.