Error message: Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.078506e-18.

15 vues (au cours des 30 derniers jours)
Hello, I am trying to run this code, the values for D_g are going infinity at some points. Is there anything I can do?
th = 0:10:90;
lam = 3:0.01:7;
lp = 0.168; lc = 8.932;
ep = 1 - ((lam.^2.*lc)./(lp.^2*(lc - 1i.*lam)));
er = real(ep);
eim = imag(ep);
ng = sqrt(0.5*(sqrt((er.^2) + (eim.^2)) + er));
kg = sqrt(0.5*(sqrt((er.^2) + (eim.^2)) - er));
nkg = ng + 1i*kg;
dg = 0.01;
for z = 1:length(th)
for j = 1:numel(lam)
phig(z,j) = 2*pi.*(dg./lam(j)).*sqrt((ng(j) - 1i*kg(j) ).^2 - sind(th(z).^2));
kgx(z,j) = (2.*pi./lam(j)).*sqrt((ng(j) - 1i*kg(j) ).^2 - sind(th(z).^2));
Dg = [kgx(z,j)/nkg(j) -kgx(z,j)/nkg(j); nkg(j) -nkg(j)];
D_g = inv(Dg)
end
end

Réponses (2)

Animesh
Animesh le 30 Mai 2023
Hello! The code is likely giving infinite values for D_g because the Dg matrix is singular for certain values of ng and kg. In other words, the determinant of Dg is zero for these values, meaning that the inverse of Dg does not exist and D_g cannot be computed.
To avoid this issue, you can check whether the determinant of Dg is zero before attempting to compute the inverse. If the determinant is zero, you can skip the calculation and set D_g to NaN or some other appropriate value to indicate that it is undefined.

Abhijeet
Abhijeet le 30 Mai 2023
Hi,
The values of D_g is going to infinity at some points because of the fact that the determinant of the matrix `Dg` is equalt to or close to zero.
You can fix this by adding a conditional check before calculating `D_g`, to check if the determinant of the Dg matrix is zero/close to zero. If it is zero, assign `D_g` to NaN or some other suitable value that indicates/fixes the error.
Here is the modified code for your reference:
th = 0:10:90;
lam = 3:0.01:7;
lp = 0.168; lc = 8.932;
ep = 1 - ((lam.^2.*lc)./(lp.^2*(lc - 1i.*lam)));
er = real(ep);
eim = imag(ep);
ng = sqrt(0.5*(sqrt((er.^2) + (eim.^2)) + er));
kg = sqrt(0.5*(sqrt((er.^2) + (eim.^2)) - er));
nkg = ng + 1i*kg;
dg = 0.01;
for z = 1:length(th)
for j = 1:numel(lam)
phig(z,j) = 2*pi.*(dg./lam(j)).*sqrt((ng(j) - 1i*kg(j) ).^2 - sind(th(z).^2));
kgx(z,j) = (2.*pi./lam(j)).*sqrt((ng(j) - 1i*kg(j) ).^2 - sind(th(z).^2));
Dg = [kgx(z,j)/nkg(j) -kgx(z,j)/nkg(j); nkg(j) -nkg(j)];
if abs(det(Dg)) < 1e-6 % check for singularity
% handle or skip the calculation for this value of j
continue;
else
D_g = inv(Dg);
end
end
end
Alternatively, You can try adding a small constant to the diagonal of the matrix `Dg`. You can achieve this by adding a small value `epsilon` to the diagonal matrix.
This can be achieved by modifying the code like:
Dg = [kgx(z,j)/nkg(j) -kgx(z,j)/nkg(j); nkg(j) -nkg(j)];
epsilon = 1e-6; % small constant value
Dg = Dg + epsilon*eye(size(Dg)); % add small value to diagonal
D_g = inv(Dg);
Thanks !!

Community Treasure Hunt

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

Start Hunting!

Translated by