undefined variable problem using vpasolve
3 views (last 30 days)
Show older comments
Vincent Nent
on 2 Jul 2023
Commented: Vincent Nent
on 2 Jul 2023
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)
1 Comment
Accepted Answer
Shantanu Dixit
on 2 Jul 2023
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)
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox 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!