Solving System of Equations

2 views (last 30 days)
Erkin Karatas
Erkin Karatas on 11 Nov 2019
Commented: Fabio Freschi on 11 Nov 2019
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)

Answers (2)

Fabio Freschi
Fabio Freschi on 11 Nov 2019
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 Comments
Fabio Freschi
Fabio Freschi on 11 Nov 2019
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 on 11 Nov 2019
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.

Sign in to comment.


Jeremy
Jeremy on 11 Nov 2019
Typically I prefer doing it numerically by putting my equations into the form
Ax=b
and then
x = A\b
  3 Comments
Jeremy
Jeremy on 11 Nov 2019
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 on 11 Nov 2019
I know, but how can i modiy my A matrix. How can I put the coefficients in to the matrix.

Sign in to comment.

Categories

Find more on Fluid Mechanics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!