How to correct the following error?Unable to perform assignment because the left and right sides have a different number of elements. Error in ps4_3 (line 33) Ca(i+1)=Ca​(i)+((h/6)​*(k1+2*k2+​2*k3+k4));

1 view (last 30 days)
h=0.1;
t=[0:h:10000]';
N=length(t);
Ca=zeros(N,1);
T=zeros(N,1);
Cd=zeros(N,1);
Ca(1)=10;
T(1)=295.2;
Cd(1)=0;
y1=2000*exp(-49000/(8.345*T));
y2=3400000*exp(-65000/(8.345*T));
y3=263000*exp(-57000/(8.345*T));
F1= @(t,Ca,T,Cd) (((10-Ca)/300)-(y1.*Ca.^3-y2.*Ca.^0.5-y3.*Ca));
G1=@(t,Ca,T,Cd) ((295.2-T/300)-(1.0303/42)+((5.6*y1*Ca.^3+5.*y2*Ca.^0.5+6*y3*Ca)*(10/4.2)));
M1=@(t,Ca,T,Cd) ((-Cd/300)+y3*Ca);
for i=1:(N-1)
k1=F1(t(i),Ca(i),T(i),Cd(i));
l1=G1(t(i),Ca(i),T(i),Cd(i));
p1=M1(t(i),Ca(i),T(i),Cd(i));
k2=F1(t(i)+(h/2),Ca(i)+(k1/2),T(i),Cd(i));
l2=G1(t(i)+(h/2),Ca(i),T(i)+(l1/2),Cd(i));
p2=M1(t(i)+(h/2),Ca(i),T(i),Cd(i)+(p1/2));
k3=F1(t(i)+(h/2),Ca(i)+(k2/2),T(i),Cd(i));
l3=G1(t(i)+(h/2),Ca(i),T(i)+(l2/2),Cd(i));
p3=M1(t(i)+(h/2),Ca(i),T(i),Cd(i)+(p2/2));
k4=F1(t(i)+h,Ca(i)+(k3),T(i),Cd(i));
l4=G1(t(i)+h,Ca(i),T(i)+(l3),Cd(i));
p4=M1(t(i)+h,Ca(i),T(i),Cd(i)+(p3));
Ca(i+1)=Ca(i)+((h/6)*(k1+2*k2+2*k3+k4));
T(i+1)=T(i)+((h/6)*(l1+2*l2+2*l3+l4));
Cd(i+1)=Cd(i)+((h/6)*(p1+2*p2+2*p3+p4));
end
Ca
T
Cd
plot(t,Ca)
xlabel("Time")
ylabel("Ca")
plot(T,Cd)
xlabel("Temperature")
ylabel("Cd")
  1 Comment
Tommy
Tommy on 27 Mar 2020
k1, k2, k3, and k4 are 1x100001 arrays (while MATLAB is expecting them to be scalar). This is because your functions F1, G1, and M1 use y1, y2, and y3, which are also 1x100001 arrays. Is it possible you instead want to call y1(i), y2(i), and y3(i) inside those functions? If so, you'll need to pass i in:
F1= @(i,t,Ca,T,Cd) (((10-Ca)/300)-(y1(i).*Ca.^3-y2(i).*Ca.^0.5-y3(i).*Ca));

Sign in to comment.

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 2 Apr 2020
As mentioned already by @Tommy, the error is caused because of the size of the ((h/6)*(k1+2*k2+2*k3+k4)) is 1x100001. Then you are adding this vector to scalar and trying it to assign to a scalar, Which is causing the error.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!