Need help creating a loop

5 Ansichten (letzte 30 Tage)
Giovanni Principato am 17 Mär. 2021
Kommentiert: Angshuman Podder am 17 Mär. 2021
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
2 KommentareKeine anzeigenKeine ausblenden
Angshuman Podder am 17 Mär. 2021
This script seems to run.
Giovanni Principato am 17 Mär. 2021
I submitted the wrong one try this one

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Angshuman Podder am 17 Mär. 2021
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
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Angshuman Podder am 17 Mär. 2021
Your previous code was also right.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by