필터 지우기
필터 지우기

Need help creating a loop

조회 수: 3 (최근 30일)
Giovanni Principato
Giovanni Principato 2021년 3월 17일
댓글: Angshuman Podder 2021년 3월 17일
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
Angshuman Podder
Angshuman Podder 2021년 3월 17일
This script seems to run.
Giovanni Principato
Giovanni Principato 2021년 3월 17일
I submitted the wrong one try this one

댓글을 달려면 로그인하십시오.

답변 (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
  댓글 수: 1
Angshuman Podder
Angshuman Podder 2021년 3월 17일
Your previous code was also right.

댓글을 달려면 로그인하십시오.

카테고리

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