% MICROCRE teaching simulation: Monte Carlo propagation with correlated inputs % NOT EXECUTED IN MATLAB/Octave in the authoring environment. % Python/SciPy counterpart has been run; see validation-report.json. % Standalone MATLAB R2016b+ script (local functions); no extra toolbox required. % Edit p below, run this file, inspect outputs and assumptions before use. % Formula: z₂,corr=ρz₁+√(1−ρ²)z₂;F=F₀+u_Fz₁;A=A₀+u_Az₂,corr;σ=F/A % Assumption: 明确假设联合正态分布与给定ρ;Park–Miller伪随机数+Box–Muller,固定种子可复现。 % Assumption: 分位数使用排序后的线性插值,概率位置为p(N−1)。 % Limitation: 输入分布必须有物理依据;正态面积可能产生非正值,工具发现后拒绝而不删样本。 % Limitation: 有限次数使分位数有抽样波动;未实现JCGM自适应容差停止。 % Limitation: 经验覆盖区间不是均值的置信区间;不构成认证不确定度预算。 % Source: https://www.bipm.org/en/doi/10.59161/jcgm101-2008 % Source: https://physics.nist.gov/cuu/Uncertainty/combination.html % This is not product performance, calibration, or compliance evidence. clear; close all; clc; p.force = 1000; % 力均值 [N] p.u_force = 2; % 力标准不确定度 [N] p.area = 10; % 面积均值 [mm²] p.u_area = 0.05; % 面积标准不确定度 [mm²] p.rho = 0; % 相关系数 [1] p.trials = 10000; % 模拟次数 [1] p.seed = 12345; % 固定种子 [1] assert(isfinite(p.force) && p.force>=0.001 && p.force<=1000000000.0, 'Invalid force'); assert(isfinite(p.u_force) && p.u_force>=0 && p.u_force<=1000000.0, 'Invalid u_force'); assert(isfinite(p.area) && p.area>=0.001 && p.area<=1000000.0, 'Invalid area'); assert(isfinite(p.u_area) && p.u_area>=0 && p.u_area<=100000.0, 'Invalid u_area'); assert(isfinite(p.rho) && p.rho>=-1 && p.rho<=1, 'Invalid rho'); assert(isfinite(p.trials) && p.trials>=100 && p.trials<=50000, 'Invalid trials'); assert(fix(p.trials)==p.trials, 'Integer required'); assert(isfinite(p.seed) && p.seed>=1 && p.seed<=2147483646, 'Invalid seed'); assert(fix(p.seed)==p.seed, 'Integer required'); n=p.trials; state=p.seed; z=zeros(n,2); for j=1:n state=mod(16807*state,2147483647); u1=state/2147483647; state=mod(16807*state,2147483647); u2=state/2147483647; rad=sqrt(-2*log(u1)); z(j,:)=[rad*cos(2*pi*u2),rad*sin(2*pi*u2)]; end f=p.force+p.u_force*z(:,1); a=p.area+p.u_area*(p.rho*z(:,1)+sqrt(1-p.rho^2)*z(:,2)); assert(all(a>0),'A sampled area is non-positive; revise the distribution.'); values=f./a; aa=p.u_force/p.area; bb=p.force*p.u_area/p.area^2; if max(values)==min(values),centers=mean(values);density=1;else edges=linspace(min(values),max(values),51); counts=histcounts(values,edges); centers=(edges(1:end-1)+edges(2:end))/2; density=counts/(n*(edges(2)-edges(1)));end lines=[s('Monte Carlo density',centers,density,'MPa','1/MPa'),s('Individual trials',1:n,values','trial','MPa')]; lines(2).plot=false; if max(values)==min(values),lines(1).name='Degenerate point mass';lines(1).yu='probability';end out=struct('mean',mean(values),'standard_deviation',std(values,0),'lower95',qlinear(values,0.025),'upper95',qlinear(values,0.975),'nominal',p.force/p.area,'first_order_uncertainty',sqrt((aa-bb)^2+2*(1-p.rho)*aa*bb),'trials',n); outdir=fullfile(pwd,'uncertainty-monte-carlo_results'); if ~exist(outdir,'dir'),mkdir(outdir);end fid=fopen(fullfile(outdir,'uncertainty-monte-carlo.csv'),'w'); assert(fid>=0,'Cannot open CSV'); fprintf(fid,'series,x,y,x_unit,y_unit\n'); for j=1:numel(lines) for k=1:numel(lines(j).x) fprintf(fid,'"%s",%.17g,%.17g,"%s","%s"\n',lines(j).name,lines(j).x(k),lines(j).y(k),lines(j).xu,lines(j).yu); end end fclose(fid); fig=figure('Color','w');hold on; for j=1:numel(lines) if lines(j).plot,plot(lines(j).x,lines(j).y,'LineWidth',1.5,'DisplayName',lines(j).name);end end grid on;legend('show','Location','best');xlabel(lines(1).xu);ylabel(lines(1).yu); title('Monte Carlo propagation with correlated inputs','Interpreter','none'); print(fig,fullfile(outdir,'uncertainty-monte-carlo.png'),'-dpng','-r160'); fid=fopen(fullfile(outdir,'uncertainty-monte-carlo-summary.json'),'w');fprintf(fid,'%s',jsonencode(struct('inputs',p,'outputs',out,'notice','Teaching model; not product validation.')));fclose(fid); disp(out); function a=s(name,x,y,xu,yu) a=struct('name',name,'x',x,'y',y,'xu',xu,'yu',yu,'plot',true); end function q=qlinear(values,p) v=sort(values); pos=p*(numel(v)-1); k=floor(pos)+1; f=pos-floor(pos); if k==numel(v),q=v(k);else,q=v(k)*(1-f)+v(k+1)*f;end end