Less Than and Less Than or Equal to Not Working Properly
9 次查看(过去 30 天)
显示 更早的评论
I was wondeirng if anyone could look at my function and see what is going on with the <= and < portions of my if/else statement. The statement is defining Is to be 200 from [0, .15), yet it is being definied as 200 from (0, .15]. Effectively, it's reading the <= as <, and the < as <=. Additionally, if anyone could explain why the table is starting at -.15 even though -.2 is the specified starting point, it would be very helpful. Any help would be appreciated, thanks.
clear all
% Set parameters (constants)
tStart = -.200 ; % start time, millisec
tEnd = .200 ; % end time, millisec
deltaT = 0.050 ; % time step, millisec
nStep = ceil((tEnd-tStart)/deltaT) ; % number of time steps
outputInterval = 20 ; % number of time steps between screen output
Vrest = -60 ; % resting potential, mV
EK = -72.1 ; % potassium Nernst potential, mV
ENa = 52.4 ; % sodium Nernst potential, mV
EL = -49.2 ; % leak Nernst potential, mV
gK_max = 36 ; % potassium saturation conductance, mS/cm^2
gNa_max = 120 ; % sodium saturation conductance, mS/cm^2
gL_max = 0.3 ; % leak saturation conductance, mS/cm^2
Cm = 1 ; % Membrane Capacitance
Jstim = 200 ;
StimDur = .15 ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set initial value of state variables
Vm = Vrest ; % membrane potential, mV
n = 0.31768 ; % initial value of n gate
m = 0.05293 ; % initial value of m gate
h = 0.59612 ; % initial value of h gate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preallocate storage for variables to plot
plot_Vm = zeros(nStep,1) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print a heading for the screen output
disp('Hodgkin-Huxley squid giant axon model')
disp(' i time Jion Vm dVdt n m h')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start the simulation
tNow = tStart ;
for iStep = 1:nStep
% Compute ion currents at tNow, then apply stimulus current
if (0<=tNow && tNow<StimDur) % start stimulus current at tNow=0
Is = Jstim ;
else % stop stimulus when tNow = StimDur
Is = 0 ;
end
JNa = gNa_max.*m.^3.*h.*(Vm-ENa) ;
JK = gK_max.*n.^4.*(Vm-EK) ;
JL = gL_max.*(Vm-EL);
Jion = JNa+JK+JL
Vdot = (Is-Jion)./Cm
% Compute gates' opening and closing rates
[a_n, b_n] = get_n_rates(Vm)
[a_m, b_m] = get_m_rates(Vm)
[a_h, b_h] = get_h_rates(Vm)
% Compute change in state variables
deltaVm = (Is - JNa - JK - JL)./Cm .* deltaT ;
delta_m = (a_m.*(1-m) - b_m.*(m)) .* deltaT ;
delta_n = (a_n.*(1-n) - b_n.*(n)) .* deltaT ;
delta_h = (a_h.*(1-h) - b_h.*(h)) .* deltaT ;
if mod(iStep,outputInterval) == 0
fprintf('%5d %8.2f %8.3f %7.3f %7.2f %7.5f %7.5f %7.5f\n', ...
iStep, tNow, Jion, Vm, dV_dt, n, m, h) ;
end % if mod(tNow)
%Update State Veriable
Vm = Vm + deltaVm ;
m = m + delta_m
n = n + delta_n
h = h + delta_h
tNow = tStart + iStep.*deltaT
% Update state variables
iStepAll(iStep) = iStep;
tNowAll(iStep) = tNow;
IsAll(iStep) = Is;
VmAll(iStep) = Vm
VdotAll(iStep) =Vdot;
nAll(iStep) = n;
mAll(iStep) = m;
hAll(iStep) = h;
end % for iStep
% end of file
T = table (iStepAll', tNowAll', IsAll', VmAll', VdotAll', nAll', mAll', hAll', 'VariableNames', ["LoopCnt", "Time", "Is", "Vm", "Vdot", "n", "m", "h"] )
3 个评论
Michael Van de Graaff
2021-12-16
Can you slim this down to somehting that isolates the problem? I don't have get_n_rates() so i don't know what its doing because it throws an error before finishing the first loop
also, you say you want Is be 200 from [0, 0.15) But I don't know what this means in MATLAB, it looks like half open interval notation, but I don't know how to interpret that as a MATLAB array.
采纳的回答
Michael Van de Graaff
2021-12-16
So, for the part of your question "Additionally, if anyone could explain why the table is starting at -.15 even though -.2 is the specified starting point, it would be very helpful."
at the end of the first iteration, you have
tNowAll(iStep) = tNow;
But this is after you've incremented tNow
tNow = tStart + iStep.*deltaT
% Update state variables
iStepAll(iStep) = iStep;
tNowAll(iStep) = tNow; % for iStep = 1, this is tStart+deltaT
so move those assignments prior to the increment
2 个评论
Michael Van de Graaff
2021-12-16
as for the inequalities, They seem to be doing the correct thing for me.
Do you want
tNowAll = [-0.2, -0.15, -0.1,-0.05, 0, 0.05, 0.10, 0.15];
or
tNowAll = [-0.2, -0.15, -0.1,-0.05, 0, 0.05, 0.10, 0.15, 2];
because if you want the end points included, you need to make nStep =9 not 8
this is because
% length(tStart:deltaT:tEnd) = (tEnd-tStart)/deltaT+1
Star Strider
2021-12-16
It’s possible that the equalities aren’t working becasue of floating-point approximation error.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!