This is my code. I have to use loop, even when i dont needed to get the results. But, i have problems with the graph. I tried in different ways and the graph goes blank.I put the plot command inside the loop and out of the loop. Use other variables, use the "figure" command and nothing. If I do not use "loop" the graph goes out. But as I mentioned, I have to use the command as part of the course.
%Dg-dosage administrated(mg).
%Va-volume of distribution(L).
%Ke-absorption rate constant (h^-1).
%Ka-elimination rate constant (h^-1).
%t-time(h)since the drug was administered.
Dg=150; %mg
Vd=50; %L
Ke=0.4; %h^-1
Ka=1.6; %h^-1
for t=0:0.1:10;
disp('t= ')
disp (t)
Cp=(Dg/Vd)*(Ka/(Ka-Ke))*(exp(-Ke*t)-exp(-Ka*t));
fprintf('Cp=%g\n',Cp)
end
figure (1)
plot (t,Cp)
title ('Drug Concentration versus Time','FontName', 'Times New Roman', 'FontSize', 16)
xlabel ('time (t) ','FontName', 'Times New Roman', 'FontSize', 14)
ylabel ('Concentration (Cp) ','FontName', 'Times New Roman', 'FontSize', 14)
grid on

 Respuesta aceptada

Star Strider
Star Strider el 16 de En. de 2020

0 votos

Subscript ‘Cp’ and the plot magically appears!
Dg=150; %mg
Vd=50; %L
Ke=0.4; %h^-1
Ka=1.6; %h^-1
t=0:0.1:10;
for k = 1:numel(t)
disp('t= ')
disp (t(k))
Cp(k)=(Dg/Vd)*(Ka/(Ka-Ke))*(exp(-Ke*t(k))-exp(-Ka*t(k)));
fprintf('Cp=%g\n',Cp(k))
end
figure (1)
plot (t,Cp)
title ('Drug Concentration versus Time','FontName', 'Times New Roman', 'FontSize', 16)
xlabel ('time (t) ','FontName', 'Times New Roman', 'FontSize', 14)
ylabel ('Concentration (Cp) ','FontName', 'Times New Roman', 'FontSize', 14)
grid on

6 comentarios

Gyp
Gyp el 16 de En. de 2020
Thanks, I didn't know about the "numel" command. I haven't been taught that code in class. But I got the graph ... thanks for the help.
Star Strider
Star Strider el 16 de En. de 2020
As always, my pleasure!
The numel funciton is efficient for vectors. Perhaps more appropriate for most purposes is the size function. The functions length, numel, and size all do different but related things, so choosing the correct one and the correct arguments are important.
Gyp
Gyp el 16 de En. de 2020
I'm having a hard time working with the "while-loops" command. I am trying now to instead of using the loop, use "while". But I have the same problem, the graph does not work out. I try to use numel with this, but a get error. At leat with these code I get the Cp.... but not the graph.
Dg=150; %mg
Vd=50; %L
Ke=0.4; %h^-1
Ka=1.6; %h^-1
t=1;
Cp=0;
while t<=10
Cp=(Dg/Vd)*(Ka/(Ka-Ke))*(exp(-Ke*t)-exp(-Ka*t));
t= t + 1;
fprintf('Cp=%g\n',Cp)
end
figure (1)
plot (t,Cp)
title ('Drug Concentration versus Time','FontName', 'Times New Roman', 'FontSize', 16)
xlabel ('time (t) ','FontName', 'Times New Roman', 'FontSize', 14)
ylabel ('Concentration (Cp) ','FontName', 'Times New Roman', 'FontSize', 14)
grid on
Star Strider
Star Strider el 16 de En. de 2020
Again, subscripting it (this time using ‘t’ as the loop index, since it has only integer values here) is necessary:
Dg=150; %mg
Vd=50; %L
Ke=0.4; %h^-1
Ka=1.6; %h^-1
t=1;
tv(t) = t;
Cp(t)=0;
while t<=10
t= t + 1;
tv(t) = t;
Cp(t)=(Dg/Vd)*(Ka/(Ka-Ke))*(exp(-Ke*t)-exp(-Ka*t));
end
fprintf('Cp=%g\n',Cp)
figure (1)
plot (tv,Cp)
title ('Drug Concentration versus Time','FontName', 'Times New Roman', 'FontSize', 16)
xlabel ('time (t) ','FontName', 'Times New Roman', 'FontSize', 14)
ylabel ('Concentration (Cp) ','FontName', 'Times New Roman', 'FontSize', 14)
grid on
I created a new variable ‘tv’ to hold the elements of ‘t’, and put the fprintf call after the loop, so it will print all the values of ‘Cp’.
Gyp
Gyp el 16 de En. de 2020
Thank you very much for the teaching.
Star Strider
Star Strider el 16 de En. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Gyp
el 16 de En. de 2020

Editada:

el 17 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by