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

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);

 Accepted 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.

7 Comments

I don't know, that depends entirely on what the purpose of your code is in the first place, and how each of these individual variables are to be interpreted.
ok. thanks. now,
in below matlab code, I variable is equal to constant values and i can plot S vs t. However, when i made an array the variable I, for example I=linspace(0,10e-3,a)'; , i can't plot S vs I.
twg=2.5e-12;
tgw=110e-9;
twr=500e-12;
tr=1.2e-9;
tp=8.92e-12;
q=1.602e-19;
Beta=1e-4;
Gamma=0.025;
Alfa=4.6e-14;
Nr=3.27;
Length=2.45e-1;
Width=12e-4;
Thickness=2e-7;
Nb=6e16;
c=3e10;
Va=(Length*Width*Thickness*3);
Vg=c/Nr;
h=6.62e-34;
Lamda=1.55e-4;
R1=0.95;
R2=0.05;
step=0.00005e-9;
t=(0:step:5e-9)';
a=length(t);
I=10e-3;
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(t,S);
That doesn't really help, you've essentially just repeated your original post. You're computing Nw in a way that's clearly wrong. Only it's difficult for me to say how to do it the right way since I don't know what Nw is supposed to represent, nor do I know what k1, k2, k3, and k4 are supposed to represent.
My best guess is that you're going wrong at this step:
fNw=@(t,Nw,Ng) ((I./(q*Va))-(Nw./twg)-(Nw./twr)+(Ng./tgw));
First of all, you're passing t as an argument to this function, but t doesn't actually appear anywhere in that expression. So for given Nw and Ng, the value of t you end up passing in here makes no difference at all. If that's what you intended, you might as well drop t from the argument list:
fNw=@(Nw,Ng) ((I./(q*Va))-(Nw./twg)-(Nw./twr)+(Ng./tgw));
Now, for given Nw and Ng, this function would return one value corresponding to each element of the length-a vector I. As a result, fNw(Nw,Ng) is itself a vector of length a. I'm assuming that's not what you want, and in particular I'm guessing you expected the k's to be scalars, not vectors. To fix this, you'll need to replace I with some scalar (e.g., some particular element of I). What scalar that should be I can't say, though.
hi Dana, this code is calculating the coupled differential equations by using the 4th runge-kutta method. In this method, step size must be and step size is depent on the time. there is no the time the coupled differential equations, but to find the step size must be used the time.
Vefa, you didn't address a single one of the specific issues I raised in my previous post. Why did you include t as an argument to a function but then never use t in that function? Why are you using a vector I in a function when you're expecting that function to output a scalar (or is that even the case)?
Also, this dif. euations are derived according to t (time). for example dy/dt=a.y+b/y

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 18 Sep 2020

Commented:

on 18 Sep 2020

Community Treasure Hunt

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

Start Hunting!