简体   繁体   English

MATLAB 中的大量线性方程

[英]large number of linear equations in MATLAB

I want to use solve() to solve a large system of linear equations.我想使用solve()来求解一个大型线性方程组。 The solve() function needs equations and variables. solve()函数需要方程和变量。 I use a loop to generate the equations and my variables are contained in a large array.我使用循环来生成方程,我的变量包含在一个大数组中。 This is a simple code of what I am trying to do:这是我正在尝试做的简单代码:

x = sym('x',[1 3])
eqn = sym('eqn', [1,3])
eqn1 = 2*x1 + x2 + x3 - 2 == 0
eqn2 = 2*x2 -x2 -x1- x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
Y = solve(eqn, x)

MATLAB does not recognize my variable x1 . MATLAB 无法识别我的变量x1 I have solved the same system using the following code:我已经使用以下代码解决了相同的系统:

syms x1 x2 x3
eqn1 = 2*x1 + x2 + x3 == 2
eqn2 =   2*x2 -x2 -x1 - x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
X = solve([eqn1, eqn2, eqn3, eqn4], [x1 x2 x3])
structfun(@subs,X)

But this is useless for a very large number of equations.但这对于大量的方程是无用的。 What am I doing wrong?我究竟做错了什么?

You don't need symbolic ( syms ) for that.你不需要符号( syms )。 This is a standard linear system of equations that can be represented as: Ax = b where A = [2 1 1; -1 1 -1; 1 2 3]这是一个标准的线性方程组,可以表示为: Ax = b其中A = [2 1 1; -1 1 -1; 1 2 3] A = [2 1 1; -1 1 -1; 1 2 3] A = [2 1 1; -1 1 -1; 1 2 3] , x = [x1; x2; x3] A = [2 1 1; -1 1 -1; 1 2 3] , x = [x1; x2; x3] x = [x1; x2; x3] x = [x1; x2; x3] and b = [0; 3; -10] x = [x1; x2; x3]b = [0; 3; -10] b = [0; 3; -10]

To solve for x, you would first define要求解 x,您首先要定义

A = [2 1 1; -1 1 -1; 1 2 3]

and

b = [0; 3; -10]

and then solve using然后解决使用

x = A\\b

PS.附注。 There are some odd things in your question, eg.你的问题中有一些奇怪的东西,例如。 in eq.2 eqn2 = 2*x2 -x2 -x1- x3 == 3 I assume you omitted simplying this to -x1 +x2 -x3 ==3 .eqn2 = 2*x2 -x2 -x1- x3 == 3我假设你省略了简单地将其简化为-x1 +x2 -x3 ==3

PS2. PS2。 This is pretty standard Matlab, you can find a lot of info under the standard mldivide page in the documentation along with a lot similar questions here on SO.这是非常标准的 Matlab,您可以在文档的标准mldivide 页面下找到很多信息,以及关于 SO 的许多类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM