I was working on a code that will produce a scatter plot for the following equation, Z = sin(sqrt(X * X + Y * Y)) / (sqrt(X * X + Y * Y)). When writting it in matlab, I get to the last line of my code and I get this error "Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 7.112887e-37.". I changed the values in my linspace values thinking that was the issue but I got the same error.
Here is my code:
>> xg = linspace (-10,10,25);
>> [X,Y] = meshgrid (xg,xg);
>> Z = sin(sqrt(X * X + Y * Y)) / (sqrt(X * X + Y * Y + eps));
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 7.112887e-37.
I do know that in order for a surface plot to prodoce I need to use "surf(X,Y,Z)". I got stuck trying to find a work around for this message. Is it because of my linspace or is it with the formula I am trying to enter?

댓글 수: 1

For context, the function eps was added to the starting formula, to avoid the outcome of when X and Y both equal 0. The inital formula is Z = sin(sqrt(X * X + Y * Y)) / (sqrt(X * X + Y * Y)) .

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

 채택된 답변

Voss
Voss 2023년 1월 22일

0 개 추천

You probably mean to use element-wise multiplication and division, in order to get a matrix Z the same size as X and Y:
Z = sin(sqrt(X .* X + Y .* Y)) ./ (sqrt(X .* X + Y .* Y));
Or, the same, but using .^2 to square X and Y, and storing the result of sqrt() to avoid computing it twice:
d = sqrt(X.^2 + Y.^2);
Z = sin(d)./d;

댓글 수: 3

Ooh so it's giving me that message because of how I typed the formula.
Thank you so much. I was able to proceed with no more issues.
Voss
Voss 2023년 1월 22일
"because of how I typed the formula"
Yes.
"*" is matrix multiplication, and ".*" is element-wise multiplication. Two different operations. Similarly "/" and "./" are different operations.
Glad it's working now!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2023년 1월 22일

댓글:

2023년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by