Borrar filtros
Borrar filtros

Question about constraints in optimization problems

1 visualización (últimos 30 días)
Rahim Rahim
Rahim Rahim el 29 de Nov. de 2020
Comentada: Walter Roberson el 29 de Nov. de 2020
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

Respuestas (1)

Ameer Hamza
Ameer Hamza el 29 de Nov. de 2020
Editada: Ameer Hamza el 29 de Nov. de 2020
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 comentarios
Rahim Rahim
Rahim Rahim el 29 de Nov. de 2020
Editada: Rahim Rahim el 29 de Nov. de 2020
@Walter Roberson thank you for your comment
Any part should I see ?
Walter Roberson
Walter Roberson el 29 de Nov. de 2020
All of the section on the topic.

Iniciar sesión para comentar.

Categorías

Más información sobre Nonlinear Optimization en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by