MATLAB Answers

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
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
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.

Tags

Community Treasure Hunt

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

Start Hunting!