Is there a way to create a program that can solve a system of equations?
Yeah enter the coefficients into a matrix and use a rref on them.
To solve a system of linear equations, put it in the form where the variables and their coefficients are on one side and the constant is on the other. So, consider the system
Turn it into
Align your variables. Then, input the coefficients and constant into a matrix like so:(1)
Then, do rref on them with the simple syntax rref([A]).
You should get
"Let not the Lord be angry, but let me speak."
How do I rearrange the equations and align the variables? And how would I input them into the matrix? I have never used a matrix before.
Well, rearrange with algebra. If you have 3x=4y-6, subtract 4y from both sides. This "moves" the 4y to the other side. So, you get 3x-4y=4y-6-4y. 4y-4y is 0, so it simplifies to 3x-4y=0-6. 0-6 is (-6), so the final equation is 3x-4y=-6. To input into a matrix, arrange the equations so that they are perfectly aligned by variable. So, all the x's are columned, the y's are columned, the regular numbers are columned, etc. Then, take away everything but the numbers, and you are left with a matrix.
I know how to solve these equations on paper, but how do you rearrange them on a calculator?
Question: how do you want your program's user to input it, as a string like "2X+3Y=5"?
Well, I was assuming that I'd input it in string form, but if there's a better way, I'm open to suggestions. But how do I rearrange the expressions?
Because I'm seeing now that the bulk of whatever you'll be writing will be dedicated to string manipulation, with the actual solving only on one line… :o
There's a naive way to manipulate your string expression so you'd start with "3Y-6=4X" and have it come out as "4X+3Y=6". What would complicate the code would be stuff like "πX-√(3)=4ֿ¹".
I suggest you insist on having the user just input the coefficients manually; it'll be less of a headache.
Surprisingly enough, people sometimes forget (especially if there are more than two variables) so I offer this reminder: make sure you remember which column corresponds to which variable. :)
So for instance, with the system(1)
corresponding to the augmented matrix(2)
where the first column corresponds to the coefficients of the variable $x$, and the second column corresponds to the coefficients of the variable $y$. The last column of course is the equation system's right-hand side.
Analogously, you can have the system(3)
and the corresponding augmented matrix(4)
running rref( on that last one,(5)
where with the result of rref(, you should now only be concerned with the last column: the one in the first row is the corresponding value for $y$, and the one in the second row is the value for $x$.
I had made a program at one time that solves systems, and does Quadratics! if anyone wants it, i may post it.
You dont necessarily need to augment the matricies. You could make a coefficent matrcie and a variable matricie and divide them.
[,] / [[5,2],[7,3]] = answer matrix [[x],[y]]
and divide them
While your logic is right, remember that there is no such thing as matrix division! If you have two matrices A and B, would A/B equal A*Bֿ¹ or Bֿ¹*A?
As a matter of fact, the ambiguity brought upon by matrix multiplication being noncommutative is the reason why MATLAB (*the* language for dealing with matrices) provides for two different types of matrix "division".
Also it is a trivial exercise to show the equivalence of working with an augmented matrix and premultiplying a vector with a matrix inverse. ;)