如何在MATLAB中计算机器epsilon? [英] How to calculate machine epsilon in MATLAB?

查看:699
本文介绍了如何在MATLAB中计算机器epsilon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到机器epsilon,并且正在执行以下操作:

I need to find the machine epsilon and I am doing the following:

eps = 1;

while 1.0 + eps > 1.0 do
    eps = eps /2;
end

但是,它向我展示了这一点:

However, it shows me this:

Undefined function or variable 'do'. 
Error in epsilon (line 3) 
while 1.0 + eps > 1.0 do

我该怎么办?

推荐答案

首先,在MATLAB中没有do关键字,因此请从代码中消除.另外,请勿将 eps 用作实际变量.这是MATLAB中的预定义函数,可计算机器epsilon ,这也是您正在尝试的计算.通过创建一个名为eps的变量,您会遮盖实际的函数,因此MATLAB中需要使用该函数的任何其他函数的行为都将出乎意料,这不是您想要的.

First and foremost, there is no such thing as a do keyword in MATLAB, so eliminate that from your code. Also, don't use eps as an actual variable. This is a pre-defined function in MATLAB that calculates machine epsilon, which is also what you are trying to calculate. By creating a variable called eps, you would shadow over the actual function, and so any other functions in MATLAB that require its use will behave unexpectedly, and that's not what you want.

请改用其他内容,例如macheps.另外,您的算法略有错误.您需要在while循环中检查1.0 + (macheps/2),而不是1.0 + macheps.

Use something else instead, like macheps. Also, your algorithm is slightly incorrect. You need to check for 1.0 + (macheps/2) in your while loop, not 1.0 + macheps.

换句话说,执行此操作:

In other words, do this:

macheps = 1;

while 1.0 + (macheps/2) > 1.0
    macheps = macheps / 2;
end

这应该给您2.22 x 10^{-16},如果您在命令提示符下键入eps,则与MATLAB一致.要仔细检查:

This should give you 2.22 x 10^{-16}, which agrees with MATLAB if you type in eps in the command prompt. To double-check:

>> format long
>> macheps

macheps =

     2.220446049250313e-16

>> eps

ans =

     2.220446049250313e-16


奖金

如果您不知道,由于浮点运算,机器epsilon是相对误差的上限.换句话说,由于用于存储浮点数的位数有限,这将是真实的浮点数与在计算机上计算出的最大期望差值.


Bonus

In case you didn't know, machine epsilon is the upper bound on the relative error due to floating point arithmetic. In other words, this would be the maximum difference expected between a true floating point number and one that is calculated on a computer due to the finite number of bits used to store a floating point number.

如果您还记得的话,浮点数将不可避免地以二进制位(或几乎任何数字形式)表示为计算机上的二进制位.根据 IEEE 754浮点标准,MATLAB假定所有数值均为,将浮点数表示为64位.您显然可以通过显式强制转换为另一种类型来覆盖此行为.使用IEEE 754浮点标准,对于double精度类型数字,有52位代表数字的分数部分.

If you recall, floating numbers inevitably are represented as binary bits on your computer (or pretty much anything digital). In terms of the IEEE 754 floating point standard, MATLAB assumes that all numerical values are of type double, which represents floating point numbers as 64-bits. You can obviously override this behaviour by explicitly casting to another type. With the IEEE 754 floating point standard, for double precision type numbers, there are 52 bits that represent the fractional part of the number.

下面是我正在谈论的一个很好的图表:

Here's a nice diagram of what I am talking about:

来源:维基百科

您会看到为数字的符号保留了一位,为指数底数保留了11位,最后为小数部分保留了52位.总共加起来为64位.小数部分是基数2的集合或总和,负指数从-1到-52.浮点数的MSB以2^{-1}开头,一直向下到2^{-52}作为LSB.本质上,机器epsilon会计算两个数字之间的二进制增加1位的最大分辨率差异,前提是它们具有 same 符号和 same 指数基.从技术上讲,机器epsilon实际上等于2^{-52},因为考虑到我先前提到的那些条件,这是浮点中单个位的最大分辨率.

You see that there is one bit reserved for the sign of the number, 11 bits that are reserved for exponent base and finally, 52 bits are reserved for the fractional part. This in total adds up to 64 bits. The fractional part is a collection or summation of numbers of base 2, with negative exponents starting from -1 down to -52. The MSB of the floating point number starts with2^{-1}, all the way down to 2^{-52} as the LSB. Essentially, machine epsilon calculates the maximum resolution difference for an increase of 1 bit in binary between two numbers, given that they have the same sign and the same exponent base. Technically speaking, machine epsilon actually equals to 2^{-52} as this is the maximum resolution of a single bit in floating point, given those conditions that I talked about earlier.

如果您实际上仔细查看上面的代码,则在每次迭代中,除以2的位就是将数字右移一位,即从1的整个值开始,即2^{0},然后将此数字加到1.我们继续进行位移,并通过将位移后的值加1来查看该值等于什么,然后向上移动直到位移到该点为止在右边,更改不再被注册.如果再向右移一位,由于下溢,该值将变为 0 ,以1.0 + 0.0 = 1.0表示,因此不再是> 1.0,这是while循环正在检查.

If you actually look at the code above closely, the division by 2 is bit-shifting your number to the right by one position at each iteration, starting at the whole value of 1, or 2^{0}, and we take this number and add this to 1. We keep bit-shifting, and seeing what the value is equal to by adding this bit-shifted value by 1, and we go up until the point where when we bit shift to the right, a change is no longer registered. If you bit-shift any more to the right, the value will become 0 due to underflow, and so 1.0 + 0.0 = 1.0, and this is no longer > 1.0, which is what the while loop is checking.

一旦退出while循环,此阈值将定义机器epsilon.如果您很好奇,如果在命令提示符下打入2^{-52},您将得到eps等于:

Once the while loop quits, it is this threshold that defines machine epsilon. If you're curious, if you punch in 2^{-52} in the command prompt, you will get what eps is equal to:

>> 2^-52

ans =

     2.220446049250313e-16

这很有意义,因为您向右移了52次,并且循环停止之前的点将位于其LSB,即2^{-52}.为了完整起见,如果要在while循环中放置一个计数器,并计算while循环执行了多少次,它将精确地执行52次,表示向右移动52位:

This makes sense as you are shifting one bit to the right 52 times, and the point before the loop stops would be at its LSB, which is 2^{-52}. For the sake of being complete, if you were to place a counter inside your while loop, and count up how many times the while loop executes, it will execute exactly 52 times, representing 52 bit shifts to the right:

macheps = 1;
count = 0;
while 1.0 + (macheps/2) > 1.0
    macheps = macheps / 2;
    count = count + 1;
end

>> count

count =

  52

这篇关于如何在MATLAB中计算机器epsilon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆