function fit=fit_catalog(data,paths) % Grouped OLS. No pooling of stack/module, pressure classes or catalogue epochs. fc=data.fuel_cell_module_candidates; groups={ 'H3_module',[1 2 4 5]; 'IE_module',[6 7 8]}; fit=struct; fit.fc=struct([]);fit.tank=struct([]); for k=1:size(groups,1) ids=groups{k,2}; x=[fc(ids).nominal_power_W]';y=[fc(ids).module_mass_kg]'; names=string({fc(ids).component_id})'; f=reg(x,y,groups{k,1},names,'mass_kg = a + b * P_W','lower_mass'); f.boundary='nominal module power and mass, verified installed net continuous boundary unavailable'; if isempty(fit.fc),fit.fc=f;else,fit.fc(end+1)=f;end T=table(names,x,y,'VariableNames',{'sample_id','P_nominal_W','module_mass_kg'}); writetable(T,fullfile(paths.inputs,['fit-' groups{k,1} '.csv']),'Encoding','UTF-8'); end tanks=data.ambient_compressed_cylinder_candidates; manufacturers=string({tanks.manufacturer}); families={'H3 Dynamics','Alsafe','CTS'}; for k=1:numel(families) ids=find(contains(lower(manufacturers),lower(families{k}))); if isempty(ids),continue;end x=[tanks(ids).empty_mass_kg]';y=[tanks(ids).listed_H2_mass_kg]';names=string({tanks(ids).component_id})'; f=reg(x,y,strrep(families{k},' ','_'),names,'H2_kg = a + b * empty_tank_kg','upper_hydrogen'); f.boundary='gross catalogue fill; unlisted regulator/support not included; no cryogenic use'; if isempty(fit.tank),fit.tank=f;else,fit.tank(end+1)=f;end writetable(table(names,x,y,'VariableNames',{'sample_id','empty_mass_kg','listed_H2_kg'}), ... fullfile(paths.inputs,['fit-tank-' f.family '.csv']),'Encoding','UTF-8'); end fit.method='OLS affine module mass / affine gross tank H2, grouped by family; 95% pointwise Student-t bands'; fit.fc_correlation_handling='1200W LV/HV share family: use lighter HV once for optimistic H3 scale; LV retained as excluded correlated variant'; fit.excluded='Doosan n=2 with mass ranges; MEYER n=1; stack-only; unspecified system; hybrid peak; derived ratios; catalogue scenario values'; fit.physical_guarantee=false; r23.write_json(fullfile(paths.run,'catalog-fits.json'),fit); draw_fits(fit,paths); end function f=reg(x,y,family,names,equation,direction) [x,I]=sort(x);y=y(I);names=names(I); X=[ones(size(x)),x];coeff=X\y; yhat=X*coeff; residual=y-yhat; dof=numel(x)-2;assert(dof>0,'Need residual degrees of freedom.'); sigma2=sum(residual.^2)/dof;covariance=sigma2*((X'*X)\eye(2)); tcrit=sqrt(dof*(1/betaincinv(0.05,dof/2,0.5)-1)); if strcmp(direction,'lower_mass'),shift=min(residual);else,shift=max(residual);end xgrid=linspace(min(x),max(x),180)'; Xg=[ones(size(xgrid)),xgrid];meanfit=Xg*coeff; se=sqrt(sum((Xg*covariance).*Xg,2)); ci=[meanfit-tcrit*se,meanfit+tcrit*se]; sePred=sqrt(se.^2+sigma2);pi95=[meanfit-tcrit*sePred,meanfit+tcrit*sePred]; coefCI=[coeff-tcrit*sqrt(diag(covariance)),coeff+tcrit*sqrt(diag(covariance))]; env=coeff(1)+shift+coeff(2)*xgrid; if strcmp(direction,'lower_mass'),cover=(coeff(1)+shift+coeff(2)*x<=y+1e-12);else,cover=(coeff(1)+shift+coeff(2)*x>=y-1e-12);end f=struct('family',family,'n',numel(x),'dof',dof,'x',x,'y',y,'sample_ids',names, ... 'domain',[min(x),max(x)],'equation',equation,'a',coeff(1),'b',coeff(2), ... 'covariance',covariance,'coefficient_CI95',coefCI, ... 'RMSE',sqrt(sigma2),'R_squared',1-sum(residual.^2)/sum((y-mean(y)).^2),'residuals',residual, ... 'xgrid',xgrid,'mean',meanfit,'mean_CI95',ci,'prediction_PI95',pi95, ... 'envelope_intercept_shift',shift,'envelope_a',coeff(1)+shift,'envelope_b',coeff(2), ... 'envelope_y',env,'sample_covered',cover,'coverage_count',sum(cover), ... 'maximum_relative_curve_change',max(abs(env-meanfit)./max(abs(meanfit),eps)), ... 'envelope_method','minimum intercept-only shift satisfying all sample inequalities; slope fixed exactly', ... 'band_scope','pointwise conditional OLS, not simultaneous or post-search 95% guarantee','boundary',''); end function draw_fits(fit,paths) for i=1:numel(fit.fc) f=fit.fc(i);fig=figure('Position',[100 100 1060 430]);tiledlayout(1,2,'Padding','compact'); nexttile;bands(f.xgrid,f.mean_CI95,f.prediction_PI95);hold on; plot(f.xgrid,f.mean,'b-');plot(f.xgrid,f.envelope_y,'r--');scatter(f.x,f.y,35,'k','filled'); xlabel('Nominal module power (W)');ylabel('Module mass (kg)');title(strrep(f.family,'_',' '));grid on; legend('95% prediction','95% mean confidence','OLS','Optimistic envelope','SKU data','Location','northwest'); nexttile;hold on; plot(f.xgrid,f.xgrid./f.mean,'b-');plot(f.xgrid,f.xgrid./f.envelope_y,'r--'); scatter(f.x,f.x./f.y,35,'k','filled');xlabel('Nominal module power (W)');ylabel('Nominal specific power (W/kg)');grid on; title('Rational saturation shape; incomplete installed boundary'); r23.save_figure(fig,paths,['fit-fc-' f.family]);close(fig); end for i=1:numel(fit.tank) f=fit.tank(i);fig=figure('Position',[100 100 1060 430]);tiledlayout(1,2,'Padding','compact'); nexttile;bands(f.xgrid,f.mean_CI95,f.prediction_PI95);hold on; plot(f.xgrid,f.mean,'b-');plot(f.xgrid,f.envelope_y,'r--');scatter(f.x,f.y,35,'k','filled'); xlabel('Empty catalogue tank mass (kg)');ylabel('Gross hydrogen capacity (kg)');title(strrep(f.family,'_',' '));grid on; legend('95% prediction','95% mean confidence','OLS','Optimistic envelope','SKU data','Location','northwest'); nexttile;hold on; lo=max(f.mean_CI95(:,1),0);hi=max(f.mean_CI95(:,2),0);plo=max(f.prediction_PI95(:,1),0);phi=max(f.prediction_PI95(:,2),0); bands(f.xgrid,[lo./(f.xgrid+lo),hi./(f.xgrid+hi)]*100,[plo./(f.xgrid+plo),phi./(f.xgrid+phi)]*100); plot(f.xgrid,100*f.mean./(f.xgrid+f.mean),'b-');plot(f.xgrid,100*f.envelope_y./(f.xgrid+f.envelope_y),'r--'); scatter(f.x,100*f.y./(f.x+f.y),35,'k','filled');grid on; xlabel('Empty catalogue tank mass (kg)');ylabel('H2 / (empty tank + H2) (%)');title('Explicit loaded-tank denominator; accessories separate'); r23.save_figure(fig,paths,['fit-tank-' f.family]);close(fig); end end function bands(x,ci,pi95) fill([x;flipud(x)],[pi95(:,1);flipud(pi95(:,2))],[0.93 0.90 0.82],'EdgeColor','none');hold on; fill([x;flipud(x)],[ci(:,1);flipud(ci(:,2))],[0.78 0.86 0.98],'EdgeColor','none'); end