Why does MatLab give non integer values for whole numbers?

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)

 Respuesta aceptada

John D'Errico
John D'Errico el 13 de Dic. de 2024
Editada: Walter Roberson el 13 de Dic. de 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)
ans = 2×1
0.7071 0.7071
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
null(sym(A)), disp(char(ans))
[1; 1]
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 comentarios

Thanks! Using null(sym(A)) worked. I didn't realize null would auto normalize. Doing the math by hand yields the "easier" solution of [1; 1].
Personally, I would have written the two versions of null to be consistent. But if I ruled the world, well, you know.
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]
A = 2×2
1 -1 -2 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N = null(A, "rational")
N = 2×1
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2024b

Preguntada:

el 13 de Dic. de 2024

Editada:

el 13 de Dic. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by