MATLAB Answers

Matrix dimensions must agree.

1 view (last 30 days)
Nyimatoulie Cham
Nyimatoulie Cham on 25 Oct 2020
Hello all,
Not sure why this error "Matrix dimensions must agree." is popping up on line 37. I'm trying to simulate adding an electrode.
Matrix dimensions must agree.
Error in hodgkinhuxeqb (line 37)
Vm = V_in-V_out;
Here is my code:
function dSdt = hodgkinhuxeqb(T,S,I,Nd, D)
%Function hodgkinhuxeq
% Inputs: t - time
% I_add - input current
% v - voltage
%variables
V_in = S(1:6:end-5);
m = S(2:6:end-4);
h = S(3:6:end-3);
n = S(4:6:end-2);
s = S(5:6:end-1);
f = S(6:6:end);
%potentials
g_na = 120;
g_k = 36;
g_l = 0.3;
E_k = -77;
E_na = 50;
E_l = -54;
cm = 1;
tau_s = 6;
tau_f = 2;
esyn = 0;
gsyn = 0.01;
sigma = 0.00179;
V_out = I./(4.*pi().*sigma.*D);
V_out = transpose(V_out);
Vm = V_in-V_out;
% alpha and betas eqs
a_m = -0.1.*(Vm+35)./(exp(-0.1.*(Vm+35))-1);
b_m = 4.0.*exp((-Vm-60)./18);
a_h = 0.07.*exp(-0.05*(Vm+60));
b_h = 1./(1+exp(-0.1*(Vm+30)));
a_n = -0.01*(Vm+50)./(exp(-0.1*(Vm+50))-1);
b_n = 0.125*exp(-0.0125*(Vm+60));
%dv/dt sections
K_1 = (g_k.*n.^4.*(E_k-Vm));
Na_1 = (g_na.*m.^3.*h).*(E_na-Vm);
L_1 = g_l.*(E_l-Vm);
%Isyn = gsyn.*(s(end/3)-f(end/3)).*(esyn-v(end/3));
%derivats m,h,n,s
%dmdt = 0;
%dndt = 0;
%dhdt = 0;
dmdt = a_m.*(1-m)-b_m.*m;
dhdt = a_h.*(1-h)-b_h.*h;
dndt = a_n.*(1-n)-b_n.*n;
dfdt = -f/tau_f;
dsdt = -s/tau_s;
%dVdt = (1/cm).*((K_1)+(Na_1)+(L_1)+gsyn.*(s-f).*(esyn-v));
%membrane current for all the compartments
dVdt = 1./cm.*(g_l.*(E_l-Vm));
%na + k currents for all compartments
dVdt = dVdt + 1./cm.*(g_na.*m.^3.*h.*(E_na-Vm)+g_k.*n.^4.*(E_k-Vm));
% applied stimulus to the first compartment
%dVdt(1) = dVdt(1)+1./cm*Ielec;
%applied stim to middle of dendrite
%dVdt(end/2) = dVdt(end/2)+1./cm*Ielec;
R = 225;
a = 0.001;
dx = 0.001;
r = a/(2*R*dx^2);
% left and right sections
dVdt(2:end) = dVdt(2:end)+r.*(Vm(1:end-1)-Vm(2:end));
dVdt(1:end-1) = dVdt(1:end-1)+r.*(Vm(2:end)-Vm(1:end-1));
%merge output
dSdt = zeros(size(S));
dSdt(1:6:end-5) = dVdt;
dSdt(2:6:end-4) = dmdt;
dSdt(3:6:end-3) = dhdt;
dSdt(4:6:end-2) = dndt;
dSdt(5:6:end-1) = dsdt;
dSdt(6:6:end) = dfdt;
dSdt = [dVdt'; dmdt'; dhdt'; dndt'; dfdt'; dsdt'];
dSdt = dSdt(:);
end

  2 Comments

Walter Roberson
Walter Roberson on 25 Oct 2020
We do not know size(I) or size(D) so we cannot figure out size(v_out)
We do not know the orientation of S, so although if we guess that S is a vector, the length of v_in would be length(S)/6 but we do not know if v_in would be a row vector or column vector.
We do not know your MATLAB release, so we do not know if your MATLAB has implicit expansion or not (and we do not know if you intend implicit expansion to be deliberately used.)
Is there a reason why you do not reshape(S,6, []) and then extract rows of that, instead of using 2:6:end-4 and so on?
Nyimatoulie Cham
Nyimatoulie Cham on 25 Oct 2020
Hi Walter, I have integrator running concurrently with this:
I've stated the values of I and D on this code, not sure if this better explains whats going on
N = 100;
S0 = [-65, 0.03, 0.7,0.26,0,0]; %values for v,m,nh
S0 = repmat(S0, 1, N);
I = -100; %input current of cathode
T = 10 %time
Nd = 6; %number of compartments
d = .01; %length of axon
x = 0:.001:.009; %distance between axon and electrode
D = sqrt(x.^2+d.^2);
[t1, S1] = ode15s(@hodgkinhuxeqb,[0 T], S0,[],0, Nd, D);
[t2, S2] = ode15s(@hodgkinhuxeqb,[T 20], S0,[],I, Nd, D);
[t3, S3] = ode15s(@hodgkinhuxeqb,[20 30], S0,[],0, Nd, D);
t = [t1;t2;t3];
S = [S1;S2;S3];
plot(t,S(:,1:6:end));

Sign in to comment.

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 1 Nov 2020
You can refer to Set Breakpoints & Debugging and Analysis to set breakpoint at line 37 and idenitfy the cause of the error.
While executing the code it appears that the size of V_in is 100x1 and that of V_out is 10x1, hence the error "Matrix dimensions must agree."

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!