Index in position 1 is invalid. Array indices must be positive integers or logical values.

2 views (last 30 days)
%% Parameters
Rm = 50; % Membrane Resistance
Cm = 0.000001; % Membrane Capacitance
Gm = 1/Rm; % Membrane Conductance
Rc = 40; % Cytoplasmic Resistance
Vrest = -70; % Resting Membrane Potential
Iinj = -0.1; % Injected Current
nx = 300; % no. of space step
nt = 150; % no. of timesteps
t_max = 2; % max time in seconds
Length = 0.5; %max length of cable
dx = Length./nx; % change in space (delta x)
dt = t_max./nt; % change in time (delta t)
d = 0.000005; % Axon Diameter
mtc = Rm.*Cm;
cablesc = sqrt(((Rm.*d)./(4.*Rc)));
s = (dt*(cablesc^2))/(mtc*(dx^2));
k = dt/mtc;
%% Initial Guess
V = zeros(nx, nt);
%% Boundary Conditions
V(1,:) = 0;
V(:,1) = 0; % Vertical Boundary Left
V(nx,nx) = 0; % Vertical Boundary Right
V(nt,:) = 0;
%% Initial Condition
V(nx/2, :) = 0.07;
%% For Loop Calculation
for j=1:nt-1
for i = 1:nx-1
V(i,j+1) = ((1-2*s-k).*V (i,j))+ (s.*(V (i-1,j) + V (i+1,j))) - (k.*(Rm.*Iinj+Vrest));
end
end
%% Graphical Solution
figure()
contourf (V,200,'linecolor','non')
xlabel('x')
ylabel('t')
title('Numerical Solution')
colormap(jet(256))
colorbar
caxis([0,1])
Help I do not know why the code is not working

Accepted Answer

Alan Stevens
Alan Stevens on 11 Feb 2022
In line
V(i,j+1) = ((1-2*s-k).*V (i,j))+ (s.*(V (i-1,j) + V (i+1,j))) - (k.*(Rm.*Iinj+Vrest));
you have V (i-1,j). When i is 1 this is V(0,j). Matlab indexing starts at 1 not 0.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!