主要内容

方程式stomatrix

Convert linear equations to matrix form

Description

example

[A,b] = equationStomatrix(eqns)converts equationseqns到矩阵形式。eqns必须是所有变量中方程式的线性系统symvarfinds ineqns

example

[A,b] = equationStomatrix(eqns,vars)convertseqnsto matrix form, whereeqns必须是线性的vars

example

A= equationsToMatrix(___)仅返回方程系统的系数矩阵。

Examples

collapse all

Convert a system of linear equations to matrix form.方程式stomatrixautomatically detects the variables in the equations by usingsymvar。The returned coefficient matrix follows the variable order determined bysymvar

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

( 1 1 - 2 1 1 1 0 2 - 1 )

b =

( 0 1 - 5 )

vars =symvar(eqns)
vars =
                      
                       
                        
                         
                          (
                         
                          
                           
                            
                             
                              x
                            
                           
                           
                            
                             
                              y
                            
                           
                           
                            
                             
                              z
                            
                           
                          
                         
                         
                          )
                        
                       
                      

You can change the arrangement of the coefficient matrix by specifying other variable order.

vars =[x,z,y]; [A,b] = equationsToMatrix(eqns,vars)
A =

( 1 - 2 1 1 1 1 0 - 1 2 )

b =

( 0 1 - 5 )

Convert a linear system of equations to the matrix form by specifying independent variables. This is useful when the equation are only linear in some variables.

For this system, specify the variables as[s t]因为系统不线性r

symsrsteqns = [s-2*t+r^2 == -1 3*s-t == 10]; vars = [s t]; [A,b] = equationsToMatrix(eqns,vars)
A =

( 1 - 2 3 - 1 )

b =

( - r 2 - 1 10 )

Return only the coefficient matrix of the equations by specifying a single output argument.

symsxyzeqns = [x+y-2*z == 0, x+y+z == 1, 2*y-z == -5]; vars = [x y z]; A = equationsToMatrix(eqns,vars)
A =

( 1 1 - 2 1 1 1 0 2 - 1 )

考虑以下时间函数的线性方程系统:

2 x ( t ) + y ( t ) + z ( t ) = 2 u ( t ) - x ( t ) + y ( t ) - z ( t ) = v ( t ) x ( t ) + 2 y ( t ) + 3 z ( t ) = - 1 0

Declare the system of equations.

symsx(t)y(t)z(t)u(t)v(t)eqn1 = 2*x + y + z == 2*u; eqn2 = -x + y - z == v; eqn3 = x + 2*y + 3*z == -10; eqn = [eqn1; eqn2; eqn3]
eqn(t) =

( 2 x ( t ) + y ( t ) + z ( t ) = 2 u ( t ) y ( t ) - x ( t ) - z ( t ) = v ( t ) x ( t ) + 2 y ( t ) + 3 z ( t ) = - 10 )

Specify the independent variables x ( t ) , y ( t ) , 和 z ( t ) 在方程式中作为符号向量vars。Use the方程式stomatrixfunction to convert the system of equations into the matrix form.

vars =[x(t); y(t); z(t)]; [A,b] = equationsToMatrix(eqn,vars)
A =

( 2 1 1 - 1 1 - 1 1 2 3 )

b =

( 2 u ( t ) v ( t ) - 10 )

Solve the matrix form of the equations using thelinsolvefunction.

X = linsolve(A,b)
X =

( 10 u ( t ) 9 - v ( t ) 9 + 20 9 4 u ( t ) 9 + 5 v ( t ) 9 - 10 9 - 2 u ( t ) 3 - v ( t ) 3 - 10 3 )

Evaluate the z ( t ) solution for the functions u ( t ) = cos ( t ) v ( t ) = sin ( 2 t ) 。绘制 z ( t ) solution.

ZSOL =subs(X(3),[u(t) v(t)],[cos(t) sin(2*t)])
ZSOL =

- sin ( 2 t ) 3 - 2 cos ( t ) 3 - 10 3

fplot(zSol)

图包含一个轴对象。轴对象包含一个类型函数线的对象。

Input Arguments

collapse all

Linear equations, specified as a vector of symbolic equations or expressions. Symbolic equations are defined by using the==operator, such asx + y == 1。For symbolic expressions,方程式stomatrixassumes that the right side is 0.

Equations must be linear in terms ofvars

Independent variables ineqns, specified as a vector of symbolic variables or symbolic functions.

Output Arguments

collapse all

Coefficient matrix of the system of linear equations, specified as a symbolic matrix.

Vector containing the right sides of equations, specified as a symbolic matrix.

More About

collapse all

线性方程系统的矩阵表示

线性方程系统

a 11 x 1 + a 12 x 2 + + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + + a 2 n x n = b 2 a m 1 x 1 + a m 2 x 2 + + a m n x n = b m

can be represented as the matrix equation A x = b 。这里,Ais the coefficient matrix.

A = ( a 11 a 1 n a m 1 a m n )

b 是包含方程式右侧的向量。

b = ( b 1 b m )

Introduced in R2012b