Error in sym / privsubsasgn and sym / subsasgn

6 ビュー (過去 30 日間)
seema redhu
seema redhu 2021 年 4 月 6 日
回答済み: Harshavardhan 2025 年 4 月 4 日
Hi.
My code is as follows:
t=850
R=10000
xA=0:.01:.99;
xB=1-xA
gIdeal=8.314*t*(xA.*log(xA)+xB.*log(xB));
gA=(-11276.24+(253.048446*t)-38.5844296*t*log(t)+0.018531982*t^(2)-5.764227*10.^(-6)*t^(3)+74092*t^(-1))
gB=(-8162.609+(137.236859*t)-22.8317533*t*log(t)-0.001912904*t^(2)-3.552*10.^(-9)*t^(3)+176667*t^(-1))
gStndrd=xA.*gA+xB.*gB;
gExcess=xA.*xB.*(113246.16-58.0001*t);
gBulk=gIdeal+gStndrd+gExcess
va=(9.78122*10^(-6)+6.745*10^(-10)*t+1.91*10^(-16)*t^(3)+3.76*10^(-6)/t)
vb=(11.99189*10^(-6)+1.485*10^(-10)*t+7.0*10^(-15)*t^(2)+7.84*10^(-6)/t-380*10^(-6)/t^(2))
VAb=va*xA+vb*xB
HAL=0.286;
HSI=0.2;
sigmaAL=0.914*(1-0.725*0.286/R);
AReaAl=1.091*(6.022*10^(23))^(1/3)*va^(2/3);
SIgmaSi=0.773*(1-0.725*0.2/R);
AReaSi=1.091*(6.022*10^(23))^(1/3)*vb^(2/3);
pal=xB.^(2).*(-3143.78+0.39297.*t)+8.314.*t.*log(xA);
psi=xA.^(2).*(113246.16-58.0001.*t)+8.314.*t.*log(xB);
psurfal=0.84*pal;
psurfsi=0.84*psi;
Pspl=psurfal-pal;
Pspi=psurfsi-psi;
syms x
a1=sigmaAL;
a2=SIgmaSi;
a3=8.314;
a4=t;
a5=0:.01:.99
a6=Pspl
a7=Pspi
a8=AReaAl
a9=AReaSi
for i=2:100
pp=a1+(1/a8)*(a6(i))+((a3*a4)/(a8))*log(x/a5(i))
qq=a2+(1/a9)*(a7(i))+((a3*a4)/(a9))*log((1-x)/(1-a5(i)))
rr=qq-pp
hh(i)=solve(rr,x)
end
a1=sigmaAL;
a2=8.314;
a3=t;
a4=AReaAl;
a5=0:.01:.99
doublea6=double(hh(i));
a6=doublea6;
a7=Pspl;
for i=2:100
ss(i)=a1+((a2*a3)/a4)*log(a6(i)/a5(i))+(1/a4)*a7(i)
end
ss=real(ss(i))
doublea1=double(VAb);
a1=doublea1;
doublea2=double(ss);
a2=doublea2;
a3=R*10^(-9);
for i=2:100
tt(i)=(a1(i)*a2(i))/a3;
end
tt=real(tt(i))
doublea1=double(gBulk);
a1=doublea1;
doublea2=double(tt(i));
a2=doublea2;
for i=2:100
L(i)=a1(i)+a2(i);
end
L=real(L(i))
plot(L(i),xB)
My code has the following error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in sym/privsubsasgn (line 1031)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 868)
C = privsubsasgn(L,R,inds{:});
Error in dimntotal10nm (line 40)
hh(i)=solve(rr,x)
  1 件のコメント
sterling lin
sterling lin 2022 年 4 月 13 日
Have you solved this problem? I meet the same issue. Could you teach me how to solve that?

サインインしてコメントする。

回答 (1 件)

Harshavardhan
Harshavardhan 2025 年 4 月 4 日
The error you're encountering is due to trying to assign a value to an array element when the right-hand side of the assignment has a different number of elements than expected. This happens in the line:
hh(i) = solve(rr, x)
The “solve” function can return multiple solutions, which means “hh(i)” might be trying to store multiple values (or none) in a single array element.
If you expect only one solution, ensure that the equation “rr” is structured to have a unique solution. You might need to check the logic of your equations.
If multiple solutions are possible and you want to store them, use a cell array.
hh = cell(1, 100); % Initialise the cell array
for i=2:100
pp=a1+(1/a8)*(a6(i))+((a3*a4)/(a8))*log(x/a5(i));
qq=a2+(1/a9)*(a7(i))+((a3*a4)/(a9))*log((1-x)/(1-a5(i)));
rr=qq-pp;
hh{i}=solve(rr,x);
end
If you are interested in a specific solution, select it explicitly:
hh= zeros(1,100); %Initializing hh with zeros
for i=2:100
pp=a1+(1/a8)*(a6(i))+((a3*a4)/(a8))*log(x/a5(i));
qq=a2+(1/a9)*(a7(i))+((a3*a4)/(a9))*log((1-x)/(1-a5(i)));
rr=qq-pp;
solutions=solve(rr,x);
if(size(solutions,1)>0)%Ensure Solutions has atleast one element
hh(i)=solutions(1); % Select the first solution, for example
else
hh(i)=-1;% Handle no solution, for example
end
end
For more information on “solve” type the following command in a MATLAB command window:
doc solve
Hope this helps.

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by