How can i solve the error in the code given below? I'm getting the following error In an assignment A(:) = B, the number of elements in A and B must be the same.

4 views (last 30 days)
syms T
h=1.054*(10^-34);
T=[1:20:1001];
kb=1.38*(10^-23);
g=1.1;
vd=6703.3;
M=4.6637*(10^-26);
td=355.33;
A=(h*(g^2)*(kb^2))/(M*(vd^2)*td*(h^2));
c=((kb^4).*(T.^3))/(2*(pi^2)*vd*(h^3));
g1=2*(10^-4);
V=2.0024*(10^-29);
syms x xd
syms ru(x) ru1(x) ru2(x) f(x) f1(x) q kc
ru(x)=(1./(A.*exp(-td./(3.*T)).*(T.^3)*(x^2)));
ru1(x)=(4*pi*(vd^3)*(h^4))./(g1*V*(kb^4)*(T.^4)*(x^4));
ru2=ru+ru1;
f(x)=((x^2)*exp(x))/((exp(x)-1)^2);
f1=ru2*f;
k=matlabFunction(f1);
xd=td./T ;
q=0;
kc=0;
for i=1:1:length(xd)
q(i)=integral(k,0,xd(i),'ArrayValued',true);
kc(i)=c(i).*q(i)
end
I'm getting the following error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in hw4 (line 26)
q(i)=integral(k,0,xd(i),'ArrayValued',true);

Answers (1)

Robert
Robert on 6 Nov 2017
Your function k returns 51 outputs. MATLAB issues that error when your code asks to store the 51 corresponding integrals in the one spot you have named: q(i). Instead, you could pre-allocate an array for q and kc and fill the rows with each iteration of the for loop.
q = nan(length(k(0)), length(xd));
kc = q;
for i=1:length(xd) % if step ~= 1, you will need to change size of q and kc
I = integral(k,0,xd(i),'ArrayValued',true);
q(i, :) = I;
kc(i, :) = c(i) .* I;
end

Categories

Find more on Preprocessing Data 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!