NaN during function Optimization

7 views (last 30 days)
Rafelox
Rafelox on 29 Sep 2018
Answered: Walter Roberson on 29 Sep 2018
Hello everyone. I wrote 3 heuristic algorithms by myself to calculate the minimum of a fitness function that arises from the optimization of a space trajectory. The problem here is that with 2 of these algorithms i can easily find the minumum, but, when it comes to the third algorithm, a strange error occurs. After the random initialization of the particles, the fitness function gets evaluated (it is an integral function) by calling a function that contains the equation of motion and then ODE45. For a certain particle the Fitness function is a NaN. The weird thing is that if I evaluate the function manually, during the debug process, by copying and pasting the components of the particle, the fitness function has a real finite value! I can't figure out how to solve this issue. Any help would be very appreciated. Thanks

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2018
When you copy and paste numbers, you are never getting the complete precision of the numbers. Even "format long g" does not give you the last digit of the number. You would need to display the numbers with a longer format, such as num2str with a %.17g format. "format long g" can differ in the last one or two bits from what you would get from %.17g .
See also num2hex() to check to see the exact representation of numbers, so that you can check whether the number you transferred is exactly the same in both cases.
For example,
>> q(2)
ans =
63782.4529892424
>> num2hex(q(2))
ans =
'40ef24ce7ee34bb5'
>> num2hex(63782.4529892424)
ans =
'40ef24ce7ee34bb2'
>> num2str(q(2),'%.17g')
ans =
'63782.452989242425'
>> num2hex(63782.452989242425)
ans =
'40ef24ce7ee34bb5'

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!