equationsToMatrix:如何获得变量的值?

时间:2021-05-09 15:20:51

When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)

当你使用equationsToMatrix时,你需要解出一组线性方程,就像这个例子(包括解)

syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])

%solution of the equation set

A =
[ 1, 1, -2]
[ 1, 1,  1]
[ 0, 2, -1]

b =
  0
  1
 -5

The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.

向量b返回问题变量的值:x,y,和z,但是如果我输入x,那么MATLAB返回x,而不是0,这是方程的解。在不添加syms选项的情况下也会发生这种情况。

The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z. What I would need is to get something like this in the end

另一个问题是,如果我键入b(1)或b(2),我不会得到任何值:我希望b包含x、y和z的值

b(1) = 0

or

x = 0

What should I do to get the values of x,y,z by just typing x,y,z?

我应该怎么做才能通过输入x y z得到x的值?

1 个解决方案

#1


3  

What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do

你所拥有的是一种通过提取系数矩阵将符号线性方程转换成数值系统的方法。要解决这个系统你需要做的

sol = A\b;

and now you can use the values in another expression with

现在你可以用另一个表达式中的值

subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));

路径替换(expr { x,y,z },{索尔(1)溶胶(2),索尔(3));

for example

例如

A =

 1     1    -2
 1     1     1
 0     2    -1

b =

 0
 1
-5

>> A\b

ans =

3.0000
-2.3333
0.3333

#1


3  

What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do

你所拥有的是一种通过提取系数矩阵将符号线性方程转换成数值系统的方法。要解决这个系统你需要做的

sol = A\b;

and now you can use the values in another expression with

现在你可以用另一个表达式中的值

subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));

路径替换(expr { x,y,z },{索尔(1)溶胶(2),索尔(3));

for example

例如

A =

 1     1    -2
 1     1     1
 0     2    -1

b =

 0
 1
-5

>> A\b

ans =

3.0000
-2.3333
0.3333