MATLAB Answers

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

59 views (last 30 days)
Erkan
Erkan on 18 Sep 2020
Commented: Erkan on 18 Sep 2020
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);

  0 Comments

Sign in to comment.

Accepted Answer

Dana
Dana on 18 Sep 2020
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

Show 4 older comments
Erkan
Erkan on 18 Sep 2020
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.
Dana
Dana on 18 Sep 2020
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)?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!