Friday, September 11, 2015

Solving a System of Linear Equations

Solving a System of Linear Equations

Part I

Find x and y that satisfies these two equations (AoPS Introduction to Algebra, problem 5.3):

$$x+3y=4$$$$-2x+5y=-30$$
In [1]:
from sympy import *
In [2]:
x, y = symbols('x y')

# NOTE: the solve command needs the equations to be homogeneous (zero on the right-hand side.)

solve([x+3*y-4,-2*x+5*y+30],[x,y])
Out[2]:
{x: 10, y: -2}

Part II

A problem from the 2011 Mathcounts competition

The eighth grade at Memorial Middle School is selling wrapping paper and gift bags to raise money for a field trip. They sold one-half as many rolls of wrapping paper as they did gift bags. The profit they make for each roll of wrapping paper is \$2.50 and for each gift bag is \$1.50. If the total profit was \$704, how many gift bags were sold?

In [3]:
from sympy import *
In [4]:
W, G = symbols('W G')

solve([2.50*W+1.50*G-704,G-2*W])
Out[4]:
{G: 256.000000000000, W: 128.000000000000}

As an experiment, see what happens if you represent 2.50 as Fraction(5,2) and 1.50 as Fraction(3,2)

In [ ]:
from fractions import Fraction
W, G = symbols('W G')
solve([Fraction(3,2)*W+Fraction(3,1)*G-1704,W-2*G])

Part III

Find coefficients a,b,c,d,e,f such that the following equation is balanced.

$ \textbf{a } CaSO_4 + \textbf{b } CH_4 + \textbf{c } CO_2 \rightarrow \textbf{d } CaCO_3 + \textbf{e } S + \textbf{f } H_2O $

$$(calcium.sulfate)+(methane)+(carbon.dioxide)\rightarrow$$$$(calcium.carbonate)+(sulfer)+(water)$$

For each element, write out an equation for the relevant coefficients so that the number of atoms of that element will be equal on both sides of the reaction.

Ca:  a=d 
S :  a=e 
O :  4a+2c=3d+f 
C :  b+c=d
H :  4b=2f
In [5]:
from sympy import *
In [6]:
a, b, c, d, e, f = symbols('a b c d e f')

solve([
        a-d,
        a-e,
        4*a+2*c-3*d-f,
        b+c-d,
        4*b-2*f,
    ],[a,b,c,d,e,f])
Out[6]:
{c: f/6, e: 2*f/3, b: f/2, a: 2*f/3, d: 2*f/3}

Select a value for f that results in integer coefficients, and re-solve.

In [7]:
solve([
        a-d,
        a-e,
        4*a+2*c-3*d-f,
        b+c-d,
        4*b-2*f,
        f-6
    ],[a,b,c,d,e,f])
Out[7]:
{f: 6, c: 1, d: 4, a: 4, e: 4, b: 3}
Resources: