필터 지우기
필터 지우기

Warning: Matrix is singular to working precision.

조회 수: 32 (최근 30일)
Kyle Greenwood
Kyle Greenwood 2020년 9월 16일
댓글: Kyle Greenwood 2020년 9월 17일
My full script :
x = -10:0.5:10;
y = x;
[X,Y] = meshgrid(x);
f1 = X.^2 - Y.^2;
f2 = X.*Y.*((X.^2 - Y.^2) / (X.^2 + Y.^2));
subplot(1,2,1), surf(X,Y,f1)
xlabel('x')
ylabel('y')
zlabel('z')
subplot(1,2,2), surf(X,Y,f2)
xlabel('x')
ylabel('y')
zlabel('z')
When I run the script the first plot works fine but the second plots nothing as all entries of f2 are NaN.
I understand that this error occurs when you attempt to take the inverse of a matrix that is singular, however I'm failing to see where the code is trying to take the inverse? Additionally these functions f1, f2 were given to me in an assignment, presumably functions that would not give this result when used correctly. With the requirements of "using a resolution of at least 0.5 between points, from -10 to 10." giving the meshgrid specifications. If someone can see why this doesn't work or a workaround it would be much appreciated!

채택된 답변

Arthur Roué
Arthur Roué 2020년 9월 16일
편집: Arthur Roué 2020년 9월 16일
The operator to inverse a matrix is inv.
If you want to divide your matrices element wise, you have to add a "." in front of "/"
% Before
f2 = X.*Y.*((X.^2 - Y.^2) / (X.^2 + Y.^2));
% After
f2 = X.*Y.*((X.^2 - Y.^2) ./ (X.^2 + Y.^2));
EDIT : correction thanks to @Stephen Cobeldick's comment
  댓글 수: 2
Stephen23
Stephen23 2020년 9월 16일
" The operator to inverse a matrix is backslash..."
The operator to invert a square matrix is inv. Backslash (actually mldivide) solves systems of equations.
"...inv(A)*b is equivalent to A\b."
mldivide and mrdivide are more efficient and accurate at solving systems of equations:
Kyle Greenwood
Kyle Greenwood 2020년 9월 17일
Seems so obvious now, thank you for solving my madness

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Detection에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by