Need help creating a loop

3 次查看(过去 30 天)
Giovanni Principato
The concentration of a drug in the body, Cp, can be modeled by the equation:
where Dg is the dosage administered in mg, Vd is the volume of the distribution in liters, ka is the absorption rate of the drug into the body in 1/hour, ke is the body's elimination rate of the drug from the body also in 1/hr, and t is the time in hours since the drug was administered.
Create a script file that uses a for loop to calculate the concentration of two, 12 hour doses of a drug over a 24 hour period every 6 minutes (0.1 hours) given that Dg=100 mg, Vd=50 L, ka=1.8/hr, and ke=0.1/hr. Create a vector named Cp that stores all concentration values over the entire 24 hour period. You will need to vectorize your code (create a vector, one element at a time with each pass through the loop). It may help to use a counting variable to go through your loop and that could look something like this:
Cp(count) = ....
To properly account for the two seperate doses, you need to set up your equations as follows:
If t <= 12 hours use this equation:
If t > 12 hours and <= 24 hours, use this equation:
Generate a fully formatted plot for Cp over 24 hours time period.
Note: You must use the time vector as your range over which you are looping your iterator (loop) variable. Use a counting variable which you increment at the end of the loop to vectorize Cp.
I use this code but keep getting it wrong. I don't know what to do.
Dg = 100;
Vd = 50;
ka = 1.8;
ke = 0.1;
t = 0.1:.1:24;
for k = 1:length(t)
if t(k) <= 12
Cp(k) = (Dg/Vd)/(ka/(ka-ke))*(exp((-ke).*t) - exp((-ka).*t))
else
Cp(k) = (Dg/Vd)/(ka/(ka-ke))*(exp((-ke).*t) - exp((-ka).*t)) + (Dg/Vd)/(ka/(ka-ke))*(exp((-ke).*(t-12)) - exp((-ka).*(t-12)))
end
end

回答(1 个)

Angshuman Podder
Angshuman Podder 2021-3-17
Replace t with t(k).
clc
clear
Dg = 100;
Vd = 50;
ka = 1.8;
ke = 0.1;
t = 0.1:.1:24;
Cp = zeros(1,length(t));
for k = 1:length(t)
if t(k) <= 12
Cp(k) = (Dg/Vd)/(ka/(ka-ke))*(exp((-ke)*t(k)) - exp((-ka)*t(k)));
else
Cp(k) = (Dg/Vd)/(ka/(ka-ke))*(exp((-ke)*t(k)) - exp((-ka)*t(k))) + (Dg/Vd)/(ka/(ka-ke))*(exp((-ke)*(t(k)-12)) - exp((-ka)*(t(k)-12)));
end
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by