How to avoid error calculation going to infinity when dividing by near-zero values?

21 次查看(过去 30 天)
Hello,
I am calculating the relative error between two signals using the next equation:
relative_error(i) = abs((measured(i)-estimated(i))./measured(i))*100;
The problem arises when measured is zero or near zero that relative_error explodes.
Do you know a more appropriated way of calculating an error between two signals that avoids this problem?
Thanks,
Cerilet

回答(1 个)

Honglei Chen
Honglei Chen 2017-10-24
There are several ways to handle this, for example, you can add one more line to set all those points to a predefined value
relative_error(i) = abs((measured(i)-estimated(i))./measured(i))*100;
relative_error(measured==0) = 1; % say predefined value is 1
Or just add an epsilon to avoid this issue
epsilon = 1e-8;
relative_error(i) = abs((measured(i)-estimated(i))./(measured(i)+epsilon))*100;
HTH

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by