why do i keep getting this error "Error using + Matrix dimensions must agree" ?
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kosgey Kip
il 24 Feb 2019
Commentato: Kosgey Kip
il 25 Feb 2019
Please assist:
When i try running the following i keep getting the above error:
%initial conditions
y0=[19.125;19.125;114.75;51;51;1;2;1;55;50;0.5;3.187;3.187;19.125;8.5;8.5;1;2;1;55;50;0.5;1.594;1.594;9.5625;4.25;4.25;1;2;1;55;50;0.5;1.594;1.594;9.5625;4.25;4.25;1;2;1;55;50;0.5;6.375;6.375;38.250;17;17;1;2;1;55;50;0.5];
t0=0;
tEnd=282;
h=0.0006944444;
N=ceil((tEnd-t0)/h);
%initilising solution
T=t0:h:tEnd;
Y=zeros(55,N+1);
Y(:,1)=y0;
%boundaries
for i=1:N
T(i+1)=T(i)+h;
Y(Y<0)=0;
y11=Y(11);
y11(y11>8.5)=8.5;
%update y
yi=Y(:,i);
a1=UASBFun(T(i), yi);
a2=UASBFun(T(i)+0.5*h, yi+0.5*a1*h);
a3=UASBFun(T(i)+0.5*h, yi+0.5*a2*h);
a4=UASBFun(T(i)+ h, yi+ a3*h);
yNew=yi+(h/6)*(a1+2*a2+2*a3+a4);
Y(:,i+1)=yNew;
end
%plotting
yEnd=Y(:,end);
plot(T,Y(50,:),'-r'); hold on
plot(T,Y(51,:),'-g'); hold on
plot(T,Y(52,:),'--r'); hold on
plot(T,Y(53,:),'-k'); hold on
plot(T,Y(54,:),'-b'); hold on
plot(T,Y(55,:),'--g'); hold off
xlabel('Time (days)'); ylabel('Concentration (g/m^3)');
Function:
function fval=UASBFun(~,y)
%Define variables
Xaoba=y(1); Xaobb=y(12); Xaobc=y(23); Xaobd=y(34); Xaobe=y(45);
Xnoba=y(2); Xnobb=y(13); Xnobc=y(24); Xnobd=y(35); Xnobe=y(46);
Xamxa=y(3); Xamxb=y(14); Xamxc=y(25); Xamxd=y(36); Xamxe=y(47);
Xhana=y(4); Xhanb=y(15); Xhanc=y(26); Xhand=y(37); Xhane=y(48);
Xhaera=y(5); Xhaerb=y(16); Xhaerc=y(27); Xhaerd=y(38); Xhaere=y(49);
Xsa=y(6); Xsb=y(17); Xsc=y(28); Xsd=y(39); Xse=y(50);
Ssea=y(7); Sseb=y(18); Ssec=y(29); Ssed=y(40); Ssee=y(51);
Sno3a=y(8); Sno3b=y(19); Sno3c=y(30); Sno3d=y(41); Sno3e=y(52);
Sno2a=y(9); Sno2b=y(20); Sno2c=y(31); Sno2d=y(42); Sno2e=y(53);
Snh4a=y(10); Snh4b=y(21); Snh4c=y(32); Snh4d=y(43); Snh4e=y(54);
So2a=y(11); So2b=y(22); So2c=y(33); So2d=y(44); So2e=y(55);
The functions for each variable are long though hence i stop at this.
PLease someone assist here please.....
2 Commenti
Geoff Hayes
il 24 Feb 2019
Kosgey - which line of code is throwing the error message? I can't run the above because of errors with your UASBFun function (not sure what you are attempting there).
Anyway, the error message is telling you that you are trying to add two matrices together that have different dimensions...
Risposta accettata
Walter Roberson
il 25 Feb 2019
yi is initialized from one column of Y, Y is initialized to 55 rows. So yi has 55 entries.
a1 is computed by calling UASBFun, passing in something that is ignored, and passing in yi. UASBFun takes in those 55 values, and computes 11 fval from it.
The code for calculating a2 tries to do yi + 0.5 * a1 * h . 0.5 and h are scalar, a1 is length 11 as noted a moment ago. yi is length 55 as noted a moment ago. So you are trying to add something length 55 together with something length 11. That is not going to work.
I do not think that your code can be fixed; I think you will have to abandon it.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!