So I was just reading my algebra-precalculus textbook, and learned that matrices can be used to solve systems of equations. Since the whole process seemed so algorithmic, I wondered if I could implement it as a program. The result is the following:
/*
*
* Solve a system of equations in two variables, x and y
*
*/
#include <iostream>
struct LinearEquation
{
double xCoefficient;
double yCoefficient;
double constantTerm;
};
struct Solution
{
double x;
double y;
};
LinearEquation readEquation();
double findDeterminant(const LinearEquation& a, const LinearEquation& b);
Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant);
int main()
{
std::cout << "Welcome to the linear equation solver! Enter the following inputs for two equations of the form ax + by = c.\n\n";
LinearEquation one, two;
std::cout << "Equation #1\n";
one = readEquation();
std::cout << "Equation #2\n";
two = readEquation();
double det = findDeterminant(one, two);
if (det != 0)
{
Solution solution = solveEquation(one, two, det);
std::cout << "The solution:\n";
std::cout << "x = " << solution.x << "\n";
std::cout << "y = " << solution.y << "\n";
}
else
{
std::cout << "This linear equation has no unique solutions!\n";
}
}
LinearEquation readEquation()
{
LinearEquation result;
std::cout << "X coefficient: ";
std::cin >> result.xCoefficient;
std::cout << "Y coefficient: ";
std::cin >> result.yCoefficient;
std::cout << "Constant term: ";
std::cin >> result.constantTerm;
return result;
}
double findDeterminant(const LinearEquation& a, const LinearEquation& b)
{
return a.xCoefficient * b.yCoefficient - a.yCoefficient * b.xCoefficient;
}
Solution solveEquation(const LinearEquation& a, const LinearEquation& b, double determinant)
{
// Calculating the inverse
double matrixTL = b.yCoefficient / determinant;
double matrixTR = -1 * (a.yCoefficient / determinant);
double matrixBL = -1 * (b.xCoefficient / determinant);
double matrixBR = a.xCoefficient / determinant;
Solution result;
// Matrix multiplication
result.x = matrixTL * a.constantTerm + matrixTR * b.constantTerm;
result.y = matrixBL * a.constantTerm + matrixBR * b.constantTerm;
return result;
}
This program simply goes through each coefficient and constant term and basically applies a "formula" to solve the equation. It works as far, but it could have minor bugs. I am looking for ways this program can be improved in terms of program size or efficiency. Another problem I want to solve is in terms to making this program handle more equations/variables, which would currently require manually adding parts of the matrix.
1 Answer 1
Why not do the arithmetic in parentheses? That way, it will be clearer. The rest of the code is very clear and understandable, I didn't find any bugs.
-
\$\begingroup\$ What do you mean "do the arithmetic in parentheses". It's unclear to me what you mean by that. If you mean to use parentheses around operations that rely on order of operations, wouldn't that be redundant? \$\endgroup\$Arnav Borborah– Arnav Borborah2018年06月07日 14:05:50 +00:00Commented Jun 7, 2018 at 14:05
-
\$\begingroup\$ It is convenient for the compiler, at the same time, it will be more clear for a person to see the priority of operations without calculating it in his head. But whatever you like \$\endgroup\$Марк Стариков– Марк Стариков2018年06月07日 15:33:01 +00:00Commented Jun 7, 2018 at 15:33
-
\$\begingroup\$ I get the second part, but why is it convenient for the compiler? (Do you have a source?) \$\endgroup\$Arnav Borborah– Arnav Borborah2018年06月07日 15:43:17 +00:00Commented Jun 7, 2018 at 15:43
-
\$\begingroup\$ you misunderstood me ( sorry for the bad English ), I meant that the compiler has no problems prioritizing operations, and the person - no. Therefore, when there are brackets, it is easier to read the expression. Again, this is my personal opinion \$\endgroup\$Марк Стариков– Марк Стариков2018年06月07日 16:32:53 +00:00Commented Jun 7, 2018 at 16:32
-
\$\begingroup\$ Ah, I see, it was somewhat difficult to understand at first. Thanks for clarifying! \$\endgroup\$Arnav Borborah– Arnav Borborah2018年06月07日 17:40:15 +00:00Commented Jun 7, 2018 at 17:40