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)) .
Warning message when creating surface plot.
3 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
Voss
2023-1-22
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!