Search code examples
rmatrixgaussiancoefficients

Modifying R code for getting the augmented coefficient matrix for 5 or more equations and unknown variables


Can anyone help me in modifying this code? I am stuck in this ever since last week. I am trying to make this code work for these following equations yet nothing works.

Also, I am doing this so I can create 2 R functions for Gaussian elimination and Gauss-Jordan elimination. If you are knowledgeable with this, can you help me with it too? Thank you!

Each function (Gauss and Gauss-Jordan) should return a labelled list containing the following:

  • variables: this is a vector containing the unknown variables
  • augcoeffmatrix: this is the last snapshot of the transformed augmented coefficient matrix
  • solution: a vector containing the solution (i.e. the value for each unknown)

If the system has no solution or has an infinite number of solutions, solution should be NA.

These are the equations that I am trying to work with:

# E1 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 8000 * x1 + 4500 * x2 + 4000 * x3 + 3000 * x4 + 2000 * x5 + 1000 * x6 + 900 * x7 + 250 * x8 + -143145000
# E2 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 7800 * x1 + 6500 * x2 + 5800 * x3 + 0 * x4 + 3100 * x5 + 1600 * x6 + 1000 * x7 + 300 * x8 + -58870000
# E3 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 10000 * x1 + 0 * x2 + 3100 * x3 + 0 * x4 + 2600 * x5 + 1300 * x6 + 850 * x7 + 150 * x8 + -108440000
# E4 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 5200 * x1 + 3700 * x2 + 3100 * x3 + 2700 * x4 + 2400 * x5 + 1800 * x6 + 1200 * x7 + 450 * x8 + -143805000
# E5 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 7700 * x1 + 7100 * x2 + 0 * x3 + 5700 * x4 + 5100 * x5 + 1300 * x6 + 950 * x7 + 95 * x8 + -181390500
# E6 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 9300 * x1 + 8700 * x2 + 6100 * x3 + 5100 * x4 + 4000 * x5 + 1000 * x6 + 700 * x7 + 70 * x8 + -209273000
# E7 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 6000 * x1 + 0 * x2 + 5000 * x3 + 4300 * x4 + 3000 * x5 + 1900 * x6 + 1400 * x7 + 920 * x8 + -174388000
# E8 <- function(x1, x2, x3, x4, x5, x6, x7, x8) 8500 * x1 + 3700 * x2 + 4200 * x3 + 3900 * x4 + 3500 * x5 + 2400 * x6 + 1000 * x7 + 250 * x8 + -183065000

THESE IS THE AUGMENTED COEFFICIENT MATRIX CODE:

# Define the equations as functions
E1 <- function(x1, x2, x3) 25 * x1 + 5 * x2 + 1 * x3 + -106.8
E2 <- function(x1, x2, x3) 64 * x1 + 8 * x2 + 1 * x3 + -177.2
E3 <- function(x1, x2, x3) 144 * x1 + 12 * x2 + 1 * x3 + -279.2

system <- list(E1, E2, E3)

AugCoeffMatrix <- function(system){
  
  # Check if the number of unknown variables is equal for all equations in the system.
  numOfVar <- sapply(system, function(eq)length(formals(eq)))
  if (length(unique(numOfVar)) != 1) {
    return(NA)
  }
  
  # Check if the number of equations is less than the number of unknown variables.
  if (length(system) < numOfVar[1]) {
    return(NA)
  }
  
  # Initialize an empty character vector named "variables" to store the variable names.
  variables <- character(0)
  
  # Create a matrix named "augcoeffmatrix" with dimensions based on the length of the system.
  augcoeffmatrix <- matrix(0, length(system), length(system)+1)
  
  # Iterate over each equation in the system.
  for (count in 1:length(system)){
    
    # Retrieving the equation string representation using the "deparse" function.
    equation <- deparse(system[[count]])
    
    # Extracting the variable names from the equation string.
    variables <- gsub("function","",equation[1])
    variables <- gsub(" ", "", variables)
    variables <- gsub("\\(","",variables) #Because parentheses must be escaped with double backslashes because they are special characters in regular expressions, the pattern "\\(" is employed.
    variables <- gsub("\\)","",variables)
    variables <- strsplit(variables,",")
    
    # Creating column names for the augmented coefficient matrix. These are the variable names plus "RHS" to represent the right-hand side.
    colnames(augcoeffmatrix) <- c(variables[[1]], "RHS")
    
    # Creating row names for the augmented coefficient matrix. These are the numbers from 1 to the length of the system.
    rownames(augcoeffmatrix) <- 1:length(system)
    
    # Split the equation string into individual terms using the "+" delimiter.
    terms <- strsplit(equation[2]," \\+")
    
    # Iterate (using for loop) over each term in the equation.
    for (i in terms[[1]]){
      
      # Check if the term contains a multiplication operator "*".
      if (grepl("*",i,fixed=TRUE)){
        
        # Split the term into coefficient and variable parts using the "*" delimiter.
        coeff <- trimws(unlist(strsplit(i,"\\*")))
        
        # Updating the corresponding entry in the augmented coefficient matrix with the numeric value of the coefficient.
        augcoeffmatrix[count,coeff[[2]]] <- as.numeric(coeff[[1]])
        
      }else{
        
        # If the term does not contain a multiplication operator, it represents a constant value on the right-hand side of the equation.
        # Multiply the constant value by -1 and update the "RHS" column of the matrix.
        augcoeffmatrix[count, "RHS"] <- as.numeric(i)*-1
      }
    }
  }
  
  result <- list("variables" = variables[[1]], "augcoefmatrix" = augcoeffmatrix)
  
  return(result)
}

result1 = AugCoeffMatrix(system)
print(result1)

Solution

  • you are using deparse to break the function into textual parts. but linebreaks are hurting you. you can dodge that with

        equation <- deparse(system[[count]],width.cutoff = 500)