Question about constraints in optimization problems

2 次查看(过去 30 天)
I have the following victor:
A=[ 0.1 0.1 0.3 0.2 0.3]
The sum of this vector is 1, but when I try the following code:
Test = (sum(A(:)) ==1)
If ( Test == 1 )
"True"
Else
"False"
The results is always FALSE.
What is the problem

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-11-29
编辑:Ameer Hamza 2020-11-29
This is due to the finite precision of floating-point numbers. Calculations on floating points numbers can accumulate errors, so the value is not exactly equal to one. You need to allow a bit of tolerance in comparing floating-point values. For example
Test = (sum(A(:)) ==1)
if ( abs(Test-1) < 1e-6 )
"True"
else
"False"
end
  3 个评论
Rahim Rahim
Rahim Rahim 2020-11-29
编辑:Rahim Rahim 2020-11-29
@Walter Roberson thank you for your comment
Any part should I see ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by