Reference to non-existent field although the field is determined
2 views (last 30 days)
Show older comments
raha ahmadi
on 12 Feb 2021
Commented: raha ahmadi
on 13 Feb 2021
Hi, I m working on a POS algorithm for optimizing an elemet of a matrix which is produced of multiplication of almost 1000 matrices.
I think my PSO code is correct but I don’t know why I get this error?
Could it be because of my CostFunction which is resulted from multiplication of 1000 matrices? I determine the filed of the structure but nothing happened?
Reference to non-existent field 'Position'.
Error in PSO (line 95)
+c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
Thanks in advance
Raha
2 Comments
Accepted Answer
Walter Roberson
on 13 Feb 2021
Edited: Walter Roberson
on 13 Feb 2021
GlobalBest=particle(i).Best;
You do that conditionally on some particle cost being found that is less than the initialized cost of infinity. However, that never comes true: your calculated costs for your initial population are all nan +/- nan*1i (complex nan).
Nth=1.4786e24;
You are working in ranges about 1e23 to 1e25.
g1=a1*(N-N0)-a2*(lambda-(lambdaB-a3*(N-Nth)))^2;
lambda-(lambdaB-a3*(N-Nth)) is about 750. Squaring that is on the order of 1E5. a2 is on the order of 1e19, so the product of the two is on the order of 1e24
gamma1=g1/2+1i*n1*omega/c;
That is the same order of magnitude, with a small (roughly pi) complex component
M1=[exp(gamma1*pitch/2),0;0,exp(-gamma1*pitch/2)]; % in environment 1
exp(gamma1*pitch/2) and exp(-gamma1*pitch/2) are the same but with different signs on the exponent. If gamma1 is about 1e24 and negative, exp(gamma1*pitch/2) would be roughly exp(-1e24) which would come out as 0, but exp(-gamm1*pitch/2) would be roughly exp(1e24) which would overflow to infinity. If gamma1 is about -1e24, then which of the two exp() is 0 and which is inf switches roles. Either way, one of the two is going to be infinite when you are working in that range. And that is going to poison all the rest of the calculations, leading to NaN results.
You have large values on the order of 1e24 hard-coded, you aren't going to get around those at all easy; no mere typing mistake. But to get finite values out of the expression, your pitch would have to down-scale the 1e24 into the range of no more than 700-ish.
Or, you would have to switch to the symbolic toolbox for that calculation. For the symbolic toolbox, the limit is roughly
>> vpa(exp(sym(7.4e8)))
ans =
4.0588813157826340060476320819064e321377916
which gets you an extra factor of 1e6 to play with.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!