undefined variable problem using vpasolve

3 次查看(过去 30 天)
So i need to solve a set of equations and wrote some code for that. Im not very confident in my matlab skills but i cant find the mistake in here. Im getting undefined variable error over and over again.
Heres the Code maybe someone here can help me.
% Parameter
A_1 = 8.23714;
A_2 = 8.19625;
B_1 = 1592.864;
B_2 = 1730.63;
C_1 = 226.184;
C_2 = 233.426;
c1 = 1.701;
c2 = 0.9425;
p = 1022.48; % [mbar]
T = 80; % [°C]
g1 = exp((c1.*((1-x1)).^2)./(((1-x1)+(c1/c2).*x1).^2));
g2 = exp((c2.*(x1).^2)./((x1+(c2/c1).*(1-x1)).^2));
p1 = 10^(A_1-(B_1./(T+C_1)));
p2 = 10^(A_2-(B_2./(T+C_2)));
syms x1
eqn = (g1.*p1.*x1)+(g2.*p2.*(1-x1))-p==0;
S = vpasolve(eqn,x1)

采纳的回答

Shantanu Dixit
Shantanu Dixit 2023-7-2
Hi Vincent,
The error you are encountering is due to the fact that you are using the symbolic variable x1 in the calculations before declaring it as a symbolic variable using the syms function.
To resolve it, you need to declare x1 as a symbolic variable before using it in the equations.
% Parameter
A_1 = 8.23714;
A_2 = 8.19625;
B_1 = 1592.864;
B_2 = 1730.63;
C_1 = 226.184;
C_2 = 233.426;
c1 = 1.701;
c2 = 0.9425;
p = 1022.48; % [mbar]
T = 80; % [°C]
% Declare x1 as a symbolic variable
syms x1
% Calculate g1 and g2
g1 = exp((c1.*((1-x1)).^2)./(((1-x1)+(c1/c2).*x1).^2));
g2 = exp((c2.*(x1).^2)./((x1+(c2/c1).*(1-x1)).^2));
% Calculate p1 and p2
p1 = 10^(A_1-(B_1./(T+C_1)));
p2 = 10^(A_2-(B_2./(T+C_2)));
% Define the equation
eqn = (g1.*p1.*x1) + (g2.*p2.*(1-x1)) - p == 0;
% Solve the equation symbolically
S = vpasolve(eqn, x1)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by