MATLAB: Unable to perform assignment because the left and right sides have a different number of elements

errorfor loop

Hi everbody, in my program, i can plot S vs t, but i can not plot S vs I and i take the erros 'Unable to perform assignment because the left and right sides have a different number of elements'. to solution this problem very important for me. thanks in advance.
step=0.0005e-9;
t=(0:step:50e-9)';
a=length(t);
I=linspace(0,10e-3,a)';
Nw=zeros(a,1);
Ng=zeros(a,1);
S=zeros(a,1);
Pout=zeros(a,1);
Alfa_m=(log(1/(R1*R2)))/(2*Length*Nr);
Pcon=((Vg*h*Va*Alfa_m*c)/(Lamda*Gamma));
fNw=@(t,Nw,Ng) ((I./(q*Va))-(Nw./twg)-(Nw./twr)+(Ng./tgw));
fNg=@(t,Nw,Ng,S) ((Nw./twg)-(Ng./tgw)-(Ng./tr)-Gamma*Vg*Alfa.*(Ng-Nb).*S);
fS=@(t,Ng,S) (Gamma*Vg*Alfa.*(Ng-Nb).*S)-(S./tp)+(Beta.*(Ng./tr));
for i=1:a-1
k1=fNw(t(i),Nw(i),Ng(i));
m1=fNg(t(i),Nw(i),Ng(i),S(i));
n1=fS(t(i),Ng(i),S(i));
k2=fNw(t(i)+step/2,Nw(i)+step/2*k1,Ng(i)+step/2*m1);
m2=fNg(t(i)+step/2,Nw(i)+step/2*k1,Ng(i)+step/2*m1,S(i)+step/2*n1);
n2=fS(t(i)+step/2,Ng(i)+step/2*m1,S(i)+step/2*n1);
k3=fNw(t(i)+step/2,Nw(i)+step/2*k2,Ng(i)+step/2*m2);
m3=fNg(t(i)+step/2,Nw(i)+step/2*k2,Ng(i)+step/2*m2,S(i)+step/2*n2);
n3=fS(t(i)+step/2,Ng(i)+step/2*m2,S(i)+step/2*n2);
k4=fNw(t(i)+step,Nw(i)+step*k3,Ng(i)+step*m3);
m4=fNg(t(i)+step,Nw(i)+step*k3,Ng(i)+step*m3,S(i)+step*n3);
n4=fS(t(i)+step,Ng(i)+step*m3,S(i)+step*n3);
Nw(i+1)=Nw(i)+step/6*(k1+2*k2+2*k3+k4);
Ng(i+1)=Ng(i)+step/6*(m1+2*m2+2*m3+m4);
S(i+1)=S(i)+step/6*(n1+2*n2+2*n3+n4);
end
plot(I,S);

Best Answer

Your varibles k1, k2, k3, and k4 are vectors, so when you do the line near the bottom of your loop
Nw(i+1)=Nw(i)+step/6*(k1+2*k2+2*k3+k4);
the right-hand side evaluates to a vector of the same length as the k's, which you're trying to assign to a single element of Nw. Hence the error. I think you'll have a similar problem with the next two lines after that one as well.
Related Question