The professor asked to remake the C++ code in MATLAB.

Gives the error "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 9991-by-1." I'm not very smart to understand this. We need to find zz and plot the dependence of zz on temperature T. T varies from 10 to 10000. How to correctly place for in order for the program to work?
clc
clear all
nnucl = 1;
nH = 10.^-7;
nH2 = 10.^-3;
nHp = 10.^-2;
nOI = 10.^-8;
ne = 10.^-2;
kb = 1.3806488e-16;
g1g0 = 0.6;
E10 = 230.0;
A10 = 8.9.*(10.^-5);
g2g0 = 0.2;
E20 = 330.0;
A20 = 1.3.*(10.^-10);
g2g1 = 0.33333333333;
E21 = 98.0;
A21 = 1.8.*(10.^-5);
T = (10:10000)';
qH21 = (2.7.*(10.^-11).*(T.^0.362) + 3.46.*(10.^-11).*(T.^0.316)).*0.5;
qH1 = 9.20.*(10.^-11).*(T.^0.67);
qe1 = 5.21.*(10.^-10).*(T.^(-0.075));
qHp1 = 6.38.*(10.^-11).*(T.^0.4);
qH22 = (5.49.*(10.^-11).*(T .^0.317) + 7.07.*(10.^-11).*(T .^0.268)).*0.5;
qH2 = 4.30.*(10.^-11).*(T .^0.8);
qe2 = 4.86.*(10.^-10).*(T .^-0.026);
qHp2 = 6.10.*(10.^-13).*(T .^1.1);
qH23 = (2.74.*(10.^-14).*(T .^1.06) + 3.33.*(10.^-15).*(T .^1.360)).*0.5;
qH3 = 1.10.*(10.^-10).*(T .^0.44);
qe3 = 1.08.*(10.^-14).*(T .^0.926);
qHp3 = 3.43.*(10.^-10).*(T .^0.19);
C10 = zeros(1,5);
C20 = zeros(1,5);
C21 = zeros(1,5);
for r=1:5
C10(:,r) = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);
C20(:,r)= (qH22.*qH22 + qH2.*nH + qHp2.*qHp2 + ne.*qe2);
C21(:,r)= (qH23.*qH23 + qH3.*nH + qHp3.*qHp3 + ne.*qe3);
end
C01 = C10.*g1g0.*exp(-E10./T);
C02 = C20.*g2g0.*exp(-E20./T);
C12= C21.*g2g1.*exp(-E21./T);
tmp = (C01 + C02);
P = ( A10 + C10)./tmp;
Q = ( A20 + C20)./tmp;
tmp = ( C10 + C12);
V=C01./tmp;
W=( A21 + C21)./tmp;
K0 = (P.*W + Q)./(1 - P.*V);
K1 = V.*K0 + W;
n2 = (1 + K0 + K1);
n1 = K1.* n2;
n0 = K0.* n2;
zz=(A10.*E10.*n1 +(A20.*E20+A21.*E21).*n2);
plot (T,zz)
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 9991-by-1.
Error in finestructures (line 46)
C10(:,r) = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);

Answers (3)

T = (10:10000)';
Large vector. 9991 x 1 elements.
qH21 = (2.7.*(10.^-11).*(T.^0.362) + 3.46.*(10.^-11).*(T.^0.316)).*0.5;
qH1 = 9.20.*(10.^-11).*(T.^0.67);
qe1 = 5.21.*(10.^-10).*(T.^(-0.075));
qHp1 = 6.38.*(10.^-11).*(T.^0.4);
qH22 = (5.49.*(10.^-11).*(T .^0.317) + 7.07.*(10.^-11).*(T .^0.268)).*0.5;
qH2 = 4.30.*(10.^-11).*(T .^0.8);
qe2 = 4.86.*(10.^-10).*(T .^-0.026);
qHp2 = 6.10.*(10.^-13).*(T .^1.1);
qH23 = (2.74.*(10.^-14).*(T .^1.06) + 3.33.*(10.^-15).*(T .^1.360)).*0.5;
qH3 = 1.10.*(10.^-10).*(T .^0.44);
qe3 = 1.08.*(10.^-14).*(T .^0.926);
qHp3 = 3.43.*(10.^-10).*(T .^0.19);
Those all use all of the vector T, so all of the results are length 9991 x 1.
for r=1:5
C10(:,r) = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);
C20(:,r)= (qH22.*qH22 + qH2.*nH + qHp2.*qHp2 + ne.*qe2);
C21(:,r)= (qH23.*qH23 + qH3.*nH + qHp3.*qHp3 + ne.*qe3);
end
You use r as an index for the destination, but you do not use r as an index for any of the right-hand sides, so each iteration is going to produce exactly the same size.
As discussed above, each of those variables such as qH21 are 9991 x 1, so the right hand side of each of the calculations is 9991 x 1.
But with you having pre-initialized C10, C20, C21 to 1 x 5, then C10(:,r) designates a scalar location C10(1,r) . Which is a problem because the right hand side is 9991 x 1.
You need to figure out why you are looping over r there. and what size of output you are expecting.
C01 = C10.*g1g0.*exp(-E10./T);
C10 was declared 1 x 5. You .* that with a calculation based on T which is 9991 x 1. That is potentially possible in MATLAB, giving a 9991 x 5 result.
Those would probably lead to zz being 9991 x 5. In the plot(T, zz) that would result in 5 draw lines, each with 9991 points in the line. Is that what you are looking for?
Egor Popov
Egor Popov on 19 Mar 2022
Edited: Egor Popov on 19 Mar 2022
I don't get hung up on r. I just don't know what to do anymore. I need to get the dependency graph (T,zz). The for array is probably in the wrong place. I just can't figure this program out anymore. Thanks for the answer.

2 Comments

Please help me solve this problem. idk what I need to do next in order for the program to work and give out a graph.
We need to find zz and plot the dependence of zz on temperature T
That does not require a for loop.
C10 = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);

Sign in to comment.

Don't know if this is what you want:
nnucl = 1;
nH = 10.^-7;
nH2 = 10.^-3;
nHp = 10.^-2;
nOI = 10.^-8;
ne = 10.^-2;
kb = 1.3806488e-16;
g1g0 = 0.6;
E10 = 230.0;
A10 = 8.9.*(10.^-5);
g2g0 = 0.2;
E20 = 330.0;
A20 = 1.3.*(10.^-10);
g2g1 = 0.33333333333;
E21 = 98.0;
A21 = 1.8.*(10.^-5);
T = (10:100)';
qH21 = (2.7.*(10.^-11).*(T.^0.362) + 3.46.*(10.^-11).*(T.^0.316)).*0.5;
qH1 = 9.20.*(10.^-11).*(T.^0.67);
qe1 = 5.21.*(10.^-10).*(T.^(-0.075));
qHp1 = 6.38.*(10.^-11).*(T.^0.4);
qH22 = (5.49.*(10.^-11).*(T .^0.317) + 7.07.*(10.^-11).*(T .^0.268)).*0.5;
qH2 = 4.30.*(10.^-11).*(T .^0.8);
qe2 = 4.86.*(10.^-10).*(T .^-0.026);
qHp2 = 6.10.*(10.^-13).*(T .^1.1);
qH23 = (2.74.*(10.^-14).*(T .^1.06) + 3.33.*(10.^-15).*(T .^1.360)).*0.5;
qH3 = 1.10.*(10.^-10).*(T .^0.44);
qe3 = 1.08.*(10.^-14).*(T .^0.926);
qHp3 = 3.43.*(10.^-10).*(T .^0.19);
%C10 = zeros(1,5);
%C20 = zeros(1,5);
%C21 = zeros(1,5);
%for r=1:5
% C10(:,r) = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);
% C20(:,r)= (qH22.*qH22 + qH2.*nH + qHp2.*qHp2 + ne.*qe2);
% C21(:,r)= (qH23.*qH23 + qH3.*nH + qHp3.*qHp3 + ne.*qe3);
%end
C10 = (qH21.*qH21 + qH1.*nH + qHp1.*qHp1 + ne.*qe1);
C20= (qH22.*qH22 + qH2.*nH + qHp2.*qHp2 + ne.*qe2);
C21= (qH23.*qH23 + qH3.*nH + qHp3.*qHp3 + ne.*qe3);
C01 = C10.*g1g0.*exp(-E10./T);
C02 = C20.*g2g0.*exp(-E20./T);
C12= C21.*g2g1.*exp(-E21./T);
tmp = (C01 + C02);
P = ( A10 + C10)./tmp;
Q = ( A20 + C20)./tmp;
tmp = ( C10 + C12);
V=C01./tmp;
W=( A21 + C21)./tmp;
K0 = (P.*W + Q)./(1 - P.*V);
K1 = V.*K0 + W;
n2 = (1 + K0 + K1);
n1 = K1.* n2;
n0 = K0.* n2;
zz=(A10.*E10.*n1 +(A20.*E20+A21.*E21).*n2);
plot (T,zz)

Categories

Asked:

on 17 Mar 2022

Answered:

on 19 Mar 2022

Community Treasure Hunt

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

Start Hunting!