Why does MatLab give non integer values for whole numbers?
2 views (last 30 days)
Show older comments
I noticed MatLab sometimes gives ridiculous answers to obvious problems. As an example, I gave it a 2x2 matrix and asked it to give me the null.
A = [1 -1; -2 2]
B = null(A)
it prints out that B is [985/1393; 985/1393] or [0.7071; 0.7071] depending on format.
The answer should be B = [1; 1]
Any thoughts on how to fix this? It has become a problem as sometimes the answer has been 0.7071.
AS an example doing QR factorization of A = [-4 -1; 4 0] yields Q = [-0.7071 0.7071; 0.7071 0.7071]
[Q, R] = qr(A)
0 Comments
Accepted Answer
John D'Errico
on 13 Dec 2024
Edited: Walter Roberson
on 13 Dec 2024
It is not even remotely ridiculous.
Are you seriously claiming that the vector [0.7071;0.7071] is NOT a null vector?
Any multiple of the vector [1;1] is as equally a valid result. If you don't like how it is normalized, then there is no reason you cannot normalize it yourself.
Why is it that you will sometimes see different normalizations? The Symbolic toolbox makes a different choice of how to normalize things. But since the normalization is completely and absolutely irrelevant, why do you care?
A = [1 -1;-2 2];
null(A)
null(sym(A)), disp(char(ans))
So your "problem" stems from the fact that SOMETIMES, you are computing a nullspace vector from a symbolic array, and other times, you are working from an array of doubles. When called with a double array, null automatically normalizes the vector to have unit 2-norm, ergo the 0.7071.
Either result is equally valid, and equally easy to change if you don't like it,
3 Comments
John D'Errico
on 13 Dec 2024
Personally, I would have written the two versions of null to be consistent. But if I ruled the world, well, you know.
Steven Lord
on 13 Dec 2024
When a problem has multiple solutions, sometimes determining which is the "easiest" solution can be tricky. See for example this post from Cleve Moler's blog: The World's Simplest Impossible Problem.
Is 3 and 3 the "easiest" solution to the problem Cleve posed? Or is it 0 and 6? 2 and 4?
But if you want a solution that "looks nicer", you can do that with the "rational" option for the null function to return rational answers. [Be sure to read the caution in the Description section for that syntax.]
A = [1 -1; -2 2]
N = null(A, "rational")
More Answers (0)
See Also
Categories
Find more on Digital Filtering 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!