Simultaneous Equation using reduced row method

Hi, I am new to the software and am wondering how you would write a code which would solve any number of simultaneous equations. ie a code that when 3 equations were inputted would work out the answers as well as if you inputted 10 equations

 Accepted Answer

Put the coefficients into a matrix, then divide.
Syntax
Description
x = A\B solves the system of linear equations A*x = B. The matrices A and B must have the same number of rows. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.
  • If A is a scalar, then A\B is equivalent to A.\B.
  • If A is a square n-by-n matrix and B is a matrix with n rows, then x = A\B is a solution to the equation A*x = B, if it exists.
  • If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.
Example:
% 2x + 4y + 3z = 3
% 3x + 5y + 2z = 8
% 1x + 2y + 5z = 9
A = [2,4,3; 3,5,2; 1,2,5]
B = [3;8;9]
xyz = A\B
% Other example from the help
A = magic(3)
B = [15; 15; 15];
x = A\B
If you still need help, give us your set of equations.

6 Comments

Thank you for the help that is brilliant!
The problem I was given to work on after my lecture was as follows:
A system was modelled using first principles and the following set of linear equations was developed to describe it
Develop a script using MATLAB that will compute the reduced row echelon form of the augmented matrix developed, using only the basic operations of addition, subtraction, multiplication, and division, and the identifier for rows and columns Extra credit will be awarded for a script that can solve any set of three linear equations, and full credit will be awarded for a script that can solve any set of any number of linear equations.
Would your solution work for this?
Thanks in advance, Harry
If you can get the matrices, then yes, for sure. But what is your input? Is it the matrices? Or is it a character string with the equation and you have to parse out the numbers from it?
The way I’ve understood it is that A would be [2,4,6; 7,3,0; 2,0,1] and B would be [3;-2;-3] and I would have to use these to transform the inverse of A into a 3x3 identity matrix which in turn B into the answers for a b c. I wouldn’t know how to script this.
If you want, you can do the regular multi-matrix solution for least squares using ' to do the transpose, and inv() to do the inverse : inv(A' * A) * A' * B.
Here it is both ways. They both give the same answer:
A =[2,4,6;
7,3,0;
2,0,1]
B = [3;
-2;
-3]
% Method 1 : using \
abc = A \ B;
a = abc(1)
b = abc(2)
c = abc(3)
% Double check. We should recover B when we multiply things out.
b1 = A(1,1) * a + A(1,2) * b + A(1,3) * c
b2 = A(2,1) * a + A(2,2) * b + A(2,3) * c
b3 = A(3,1) * a + A(3,2) * b + A(3,3) * c
% Method 2 : using matrix solution
abc2 = inv(A' * A) * A' * B
a = abc2(1)
b = abc2(2)
c = abc2(3)
% Double check. We should recover B when we multiply things out.
b1 = A(1,1) * a + A(1,2) * b + A(1,3) * c
b2 = A(2,1) * a + A(2,2) * b + A(2,3) * c
b3 = A(3,1) * a + A(3,2) * b + A(3,3) * c
You'll see:
A =
2 4 6
7 3 0
2 0 1
B =
3
-2
-3
a =
-1.22413793103448
b =
2.18965517241379
c =
-0.551724137931035
b1 =
3
b2 =
-2
b3 =
-3
abc2 =
-1.22413793103448
2.18965517241379
-0.551724137931035
a =
-1.22413793103448
b =
2.18965517241379
c =
-0.551724137931035
b1 =
3
b2 =
-2
b3 =
-3
My pleasure. To thank people in the forum, you can award them "reputation points" by "Accepting" their answer and also clicking on the "Vote" icon. Thanks in advance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!