How do I save the answers from a for loop that solved an equation symbolically?

36 次查看(过去 30 天)
I am trying to save the answers from a for loop in which I solved an equation symbolically.
T_c = 33.145; %crit temp of hydrogen (K)
P_c = 1.3e6; %crit pressure (Pa)
R = 8.314472; %universal gas constant (J/(mol*K)
a = (0.427487*(R^2)*(T_c^2.5))/P_c;
b = (0.08662*R*T_c)/P_c;
x = 0.000051473700293409386773440257598528; %V_m
for P = 2e6:2e6:70e6
syms T_g
eqn = ((8.314472*T_g)/(x-b)) - (a/(sqrt(T_g)*x*(x+b))) == P;
solx = solve(eqn,T_g,"Real",true);
solx = vpa(solx);
end
With this code, I only have the final answer.
I tried using the code below to save the answers from the for loop, but I could not save them and got the answer in the screenshot.
k=0;
for P = 2e6:2e6:70e6
k=k+1;
syms T_g
eqn = ((8.314472*T_g)/(x-b)) - (a/(sqrt(T_g)*x*(x+b))) == P;
solx = solve(eqn,T_g,"Real",true);
solx = vpa(solx);
T_g(:,k) = solx;
end
Can somebody show how to save the answers from a for loop?

回答(1 个)

Hassaan
Hassaan 2024-8-9,20:13
T_c = 33.145; % critical temperature of hydrogen (K)
P_c = 1.3e6; % critical pressure (Pa)
R = 8.314472; % universal gas constant (J/(mol*K))
a = (0.427487*(R^2)*(T_c^2.5))/P_c;
b = (0.08662*R*T_c)/P_c;
x = 0.000051473700293409386773440257598528; % V_m
% Preallocate a cell array to store the solutions
T_g_solutions = cell(1, 35);
k = 0;
for P = 2e6:2e6:70e6
k = k + 1;
syms T_g
eqn = ((8.314472*T_g)/(x-b)) - (a/(sqrt(T_g)*x*(x+b))) == P;
solx = solve(eqn, T_g, "Real", true);
solx = vpa(solx);
% Store the solution in the cell array
T_g_solutions{k} = solx;
end
% Now T_g_solutions contains the symbolic solutions for each pressure
T_g_solutions
T_g_solutions = 1x35 cell array
Columns 1 through 7 {[34.91961423273...]} {[40.85084631403...]} {[47.10283616829...]} {[53.61292670149...]} {[60.33079643031...]} {[67.21676758485...]} {[74.23975776148...]} Columns 8 through 14 {[81.37540390794...]} {[88.60451998793...]} {[95.91189116355...]} {[103.2853532664...]} {[110.7150969967...]} {[118.1931430909...]} {[125.7129454872...]} Columns 15 through 21 {[133.2690897439...]} {[140.8570623343...]} {[148.4730728503...]} {[156.1139159119...]} {[163.7768630682...]} {[171.4595775147...]} {[179.1600462953...]} Columns 22 through 28 {[186.8765260074...]} {[194.6074990105...]} {[202.3516378662...]} {[210.1077762750...]} {[217.8748851753...]} {[225.6520529714...]} {[233.4384690868...]} Columns 29 through 35 {[241.2334102084...]} {[249.0362287247...]} {[256.8463429606...]} {[264.6632288904...]} {[272.4864130767...]} {[280.3154666267...]} {[288.1499999999...]}
  3 个评论
Torsten
Torsten 2024-8-11,15:39
编辑:Torsten 2024-8-11,15:46
Since the "solve" command always returns exactly one value for "solx", you can use
T_c = 33.145; % critical temperature of hydrogen (K)
P_c = 1.3e6; % critical pressure (Pa)
R = 8.314472; % universal gas constant (J/(mol*K))
a = (0.427487*(R^2)*(T_c^2.5))/P_c;
b = (0.08662*R*T_c)/P_c;
x = 0.000051473700293409386773440257598528; % V_m
% Preallocate a cell array to store the solutions
T_g_solutions = zeros(1, 35);
k = 0;
for P = 2e6:2e6:70e6
k = k + 1;
syms T_g
eqn = ((8.314472*T_g)/(x-b)) - (a/(sqrt(T_g)*x*(x+b))) == P;
solx = solve(eqn, T_g, "Real", true);
solx = vpa(solx);
% Store the solution in the cell array
T_g_solutions(k) = solx(1);
end
% Now T_g_solutions contains the symbolic solutions for each pressure
plot(2e6:2e6:70e6,T_g_solutions)
grid on
But the code might not give the "correct" value of T if your equation has more than one real solution (I just chose the first one arbitrarily).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by