物理书里给了个间接测量物理量的不确定度的公式。。。。很复杂:
然后我觉得很不爽。。。。。。
然后因为刚好在研究matlab。。。顺手写了个mathlab函数来做。。。一开始写了好久。。。然后突然发现,作为解释性语言matlab有个很好的eval函数。。。然后用那个直接执行字符串命令就很容易写了。。。
matlab语法设计很好跟python差不多。。。。。比起mathematica容易学多了。
而且函数内默认是局部变量~真好。。。否则这个程序还得缓存变量本来的值。。。。。
用法很简单,uncertainty((x^2+y^2)^(1/3),[x,y],[4,3],[0.2,0.1]);其中第一个参数是待分析的函数,第二个是变量表,第三个是对应变量的均值,第四个是对应变量的不确定度。
还没研究出来对应的写成Mathematica怎么写TwT
%MATLAB b
%by phoeagon
function re = uncertainty( func , Var , Var_data, Err )
%UNCERTAINTY [ function_to_calculate , [list_of_variables] , [list of means of variables] , [list of uncertainty] ]
%
%[func Var Var_data Err]
f = func;
fpai = [];
fexp = ”;
under_root = 0; %expression under SQRT[]
set_value = [];
term = ”;
for loop = 1:length(Var)
fpai = [fpai,(diff(f,Var(loop))^2* Err(loop)^2) ];
u = char(Var(loop));
u = strcat(u,’=’);
u = strcat(u,num2str(Var_data(loop)));
u = strcat(u,’;’);
set_value = [set_value,u];
term = strcat(term,’ +(‘);
term = strcat(term,string(diff(f,Var(loop))^2* Err(loop)^2));
term = strcat(term,’)’);
end
% set the value of each variable
eval(set_value)
% uncertainty output
re = sqrt(eval(term))
end
Like this:
Like Loading...
Related