Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

7 次查看(过去 30 天)
clc;clear;format compact;
syms V
P=[0.98 1.97 4.93 9.86 49.36 98.69]; %atm
T=[573 573 573 573 573 573]; %K
R=461.5; %Pa*m3/mol*K
R=convpres(R,'Pa','atm');
R=R*1000; %L*atm/mol*K
% Van der Waals
Tc=647.1;
Pc=convpres(22060000,'Pa','atm');
a1=(27*R^2*Tc^2)/(64*Pc);
b1=(R*Tc)/(8*Pc);
VW=((R*T)/(V-b1))-(a1/(V^2))==P;
for i=1:6
B(1,i)=solve(VW(i),V)
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in sym/privsubsasgn (line 1200)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in indexing (line 1031)
C = privsubsasgn(L,R,inds{:});

回答(1 个)

Walter Roberson
Walter Roberson 2023-10-16
clc;clear;format compact;
syms V
P=[0.98 1.97 4.93 9.86 49.36 98.69]; %atm
T=[573 573 573 573 573 573]; %K
R=461.5; %Pa*m3/mol*K
R=convpres(R,'Pa','atm');
R=R*1000; %L*atm/mol*K
% Van der Waals
Tc=647.1;
Pc=convpres(22060000,'Pa','atm');
a1=(27*R^2*Tc^2)/(64*Pc);
b1=(R*Tc)/(8*Pc);
VW=((R*T)/(V-b1))-(a1/(V^2))==P
VW = 
for i=1:6
B{1,i}=solve(VW(i),V);
end
B
B = 1×6 cell array {3×1 sym} {3×1 sym} {3×1 sym} {3×1 sym} {3×1 sym} {3×1 sym}
Your equation is a cubic in V, and so it has three roots.
format long g
cell2mat(cellfun(@double, B, 'uniform', 0))
ans =
Columns 1 through 3 3.22856391464644 - 0.71413166224905i 3.23235043527525 - 0.710926542708146i 3.24382222761193 - 0.701023878142358i 3.22856391464644 + 0.71413166224905i 3.23235043527525 + 0.710926542708146i 3.24382222761193 + 0.701023878142358i 2658.31153778874 + 0i 1320.0066466243 + 0i 524.578769236741 + 0i Columns 4 through 6 3.26344891359564 - 0.683381010798684i 3.45021491277963 - 0.450133424499785i 3.11984383082587 + 0i 3.26344891359564 + 0.683381010798684i 3.45021491277963 + 0.450133424499785i 4.51149984762074 + 0i 259.852401068882 + 0i 47.6648286645631 + 0i 20.5054138525534 + 0i
Only one of the roots is real-valued -- but you asked to solve the equation, not to find real-valued solutions.
  5 个评论
Torsten
Torsten 2023-10-16
编辑:Torsten 2023-10-17
clc;clear;format compact;
syms V
P=[0.98 1.97 4.93 9.86 49.36 98.69]; %atm
T=[573 573 573 573 573 573]; %K
R=461.5; %Pa*m3/mol*K
R=convpres(R,'Pa','atm');
R=R*1000; %L*atm/mol*K
% Van der Waals
Tc=647.1;
Pc=convpres(22060000,'Pa','atm');
a1=(27*R^2*Tc^2)/(64*Pc);
b1=(R*Tc)/(8*Pc);
VW=((R*T)/(V-b1))-(a1/(V^2))==P
VW = 
for i=1:6
B{1,i}=solve(VW(i),V);
end
format long g
M = cell2mat(cellfun(@double, B, 'uniform', 0))
M =
Columns 1 through 3 3.22856391464644 - 0.71413166224905i 3.23235043527525 - 0.710926542708146i 3.24382222761193 - 0.701023878142358i 3.22856391464644 + 0.71413166224905i 3.23235043527525 + 0.710926542708146i 3.24382222761193 + 0.701023878142358i 2658.31153778874 + 0i 1320.0066466243 + 0i 524.578769236741 + 0i Columns 4 through 6 3.26344891359564 - 0.683381010798684i 3.45021491277963 - 0.450133424499785i 3.11984383082587 + 0i 3.26344891359564 + 0.683381010798684i 3.45021491277963 + 0.450133424499785i 4.51149984762074 + 0i 259.852401068882 + 0i 47.6648286645631 + 0i 20.5054138525534 + 0i
for i = 1:size(M,2)
indices = find(abs(imag(M(:,i))) < 1e-6);
sol{i} = M(indices,i); % better save in cell array for the case of 3 real solutions
end
sol
sol = 1×6 cell array {[2658.31153778874]} {[1320.0066466243]} {[524.578769236741]} {[259.852401068882]} {[47.6648286645631]} {3×1 double}
Walter Roberson
Walter Roberson 2023-10-17
Your last P has three real-valued solutions.
clc;clear;format compact;
syms V real
P=[0.98 1.97 4.93 9.86 49.36 98.69]; %atm
T=[573 573 573 573 573 573]; %K
R=461.5; %Pa*m3/mol*K
R=convpres(R,'Pa','atm');
R=R*1000; %L*atm/mol*K
% Van der Waals
Tc=647.1;
Pc=convpres(22060000,'Pa','atm');
a1=(27*R^2*Tc^2)/(64*Pc);
b1=(R*Tc)/(8*Pc);
VW=((R*T)/(V-b1))-(a1/(V^2))==P;
for i=1:6
B{1,i} = solve(VW(i),V, 'maxdegree', 3);
end
cellfun(@double, B, 'uniform', 0)
ans = 1×6 cell array {[2.6583e+03]} {[1.3200e+03]} {[524.5788]} {[259.8524]} {[47.6648]} {3×1 double}
B{end}
ans = 
format long g
double(B{end})
ans =
20.5054138525534 - 7.21326454514511e-130i 3.11984383082587 + 3.60663227257255e-130i 4.51149984762074 + 3.60663227257255e-130i

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by