Whats wrong with my code?

1 次查看(过去 30 天)
Lily Shepherdly
Lily Shepherdly 2020-5-2
When I run my script, that contains the following code:
% Clear workspace and command window
clear
clc
dt=1; % time step (minutes)
t=0:dt:120; % time vector
% Pregenerate vectors
z=zeros(1,length(t));
Glucose=z;
Glucose_Used=z;
Insulin=z;
Insulin_Breakdown=z;
% Modify Usuage_Fraction(t) by adding a values
a3 = -1.722E-14;
a2 = 4.666E-10;
a1 = 1.435E-8;
a0 = 8.262E-3
% Modify Insulin_Secretion(t) by adding b values
b3 = -2.626E-9;
b2 = 4.682E-5;
b1 = -0.1140;
b0 = 72.5;
% Constants and initial conditions
Glucose(1)=5940; % mg initial glucose
Insulin(1==9000); % mg initial insulin
Usage_Fraction(t) = a3* Insulin(t)^3 + a2* Insulin(t)^2 + a1* Insulin(t) + a0;
Blood_Volume=60; % dL
Glucose_Released=185.625*ones(1,length(t)); % mg/min
Insulin_Secretion(t) == b3* Glucose(t)^3 + b2* Glucose(t)^2 + b1* Glucose(t) == b0; % mg/min
k = 26.5;
% calculate insulin and glucose amounts from given equations
for i=2:length(t);
Glucose_Used(i-1)=Glucose(i-1)*Usage_Fraction(t);
Glucose(i)=Glucose(i-1)+(Glucose_Released(i-1)-Glucose_Used(i-1))*dt;
Insulin_Breakdown(i-1)=Insulin(i-1)/k*k;
Insulin(i)=Insulin(i-1)+(Insulin_Secretion(i-1)-Insulin_Breakdown(i-1))*dt;
end
Glucose_Conc=Glucose/Blood_Volume; % glucose concentration mg/dL
% plot glucose release and blood glucose profiles
subplot(1,2,1)
plot(t,Glucose_Released)
title('Glucose Release Profile')
xlabel('Time (min)')
ylabel('Glucose released into blood (mg/min)')
subplot(1,2,2)
plot(t,Glucose_Conc)
title('Blood Glucose Profile')
xlabel('Time (min)')
ylabel('Blood glucose level (mg/dL)')
I get the error
Array indices must be positive integers or logical values.
Error in homeostasis_mod_1901465 (line 32)
Usage_Fraction(t) = a3* Insulin(t)^3 + a2* Insulin(t)^2 + a1* Insulin(t) + a0;
and im not sure how to correct it
  2 个评论
John D'Errico
John D'Errico 2020-5-2
I'm not sure what you think this does:
Insulin(1==9000); % mg initial insulin
Well, actually, I might giuess what you think it does. Regardless, whatever you may want it to do, is not what it does.
What else? We see this:
dt=1; % time step (minutes)
t=0:dt:120; % time vector
The variable Insulin is a vector. It is NOT a function. Then you do this:
Usage_Fraction(t) = a3* Insulin(t)^3 + a2* Insulin(t)^2 + a1* Insulin(t) + a0;
We learend above, we learned the vector t starts out at zero.
However, MATLAB does not allow you to index a vector at element 0. What was the error message?
Array indices must be positive integers or logical values.
What should you have done?
I think you are trying to treat the vector Insulin as both a vector and a function. You cannot do that. Sorry.
Walter Roberson
Walter Roberson 2020-5-2
Insulin_Secretion(t) == b3* Glucose(t)^3 + b2* Glucose(t)^2 + b1* Glucose(t) == b0; % mg/min
That does not do whatever you might think it does. It contains no assignment operations, It contains two comparison operations, and after it is done with the calculation it throws the results of the calculations away.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by