# How to solve this warning: Matrix is close to singular or badly scaled

2 views (last 30 days)
Atinesh Singh on 14 Nov 2016
Answered: Walter Roberson on 14 Nov 2016
I'm trying to implement a Evolutionary Strategy algorithm (an optimization algorithm). On increasing the size of the parameter nx from 3 onwards, I'm getting warning
nx = 3
Warning: Matrix is singular to working precision.
nx >= 4
Warning: Matrix is close to singular or badly scaled
At this line
alphar{i}(p,m) = alphar{i}(p,m) - 2*pi*(alphar{i}(p,m)/abs(alphar{i}(p,m)));
Complete Code is attached below

Walter Roberson on 14 Nov 2016
Change
[p,m] = find(abs(alphar{i}) > pi);
alphar{i}(p,m) = alphar{i}(p,m) - 2*pi*(alphar{i}(p,m)/abs(alphar{i}(p,m)));
to
ind = find(abs(alphar{i}) > pi);
alphar{i}(ind) = alphar{i}(ind) - 2*pi*(alphar{i}(ind) ./ abs(alphar{i}(ind)));
Your find is returning multiple locations. That results in a vector of p and a vector of m. When you access alphar{i} with two vectors as coordinates, the result is not the array accessed at just the individual locations (p(K), m(K)) for K = 1 : length(p) : the result is submatrices, exactly like if you had asked for
A([3, 8], [9, 2])
meaning to access the submatrix [A(3,9), A(3,2); A(8, 9), A(8,2)]
Even after you correct for that by using the linear indices instead of the subscript pairs, you have a matrix / a matrix, which is like Array * inv(Array) -- algebraic matrix division. But you want the locations to act independently, so you need the ./ operator not /

### Categories

Find more on Problem-Based Optimization Setup 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!

Translated by