% MICROCRE teaching simulation: Angle-to-displacement ray geometry % 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: δ=L tanθ;δ_small≈Lθ;θ[rad]=θ[arcsec]π/(180×3600) % Assumption: 单程平面射线几何,无折射和镜头畸变;401个扫描角。 % Limitation: 不能把单程Ltanθ直接当成反射自准直整机标定式。 % Limitation: 不包括空气折射、支架漂移和双轴串扰。 % Source: https://www.nist.gov/publications/simple-method-high-precision-calibration-angle-encoder-using-electronic-nulling % This is not product performance, calibration, or compliance evidence. clear; close all; clc; p.length = 500; % 传播距离 [mm] p.angle = 1000; % 扫描半角 [arcsec] assert(isfinite(p.length) && p.length>=0.001 && p.length<=1000000.0, 'Invalid length'); assert(isfinite(p.angle) && p.angle>=0.001 && p.angle<=100000, 'Invalid angle'); theta=linspace(-p.angle,p.angle,401); rad=theta*pi/(180*3600); exact=1000*p.length*tan(rad); linear=1000*p.length*rad; lines=[s('Ray geometry: tan(theta)',theta,exact,'arcsec','um'),s('Small-angle approximation',theta,linear,'arcsec','um')]; out=struct('max_displacement',exact(end),'small_angle_error',abs(exact(end)-linear(end)),'relative_error',100*abs((exact(end)-linear(end))/exact(end))); outdir=fullfile(pwd,'optical-angle_results'); if ~exist(outdir,'dir'),mkdir(outdir);end fid=fopen(fullfile(outdir,'optical-angle.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('Angle-to-displacement ray geometry','Interpreter','none'); print(fig,fullfile(outdir,'optical-angle.png'),'-dpng','-r160'); fid=fopen(fullfile(outdir,'optical-angle-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