Error: Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.

3 次查看(过去 30 天)
I am getting this error after implemnting my BCs:
clc; clear all;
N=30;
% compute the chebyshev differentiation matrix and x-grid
Ly =3;
eta_ygl = 2/Ly; etagl = -cos(pi*(0:N)/N)'; ygl = (etagl+1)/eta_ygl;
%ygl = -cos(pi*(0:N)/N)'; %Gauss-Lobatto Points
VGL = cos(acos(ygl(:))*(0:N));%C0 operator from thesis
dVGL = diag(1./sqrt(1-ygl.^2))*sin(acos(ygl)*(0:N))*diag(0:N);
dVGL(1,:) = (-1).^(1:N+1).*(0:N).^2;
dVGL(N+1,:) = (0:N).^2;
% Differentiation Matrices for Gauss & GaussLobatto
Dgl = dVGL/VGL;
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.696637e-34.
D = Dgl;
x=ygl;
% compute the derivatives at x (i.e. at the chebyshev grid points)
A=D*eta_ygl;
R = 2*x.*exp(x.^2 - 1);
% boundary condition
A(end,:) = 0;
A(end,end) = 1;
R(end) = exp(8); %u(Ly)
%LHS
A(1,:) = 0;
A(1,1) = 1;
R(1) = exp(-1); %u(0)
% solve
u = A\R;
plot(x,u,'--rs',x,exp(x.^2-1))
legend('Chebyshev','Exact')
The output is okay as shown in the plot, however I am not sure why I get the error and if there's a way to fix this? if I change the BCs I can get rid of the error but then the solution isn't right. Thanks!

回答(2 个)

Walter Roberson
Walter Roberson 2023-10-4
% Differentiation Matrices for Gauss & GaussLobatto
Dgl = dVGL/VGL;
In MATLAB the / operator is mrdivide, / and is approximately equivalent to dVGL * pinv(VGL) -- which attempts to find linear models relating combinations of columns.
You probably want the ./ operator rdivide, ./
Or perhaps you should be looking at gradient
If you really do mean matrix division... you have the problem that rank(VGL) is only 8 for a 31 x 31 matrix...
  1 个评论
Walter Roberson
Walter Roberson 2023-10-4
If you proceed at higher precision then the actual rank of VGL is only 3.
Q = @(v) sym(v);
N=30;
% compute the chebyshev differentiation matrix and x-grid
Ly = Q(3);
eta_ygl = Q(2)/Ly;
etagl = -cospi(Q(0:N)/N)';
ygl = (etagl+1)/eta_ygl;
VGL = cos(acos(ygl(:))*(0:N));%C0 operator from thesis
dVGL = diag(1./sqrt(1-ygl.^2))*sin(acos(ygl)*(0:N))*diag(0:N);
dVGL(1,:) = (-Q(1)).^(1:N+1).*(0:N).^2;
dVGL(N+1,:) = Q(0:N).^2;
% Differentiation Matrices for Gauss & GaussLobatto
rank(VGL)
ans = 3
%Dgl = dVGL/VGL;

请先登录,再进行评论。


Bruno Luong
Bruno Luong 2023-10-4
编辑:Bruno Luong 2023-10-4
N=30;
% compute the chebyshev differentiation matrix and x-grid
Ly =3;
eta_ygl = 2/Ly; etagl = -cos(pi*(0:N)/N)'; ygl = (etagl+1)/eta_ygl;
%ygl = -cos(pi*(0:N)/N)'; %Gauss-Lobatto Points
VGL = cos(acos(ygl(:))*(0:N));%C0 operator from thesis
I don't know what the "thesis" is about but I notice with the parameter you set (Ly=3) it is odd that you compute acos of ygl that goes up to 3 and not in the domain [-1,1]; so acos function returns a complex value. Then the imaginary part get large when you multiply by N. Then cos of the complex value behaves like cosh or more or less like an exponential function exp. You matrix VGL has huge dynamic range, and become totally ill-condition because pof this huge dynamic range. The warning about
Dgl = dVGL/VGL
is due to that. I guess your Gauss Labato somehow still working but as MATLAB warning warn 'The result is innacurate".
I guess the setup of Ly=3 is wrong or the way you use it is wrong.
Usually the Gauss Labato points coordinates are derived for [0,1] interval then scale it with mesh size. The mesh size should not be used i argument.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by