MATLAB脚本生成算法中舍入错误的报告

Pet*_*one 1 algorithm matlab

我有兴趣使用或创建一个脚本来获取算法中的错误舍入报告.我希望脚本或类似的东西已经完成......我认为这对于数字电子系统设计是有用的,因为有时需要研究如何根据小数位数考虑的准确度误差.设计.该脚本可以使用3个元素,算法代码,输入和输出.此脚本将逐行显示算法代码的错误.它会使用roundn之类的命令修改算法代码,并比较输出的错误.我将错误定义为

Errorrounding = Output(without rounding) - Output round 
Run Code Online (Sandbox Code Playgroud)

例如,我有下一个算法

calculation1 = input*constan1 + constan2 %line 1 of the algorithm
output = exp(calculation1)               %line 2 of the algorithm
Run Code Online (Sandbox Code Playgroud)

'input'是n个元素的输入,vector和'output'是输出,'constan1'和'constan2'是常量.n是输入向量的元素数

所以,我会将我的算法放在脚本中,并以自动方式生成下一个算法:

input_round = roundn(input,-1*mdec)
calculation1 = input*constant1+constant2*ones(1,n)
calculation1_round = roundn(calculation1,-1*mdec)
output=exp(calculation1_round)
output_round= roundn(output,-1*mdec)
Run Code Online (Sandbox Code Playgroud)

其中mdec是要考虑的小数位数.最后,脚本提供下一条消息

The rounding error at line 1 is #Errorrounding_calculation1
Run Code Online (Sandbox Code Playgroud)

'#Errorrounding'将是下一个操作的结果Errorrounding_calculation1 = calculation1 - calculation1_round

The rounding error at line 2 is #Errorrounding_output
Run Code Online (Sandbox Code Playgroud)

其中'Errorrounding_output'将是下一个操作Errorrounding_output = output - output_round的结果

有没有人知道是否有类似的东西已经完成,或者Matlab提供了解决一些相关问题的解决方案?谢谢.

gno*_*ice 5

第一点:我建议阅读David Goldberg撰写的每个计算机科学家应该知道的关于浮点运算的内容.它应该阐明很多关于浮点计算的问题,这些问题将帮助您更好地理解您正在考虑的问题的复杂性.

观点二:我认为你正在考虑的问题是很多比你想象复杂得多.由于舍入精度降低,您对计算中引入的误差感兴趣.你没有意识到这些错误会通过你的计算传播.考虑你的例子:

output = input*C1 + C2
Run Code Online (Sandbox Code Playgroud)

如果三个操作数中的每一个都是双精度浮点数,则它们的精度都会有一些舍入误差.可以使用EPS函数找到此舍入误差的界限,该函数告诉您从一个双精度数到下一个最大精度数的距离.例如,表示的相对误差的界限input将是0.5*eps(input),或者在它与下一个最大的双精度数之间的中间.因此,我们可以估计三个操作数上的一些错误界限,如下所示:

err_input = 0.5.*eps(input);  %# Maximum round-off error for input
err_C1 = 0.5.*eps(C1);        %# Maximum round-off error for C1
err_C2 = 0.5.*eps(C2);        %# Maximum round-off error for C2
Run Code Online (Sandbox Code Playgroud)

请注意,这些错误可能是正面的,也可能是负面的,因为真实数字可能已向上或向下舍入,以将其表示为双精度值.现在,请注意当我们通过向操作数添加这些错误来舍入操作数之前的真实值时会发生什么,然后执行以下计算output:

output = (input+err_input)*(C1+err_C1) + C2+err_C2
%# ...and after reordering terms
output = input*C1 + C2 + err_input*C1 + err_C1*input + err_input*err_C1 + err_C2
%#       ^-----------^   ^-----------------------------------------------------^
%#             |                                   |
%#    rounded computation                      difference
Run Code Online (Sandbox Code Playgroud)

从中可以看出,在执行计算之前三个操作数的精确舍入可以改变我们得到的输出difference.此外,当output舍入该值以将其表示为双精度值时,将存在另一个舍入误差源.

因此,您可以看到它比您想要的更复杂,以便充分估计精确舍入引入的误差.