Solving System of Equations

5 ビュー (過去 30 日間)
Erkin Karatas
Erkin Karatas 2019 年 11 月 11 日
コメント済み: Fabio Freschi 2019 年 11 月 11 日
Hi all,
I want to solve equations, when I know the number of equations, this is how I normally do
%
syms a b c d
eqn1 = 13*a -2.5*b == 8;
eqn2 = -10.5*a + 13*b - 2.5*c == 8;
eqn4 = -10.5b + 2*c + 3*d == 8;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [a, b, c, d])
X = linsolve(A,B)
I want to modify this format(if possible) into the unkown equations, for example for 10 equations or 20. Since the number of equations can change, I cannot write them as shown above. If it's not possible, is there any other way to solve these unknown number of equations which are dependent each other?
%Modified??
n=10;
syms a b c d e x y z %number of variables can change as the number of equations change, so no idea what to write here
eqn1 = 13*a -2.5*b == 8;
eqn2 = -10.5*a + 13*b - 2.5*c == 8;
eqn3 = -10.5*b + 13*c -2.5*d == 8;
eqn4 = -10.5*c + 13*d - 2.5*e == 8;
...
eqnn = -10.5x + 2*y + 3*z == 8;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z]) %?
X = linsolve(A,B)

回答 (2 件)

Fabio Freschi
Fabio Freschi 2019 年 11 月 11 日
It seems you are solving a numeric system of equations. Why don't you simply put the coefficient matrix in A and the right-hand-side in an array b and use backslash?
% case 1
A = [13 -2.5 0 0;
-10.5 13 -2.5 0
1 1 1 1 % dummy values: equation 3 is missing in your code
0 -10.5 2 3];
b = [8
8
8 % dummy value
8];
x = A\b;
You can scale it to as many equations you like, and it is definitely faster than symbolic calulations
  3 件のコメント
Fabio Freschi
Fabio Freschi 2019 年 11 月 11 日
It is about 15 years that I write numerical formulations for electromagnetics. You should build your coefficient matrix filling the entries using your calculations (based on FEM, FDM, ...) and then using
x = A\b;
To solve the final system. BTW: using standard discretization of PDE, you will have a sparse coefficient (stiffness) matrix. It may be of help having a look at the sparse command and the use of sparse matrices in Matlab
Fabio Freschi
Fabio Freschi 2019 年 11 月 11 日
To go further in the details, the stiffness matrix is usually built as
for i = 1:Nelements % loop over elements
Aloc = zeros(Nnodes) % preallocation
for j = 1:Nnodes % loop over nodes og th i-th elem
for k = 1:Nnodes % loop over nodes og th i-th elem
Aloc(i,j) = % your method
end
end
... % map local matrix to global matrix A
end
Even if there are many examples to avoid some or all these loops.

サインインしてコメントする。


Jeremy
Jeremy 2019 年 11 月 11 日
Typically I prefer doing it numerically by putting my equations into the form
Ax=b
and then
x = A\b
  3 件のコメント
Jeremy
Jeremy 2019 年 11 月 11 日
Then you would modify your A matrix and b vector based on user input, but x = A\b is still going to give the correct answer as long as the problem is set up correctly
Erkin Karatas
Erkin Karatas 2019 年 11 月 11 日
I know, but how can i modiy my A matrix. How can I put the coefficients in to the matrix.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeComputational Fluid Dynamics (CFD) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by