How to make matlab give me a numeric answer to a matrix with infinite solutions

9 次查看(过去 30 天)
So, I have this matrix:
MCh = 1 0 -1 0
5 0 0 -1
0 2 -3 -1
0 1 -4 0
and I'm using \ to solve the system using this other matrix:
B = 0
0
0
0
This is obviously giving me an infinite amount of solutions, so how do I make it give me one of all of the sets?
I am using MCh\B to solve it.
Thanks for the help!

采纳的回答

John D'Errico
John D'Errico 2022-3-31
MCh =[ 1 0 -1 0
5 0 0 -1
0 2 -3 -1
0 1 -4 0];
rank(MCh)
ans = 3
You wish to solve the 4x4 homogeneous linear system. It will have a valid non-zero solution only if the rank is less than full rank. Since the rank is 3, it has a solution. The way to solve it is to find the nullspace of the matrix. That is:
format long
X = null(MCh)
X = 4×1
0.152498570332605 0.609994281330419 0.152498570332605 0.762492851663023
MCh*X
ans = 4×1
1.0e-15 * 0.055511151231258 0.222044604925031 0.333066907387547 0.222044604925031
As you can see, this solves the problem (if we ignore the floating point trash that remains.) The vector that results is normalized to have unit norm.
If you want an exact solution, this will do it:
Xsym = null(sym(MCh))
Xsym = 
And there we see a nice simple form falls out.
  1 个评论
Steven Lord
Steven Lord 2022-3-31
You can also get a somewhat nicer basis vector from null by adding the 'r' option.
MCh =[ 1 0 -1 0
5 0 0 -1
0 2 -3 -1
0 1 -4 0];
x = null(MCh, 'r')
x = 4×1
0.2000 0.8000 0.2000 1.0000
While this x looks "nicer" in some sense than the X from your post, if you want to show the rational form of x to make it look even "nicer" you can use the rats function. This agrees with your symbolic solution.
rats(x)
ans = 4×14 char array
' 1/5 ' ' 4/5 ' ' 1/5 ' ' 1 '

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by