equationsToMatrix
Convert linear equations to matrix form
Description
Examples
Convert Linear Equations to Matrix Form
Convert a system of linear equations to matrix form. equationsToMatrix
automatically detects the variables in the equations by using symvar
. The returned coefficient matrix follows the variable order determined by symvar
.
syms x y z eqns = [x+y-2*z == 0, x+y+z == 1, 2*y-z == -5]; [A,b] = equationsToMatrix(eqns)
A =
b =
vars = symvar(eqns)
vars =
You can change the arrangement of the coefficient matrix by specifying other variable order.
vars = [x,z,y]; [A,b] = equationsToMatrix(eqns,vars)
A =
b =
Specify Variables in Equations
Convert a linear system of equations to the matrix form by specifying independent variables. This is useful when the equations are only linear in some variables.
For this system, specify the variables as [s t]
because the system is not linear in r
.
syms r s t eqns = [s-2*t+r^2 == -1 3*s-t == 10]; vars = [s t]; [A,b] = equationsToMatrix(eqns,vars)
A =
b =
Return Only Coefficient Matrix of Equations
Return only the coefficient matrix of the equations by specifying a single output argument.
syms x y z eqns = [x+y-2*z == 0, x+y+z == 1, 2*y-z == -5]; vars = [x y z]; A = equationsToMatrix(eqns,vars)
A =
Solve System of Equations That Are Functions of Time
Consider the following system of linear equations that are functions of time:
Declare the system of equations.
syms x(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) =
Specify the independent variables , , and in the equations as a symbolic vector vars
. Use the equationsToMatrix
function to convert the system of equations into the matrix form.
vars = [x(t); y(t); z(t)]; [A,b] = equationsToMatrix(eqn,vars)
A =
b =
Solve the matrix form of the equations using the linsolve
function.
X = linsolve(A,b)
X =
Evaluate the solution for the functions and . Plot the solution.
zSol = subs(X(3),[u(t) v(t)],[cos(t) sin(2*t)])
zSol =
fplot(zSol)
Input Arguments
eqns
— Linear equations
vector of symbolic equations or expressions
Linear equations, specified as a vector of symbolic equations or expressions.
Symbolic equations are defined by using the ==
operator, such as
x + y == 1
. For symbolic expressions,
equationsToMatrix
assumes that the right side is 0.
Equations must be linear in terms of vars
.
vars
— Independent variables
vector of symbolic variables (default) | vector of symbolic functions
Independent variables in eqns
, specified as a vector of
symbolic variables or symbolic functions.
Output Arguments
A
— Coefficient matrix
symbolic matrix
Coefficient matrix of the system of linear equations, specified as a symbolic matrix.
b
— Right sides of equations
symbolic matrix
Vector containing the right sides of equations, specified as a symbolic matrix.
More About
Matrix Representation of System of Linear Equations
A system of linear equations
can be represented as the matrix equation . Here, A is the coefficient matrix.
is the vector containing the right sides of equations.
Version History
Introduced in R2012b
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)