This Mathematica notebook solves for the optimal interstage pressures in a multi-stage gas compressor when the cooling between stages is unequal (i.e., each stage’s gas is cooled back to a different temperature rather than a uniform inter-cooling temperature).​A 5-stage compressor (n = 5) compresses gas from a suction pressure P1 = 1.0 bar to a final discharge pressure Pn = 8.0 bar, with gas properties γ = 1.4 and R = 0.287 kJ/(kg·K).​Unequal cooling model: Instead of returning to the same inlet temperature at each stage, the suction temperature rises stage by stage (300 K → 305 K → 313 K → 325 K → 345 K), reflecting imperfect/unequal inter-cooling.​Objective function: A symbolic expression for total compression work (WtotalSymbolic) is built as a function of the three intermediate pressures (P2, P3, P4,P5), since P1 and P6 (=Pn) are fixed.​Optimization via PSO: A custom Particle Swarm Optimization routine (psoOptimize) is implemented (with velocity clamping and vector-bound clipping) to minimize total work. It converges over 150 iterations to a minimum total work of ≈199.004 kJ/kg, with optimal intermediate pressures {1.8423, 3.2033, 5.08719, 7.08225} bar.​Verification via NMinimize: The same optimization is cross-checked using Mathematica’s built-in NMinimize with the Differential Evolution method, yielding an identical result.​Key insight: Unlike the classical equal-cooling case (where equal pressure ratios across stages minimize work), here the optimal pressure ratios are not equal across stages (1.8423, 1.73875, 1.58811, 1.39217, 1.12958), since unequal interstage cooling breaks that symmetry.​Convergence diagnostics: A log-scale plot tracks the gap between the running best PSO solution and the final converged loss, showing rapid initial convergence followed by fine-tuning down to numerical precision (~10⁻¹²).​The interstage cooling is unequal, so the suction temperature rises monotonically from stage to stage (300, 305, 313, 325, 345 K), and the same optimality condition now forces
r
i
to decrease as
T
in,i
increases in order to keep the product
T
in,i
×
γ-1
γ
r
i
constant across all stages — which is exactly the pattern the optimizer finds (1.8423, 1.73875, 1.58811, 1.39217, 1.12958). Physically, this reflects the fact that compressing hotter gas by a given pressure ratio costs more work, so the optimal strategy shifts more of the pressure rise onto the earlier, cooler stages and less onto the later, hotter ones, which is why the pressure ratios are unequal and steadily decrease down the train.
Number of stages
n=5;
Suction pressure, e.g. bar
P1=1.0;
Final discharge pressure, e.g. bar
Pn=8.0;
Ratio of specific heats, cp/cv
gamma=1.4;
Specific gas constant, kJ/(kg K)
R=0.287;
In[]:=
T1=300.0;​​dT={5.0,8.0,12.0,20};​​Tin=Prepend[T1+Accumulate[dT],T1];​​​​Print["Stage suction temperatures (K): ",Tin];
Stage suction temperatures (K): {300.,305.,313.,325.,345.}

PSO Solution

General symbolic formula
WtotalSymbolic[{P2_,P3_,P4_,P5_}]:=Module[{r},r={P2/P1,P3/P2,P4/P3,P5/P4,Pn/P5};Sum[(gamma/(gamma-1))*R*Tin[[i]]*((r[[i]])^((gamma-1)/(gamma))-1),{i,1,5}]]
Clip each Column of a matrix against its own scalar bound
clipMatrix[mat_,lbv_,ubv_]:=Transpose[MapThread[Clip[#1,{#2,#3}]&,{Transpose[mat],lbv,ubv}]];​​​​clampMatrix[mat_,vmaxv_]:=Transpose[MapThread[Clip[#1,{-#2,#2}]&,{Transpose[mat],vmaxv}]];
PSO Optimizer
psoOptimize[lossFunc_,dim_,lb_,ub_,nParticles_:30,nIter_:100,wMax_:0.9,wMin_:0.4,c1_:1.42,c2_:1.42,vClampFrac_:0.2]:=Module[{particles,velocities,pbest,pbestScore,gbest,gbestScore,convergence={},r1,r2,scores,gbestIdx,vmax,wIter},vmax=vClampFrac*(ub-lb);​​particles=Transpose@Table[RandomReal[{lb[[i]],ub[[i]]},nParticles],{i,dim}];​​velocities=ConstantArray[0.,{nParticles,dim}];​​pbest=particles;​​pbestScore=lossFunc/@particles;​​gbestIdx=First@Ordering[pbestScore,1];​​gbestScore=pbestScore[[gbestIdx]];​​gbest=pbest[[gbestIdx]];​​Do[wIter=wMax-(wMax-wMin)*(j-1)/(nIter-1);​​r1=RandomReal[1,{nParticles,dim}];​​r2=RandomReal[1,{nParticles,dim}];​​velocities=wIter*velocities+c1*r1*(pbest-particles)+c2*r2*(ConstantArray[gbest,nParticles]-particles);​​velocities=clampMatrix[velocities,vmax];​​particles+=velocities;​​particles=clipMatrix[particles,lb,ub];​​scores=lossFunc/@particles;​​pbest=MapThread[If[#3<#2,#1,#4]&,{particles,pbestScore,scores,pbest}];​​pbestScore=MapThread[Min,{pbestScore,scores}];​​gbestIdx=First@Ordering[pbestScore,1];​​If[pbestScore[[gbestIdx]]<gbestScore,gbestScore=pbestScore[[gbestIdx]];​​gbest=pbest[[gbestIdx]];];​​AppendTo[convergence,gbestScore];​​If[j<10||Mod[j,10]==0,Print["PSO Iter ",j," Loss = ",gbestScore]];,{j,nIter}];​​{gbest,gbestScore,convergence}];
In[]:=
dim=4;
In[]:=
UB=10{1,1,1,1};​​LB=1{1,1,1,1};
{gbest,gbestScore,convergence}=psoOptimize[WtotalSymbolic,4,LB,UB,30,150];
PSO Iter 1 Loss = 208.3
PSO Iter 2 Loss = 205.565
PSO Iter 3 Loss = 202.063
PSO Iter 4 Loss = 202.063
PSO Iter 5 Loss = 200.79
PSO Iter 6 Loss = 200.14
PSO Iter 7 Loss = 200.013
PSO Iter 8 Loss = 200.013
PSO Iter 9 Loss = 200.013
PSO Iter 10 Loss = 200.013
PSO Iter 20 Loss = 199.045
PSO Iter 30 Loss = 199.014
PSO Iter 40 Loss = 199.014
PSO Iter 50 Loss = 199.005
PSO Iter 60 Loss = 199.004
PSO Iter 70 Loss = 199.004
PSO Iter 80 Loss = 199.004
PSO Iter 90 Loss = 199.004
PSO Iter 100 Loss = 199.004
PSO Iter 110 Loss = 199.004
PSO Iter 120 Loss = 199.004
PSO Iter 130 Loss = 199.004
PSO Iter 140 Loss = 199.004
PSO Iter 150 Loss = 199.004
Intermediate pressure for minimum Wtot
gbest
Out[]=
{1.8423,3.2033,5.08719,7.08225}
Minimum Wtot
gbestScore
Out[]=
199.004
All stage pressure ratios are equal for equal interstage cooling
{gbest[[1]]/P1,gbest[[2]]/gbest[[1]],gbest[[3]]/gbest[[2]],gbest[[4]]/gbest[[3]],Pn/gbest[[4]]}
Out[]=
{1.8423,1.73875,1.58811,1.39217,1.12958}
Gap to the best-known solution at each iteration
gap=convergence-gbestScore;
Convergence plot
ListLogPlotgap,

Out[]=

Numerical Solution using NMinimize

Pressure ratio
Pressure ratios are not equal for unequal interstage cooling

CITE THIS NOTEBOOK

Optimal inter-stage pressures for a 5‑stage compressor with unequal inter-stage cooling​
by Housam Binous​
Wolfram Community, STAFF PICKS, July 4, 2026
https://community.wolfram.com/groups/-/m/t/3744130