How do I make Matlab solve for a variable array?

7 次查看(过去 30 天)
I'm doing a project for my thermodynamics class, and it requires me to plot the volume of methane over the range of 300 to 1000 kelvin. For the ideal gas equation, the process was easy, but for the equations of state that cannot easily be rebalanced to have Volume on one side and the other variables on the other, MATLAB refuses to do anything resembling solving for V so I could plot it.
If anyone could give me any tips, it would be greatly appreciated. I'm at my wits end with this.
clear V
clear T
syms V
R = 8314000;
P = 20000000;
T = (300:1000);
Pc = 4599000;
Tc = 190.6;
a = .45724 .* ((R^2 .* Tc^2)/Pc);
b = .07780 .* ((R .* Tc)/Pc);
w = .012;
k = .37464+(1.54226 .* w)-(.26992 .* w^2);
Tr = T ./ Tc;
al = (1+k .* (1-(Tr).^(1/2))).^2;
S = (P == ((R .* T)./(V-b))-((a .* al)./((V.^2)+(2 .* b .* V)-b.^2)));
Volume = vpasolve(S);
Volume
plot(T,Volume)

回答(1 个)

Star Strider
Star Strider 2021-11-17
The symbolic solver had problems with it, however fsolve sort of lucked out.
Try this numeric approach —
sympref('AbbreviateOutput',false);
R = 8314000;
P = 20000000;
% T = (300:1000);
Pc = 4599000;
Tc = 190.6;
a = .45724 .* ((R^2 .* Tc^2)/Pc);
b = .07780 .* ((R .* Tc)/Pc);
w = .012;
k = .37464+(1.54226 .* w)-(.26992 .* w^2);
syms T V
Tr = T ./ Tc;
al = (1+k .* (1-(Tr).^(1/2))).^2;
S = (P - ((R .* T)./(V-b))-((a .* al)./((V.^2)+(2 .* b .* V)-b.^2)))
S = 
Sfcn = matlabFunction(S)
Sfcn = function_handle with value:
@(T,V)((sqrt(T.*(5.0./9.53e+2)).*3.9310825152e-1-1.39310825152).^2.*(-2.49658662453555e+11))./(V.*5.361410981517721e+1+V.^2-7.186181928184702e+2)-(T.*8.314e+6)./(V-2.68070549075886e+1)+2.0e+7
% Volume = solve(S,V)
% Volume = vpa(Volume)
% Volume
T = (300:10:1000);
for k = 1:numel(T)
V0(k) = randi([500 1000]);
V(k) = fsolve(@(V)Sfcn(T(k),V), randi(1000));
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared and the vector of function values is near zero as measured by the value of the function tolerance. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Solutions = [double(T); double(V); V0]
Solutions = 3×71
300.0000 310.0000 320.0000 330.0000 340.0000 350.0000 360.0000 370.0000 380.0000 390.0000 400.0000 410.0000 420.0000 430.0000 440.0000 450.0000 460.0000 470.0000 480.0000 490.0000 500.0000 510.0000 520.0000 530.0000 540.0000 550.0000 560.0000 570.0000 580.0000 590.0000 187.9722 191.0945 194.2499 197.4370 200.6548 203.9021 207.1779 210.4811 213.8108 217.1660 220.5459 223.9495 227.3761 230.8249 234.2951 237.7859 241.2967 244.8268 248.3755 251.9422 255.5264 259.1273 262.7444 266.3773 270.0254 273.6881 277.3651 281.0557 284.7597 288.4765 759.0000 750.0000 545.0000 856.0000 987.0000 897.0000 640.0000 863.0000 681.0000 905.0000 742.0000 677.0000 570.0000 805.0000 587.0000 991.0000 721.0000 816.0000 611.0000 923.0000 582.0000 620.0000 817.0000 900.0000 568.0000 555.0000 667.0000 919.0000 732.0000 741.0000
figure
plot(T,V)
grid
xlabel('T (°K)')
ylabel('Volume (m^3)')
Experiment if necessary to get different results. The original symbolic ‘S’ was changed slightly (the == became -) in order to get it to play well with matlabFunction. (I have no idea what the ‘Volume’ units are, so I guessed.)
.
  2 个评论
Spencer Culotta
Spencer Culotta 2021-11-17
So I've been working with my partner on this, and he actually found some code using symbolic notation in order to solve for other equations of state, and when i used it it also worked for the Van der waals equation. However, for this specific equation, it doesn't want to cooperate, but it gets so close to being there that I feel like there's something small I'm overlooking.
sol should be of size 1x1, but it always manages to be 2x1 somehow and I don't know how to fix that.
In the exact words of MATLAB, "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 2-by-1."
clear R V v T Tr Tc t P Pc a b w k al i eqn sol
R = 83.14;
P = 200;
T = (300:1000);
Pc = 45.99;
Tc = 190.6;
a = .45724*((R^2*Tc^2)/Pc);
b = .07780*((R*Tc)/Pc);
w = .012;
k = .37464+(1.54226*w)-(.26992*w^2);
i = 1;
V = zeros(1,701);
for t = (300:1000)
syms v
assume (v,'real');
assume (v,'positive');
al = (1+k*(1-(t/Tc).^(1/2))).^2;
eqn = ((R*t)/(v-b))-((a*al)/((v^2)+(2*b*v)-b^2))-P == 0;
sol = double(vpasolve(eqn,v,[1,1e6]));
V(1,i) = sol;
i = i+1;
end
plot(T, V)
Star Strider
Star Strider 2021-11-17
In the original post, ‘S’ is a complicated expression. It apparently does not have an analytic solution, so I chose a numeric option. That at least produced data, although given the original nature of ‘S’, while I trust its accuracy, I’m not certain how appropriate it would be.
Look carefully to be certain ‘S’ is coded correctly. If so, it simply may not have an analytic solution.
That¹s the reason numeric methods were developed.
.

请先登录,再进行评论。

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by