Blog 5: Finding Points of Intersection using Matrices

After learning how to plot data sets in R, I thought the logical next step would be to create equations and solve them against each other, more commonly known as solving systems of equations or finding Points of Intersection. I'll start with two equations in a grade 10-type problem and generalize into a whole system of equations which R is very good at doing.

Right off the bat I tried to input equations into R but found that writing my equations as matrices to be much more efficient. A quick introduction into matrices:

A matrix, not the movie, is a collection of the coefficients of a function in standard form. Each row is an equation without an equals sign, and each column is assigned to a variable. To have an equals sign, another matrix is needed. This is commonly known as the "B" matrix that just collects what the function is equal to. For example,

    y = 2x - 5 is an equation in slope-intercept form. 

    It can also be written as the function: 2x - 1y = 5

    And in matrix form as: A = [2    -1]

    Where the missing "= 5" is given by: B = [5]

Or in R code:

    A = c(2,-1)

    B = c(5)

    # Where A = B gives the solution to the equation

These are matrices! 

Building using our knowledge that each row is a different function

Let's try these two equations:

    y = 2x - 8    or     2x - y = 8

    y = -x + 1    or     x + y = 1

What does this look like in R code?

    A = rbind(c(2,-1),

             (c(1,1))

    B = c(8,1)

    solve(A,B)

    [1] 3 2

The solution, or the Point of Intersection, is x = 3. y = 2, or (3,2).

Reference: Geeks for Geeks










Comments