fsolve with 7 equations 6 unknowns: Error using vertcat Dimensions of arrays being concatenated are not consistent

6 次查看(过去 30 天)
I have a very brute code setup to use fsolve to solve a system of 7 equations and 6 unknowns, but I keep getting the error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent
Here is the code:
% Calculating vapor and liquid fraction of flash vessel outlet for
% a mixture of n-pentane, n-hexane, and argon
% Given ideal gas and ideal solution behavior
% Known variables
P = 1; % pressure of vessel (bar)
F = 150; % feed flow rate (mol/s)
z_ar = 0.05; % mol fraction of argon in feed
z_np = 0.6; % mol fraction of n-pentane in feed
z_nh = 0.35; % mol fraction of n-hexane in feed
y_ar = 0.05; % mol fraction of argon in feed
N_ar = z_ar*F; % # of moles of argon in feed
% Antoinne constants
% n-pentane valid over 286-342K (NIST)
A_np = 3.9892;
B_np = 1070.617;
C_np = -40.454;
% n-hexane valid over 269-341K (NIST)
A_nh = 4.00266;
B_nh = 1171.53;
C_nh = -48.784;
% x1 = y_nh
% x2 = y_np
% x3 = x_nh
% x4 = x_np
% x5 = beta
% x6 = T
% eqn1 = 0.05*F*y_nh - beta*y_nh*F == 0;
% eqn2 = 0.95*F*x_np - (1-beta)*x_np*F == 0;
% eqn3 = (y_ar)+(y_np - x_np)+(y_nh - x_nh) - ...
% ((K_np-1)*z_np/((1+beta)*(K_np-1)))+((K_nh-1)*z_nh/((1+beta)*(K_nh-1))) == 0;
% eqn4 = y_np/x_np - 10^(A_np - (B_np/(T+C_np))) == 0;
% eqn5 = y_nh/x_nh - 10^(A_nh - (B_nh/(T+C_nh))) == 0;
% eqn6 = y_ar + y_nh + y_np == 1;
% eqn7 = x_nh + x_np == 1;
f = @(x) [0.05*F*x(1) - x(5)*x(1)*F;
0.95*F*x(4) - (1-x(5))*x(4)*F;
(y_ar)+(x(2) - x(4))+(x(1) - x(3)) - ...
(((10^(A_np - (B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np - (B_np/(x(6)+C_np))))-1)))...
+(((10^(A_nh - (B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh - (B_nh/(x(6)+C_nh)))-1)));
x(2)/x(4) - 10^(A_np - (B_np/(x(6)+C_np)));
x(1)/x(3) - 10^(A_nh - (B_nh/(x(6)+C_nh)));
y_ar + x(1) + x(2);
x(3) + x(4)];
% eqn1 = 0.05*F*x(1) - x(5)*x(1)*F;
% eqn2 = 0.95*F*x(4) - (1-x(5))*x(4)*F;
% eqn3 = (y_ar)+(x(2) - x(4))+(x(1) - x(3)) - ...
% (((10^(A_np - (B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np - (B_np/(x(6)+C_np))))-1)))...
% +(((10^(A_nh - (B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh - (B_nh/(x(6)+C_nh)))-1)));
% eqn4 = x(2)/x(4) - 10^(A_np - (B_np/(x(6)+C_np)));
% eqn5 = x(1)/x(3) - 10^(A_nh - (B_nh/(x(6)+C_nh)));
% eqn6 = x(1)/x(3) - 10^(A_nh - (B_nh/(x(6)+C_nh)));
x0 = [0; 0; 0; 0; 0; 1; 1];
fsolve(f,x0)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in solution>@(x)[0.05*F*x(1)-x(5)*x(1)*F;0.95*F*x(4)-(1-x(5))*x(4)*F;(y_ar)+(x(2)-x(4))+(x(1)-x(3))-(((10^(A_np-(B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np-(B_np/(x(6)+C_np))))-1))),+(((10^(A_nh-(B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh-(B_nh/(x(6)+C_nh)))-1)));x(2)/x(4)-10^(A_np-(B_np/(x(6)+C_np)));x(1)/x(3)-10^(A_nh-(B_nh/(x(6)+C_nh)));y_ar+x(1)+x(2);x(3)+x(4)] (line 39)
f = @(x) [0.05*F*x(1) - x(5)*x(1)*F;

Error in fsolve (line 270)
fuser = feval(funfcn{3},x,varargin{:});

Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
I have tried changing the dimensions of the equations and the x0 maxtrices but no combination seems to work.
Also, if you could help me write this in a more pleasing to the eye format, that would also be great.
Thank you!
  1 个评论
Stephen23
Stephen23 2023-10-6
移动:Stephen23 2023-10-10
f = @(x) [0.05*F*x(1) - x(5)*x(1)*F;
0.95*F*x(4) - (1-x(5))*x(4)*F;
(y_ar)+(x(2) - x(4))+(x(1) - x(3)) - ...
(((10^(A_np - (B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np - (B_np/(x(6)+C_np))))-1))) + ...
(((10^(A_nh - (B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh - (B_nh/(x(6)+C_nh)))-1)));
x(2)/x(4) - 10^(A_np - (B_np/(x(6)+C_np)));
x(1)/x(3) - 10^(A_nh - (B_nh/(x(6)+C_nh)));
y_ar + x(1) + x(2);
x(3) + x(4)]
f = function_handle with value:
@(x)[0.05*F*x(1)-x(5)*x(1)*F;0.95*F*x(4)-(1-x(5))*x(4)*F;(y_ar)+(x(2)-x(4))+(x(1)-x(3))-(((10^(A_np-(B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np-(B_np/(x(6)+C_np))))-1)))+(((10^(A_nh-(B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh-(B_nh/(x(6)+C_nh)))-1)));x(2)/x(4)-10^(A_np-(B_np/(x(6)+C_np)));x(1)/x(3)-10^(A_nh-(B_nh/(x(6)+C_nh)));y_ar+x(1)+x(2);x(3)+x(4)]

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-10-6
f = @(x) [0.05*F*x(1) - x(5)*x(1)*F;
0.95*F*x(4) - (1-x(5))*x(4)*F;
(y_ar)+(x(2) - x(4))+(x(1) - x(3)) - ...
(((10^(A_np - (B_np/(x(6)+C_np))))-1)*z_np/((1+x(5))*((10^(A_np - (B_np/(x(6)+C_np))))-1)))...
+(((10^(A_nh - (B_nh/(x(6)+C_nh))))-1)*z_nh/((1+x(5))*(10^(A_nh - (B_nh/(x(6)+C_nh)))-1)));
x(2)/x(4) - 10^(A_np - (B_np/(x(6)+C_np)));
x(1)/x(3) - 10^(A_nh - (B_nh/(x(6)+C_nh)));
y_ar + x(1) + x(2);
x(3) + x(4)];
You have a [] list constructor, and inside of it you have a ... line continuation.
Historically, when there was a ... line continuation, it used to be the case that the ... to end of line was removed, and (for parsing purposes) the next line was pulled up to continue from exactly where the ... started. Starting a relatively small number of releases ago, instead when you have a ... line continuation, for parsing purposes the ... to end of line is logically replaced by a single space and the next line is logically pulled up to continue from where the ... started.
Historic precedent:
[A...
B]
would have been logically transformed to
[AB]
but now it would be logically transformed to
[A B]
But in your case the difference between historical and current practice is irrelevant because the line being pulled up starts with blanks. Leading blanks are not removed from what is pulled up.
Historical:
[A...
B]
would have become
[A B] %because second line started with two blanks
and the modern version would have it instead become
[A B] %... gets substituted with one blank and then the two from the start of the next line
So either way, historic or modern, you have a construct
[expression1...
+expression2
]
that is being logically converted to
[expression1 +expression2]
with spaces after the first expression, then a + and then immediately a second expression.
Now think for a second about the meaning of
[-1 +5
+5 -2]
Is it likely that the user intended that to be equivalent to adding 5 to -1 on the first row, and adding -2 to 5 on the second row? Probably not. The user probably intended a 2 x 2 array of values.
And so it is... when you have your [expression1 +expression2] that is going to be interpreted as two different entries on the same row, not as addition.
This is an example of the uplus, + unary plus operator, which is a mirror to the more common uminus, - unary minus operator. Inside a list, [A +B] is treated as [(A), (+B)] not as [(A+B)] .
If you want addition inside a [] or {} operator, then either have no spaces between the terms, like -1+5 or else have at least one space after the operator, such as [-1+ 5] or [-1 + 5] .

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by