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 views (last 30 days)
Show older comments
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?
Accepted Answer
Voss
on 22 Jan 2023
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 Comments
Voss
on 22 Jan 2023
"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!
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!