R-Studio Homework: Matrix Operations, Transformations, and Solutions

Verified

Added on  2023/06/13

|16
|1675
|428
Homework Assignment
AI Summary
This R-Studio assignment provides solutions to linear algebra problems involving matrix operations and equation solving. The first question focuses on creating a matrix A based on given conditions, then performing row operations such as swapping the first and last rows, adding the first row to others, and scaling odd rows. The code prompts the user for the number of rows (m) and columns (n). The second question involves solving for coefficients a and b from equations formed by matrices with dimensions n=3 and n=4, utilizing the 'matlib' package. The third question focuses on determining coefficients for a cubic polynomial equation using matrix operations, again leveraging the 'matlib' package to solve for the coefficients a, b, c, and d. The assignment includes R code snippets for each step, demonstrating the implementation of matrix manipulations and equation solving techniques.
Document Page
R-studio Assignment
Institution Name
Student Name
Submission Date
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
QUESTION 1
R.Code for input of n, number of columns:
Prompting the user to input the value of n, the number of columns of A
readinteger <- function()
{
n <- readline(prompt="Input n: ")
if(!grepl("^[0-9]+$",n))
{
return(readinteger())
}
return(as.integer(n))
}
print(readinteger())
Document Page
R.Code for input of m, number of rows:
#Prompting the user to input the value of m, the number of rows of A
readinteger1 <- function()
{
m <- readline(prompt="Input m: ")
if(!grepl("^[0-9]+$",m))
{
return(readinteger1())
}
return(as.integer(m))
}
print(readinteger1())
Document Page
1. R.CODE
#Part 1
#Defining the conditions for matrix A
#Creating function elements for the conditions
elements <- function()
{
#Initializing the function variables; matrix A, and integers i and J for the rows and
columns respectively1
A <- 0
i <- 0
j <- 0
#Conditional if function
if ((i %% 2) == 0)
{
A[i,j] <- i + j
}
else
{
A[i,j] <- (i*j) + (i^2)
}
1 Zhang, Fuzhen. 2009.
Linear Algebra: Challenging Problems for Students. The Johns Hopkins
University Press.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
#Returning the matrix A
A <- matrix(nrow = m, ncol = n, c(elements()))
A
2. R.CODE
i) #Part 2 (i)
#Creating matrix B to hold the matrix for first and last rows of matrix A swapped
B <- A
B[1,] <- A[m,]
B[m,] <- A[1,]
B
ii) #Part 2(ii)
#Creating function new to obtain matrix C,
new <- function()
{
#Initializing function variable C for the matrix
C <- 0
#loop and Conditional if function for additive condition
for (i in 1:m) {
if(i != 1)
{
Document Page
A[i,] = A[i,] + A[1,]
}
C <- A
}
}
#Returning the matrix C
C
iii) #Part 2 (iii)
#Looping and condtional if for scaling condition
for (i in 1:m) {
if(i %% 2 != 0)
{
A[i,] = A[i,] * A[i,1]
}
}
#Returning matrix D, scaled matrix of A
D <- A
D
Document Page
QUESTION 2
#Question Two
#Prompting the user to input the value of n for the parameter and number of columns for matrix
readinteger <- function()
{
n <- readline(prompt="Input n (n can only be 3 0r 4): ")
if(!grepl("^[0-9]+$",n))
{
return(readinteger())
}
return(as.integer(n))
}
#Conditional if for the parameter n = 3 and n = 4
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
if (n == 3)
{
basename = "matrix"
end = ".in"
filename = sprintf("%s%d%s",basename,3,end)
M <- dlmread(filename)
basename = "matrix"
end = ".in"
filename = sprintf("%s%d%s",basename,4,end)
M1 <- dlmread(filename)
} else
{
basename = "matrix"
end = ".in"
filename = sprintf("%s%d%s",basename,4,end)
M <- dlmread(filename)
Document Page
basename = "matrix"
end = ".in"
filename = sprintf("%s%d%s",basename,3,end)
M1 <- dlmread(filename)
}
#installing the package 'matlib' for solving for coefficients a and b
install.packages("matlib")
#loading the package 'matlib' for solving for coefficients a and b
library(matlib)
#Specifying the the pair of coeficients from the equations formed by n = 3 and n = 4
v <- M[1,]
w <- M[2,]
v1 <- M1[1,]
w1 <- M1[2,]
Document Page
#Specifying the the pair solution variables from the equations formed by n = 3 and n = 4
u <- M[3,]
u1 <- M1[3,]
#Forming the matrix for variables on the LHS
M3 <- martix(c(v,w,v1,w1), nrow = 2, ncol = 2)
M3
#Forming vector for values on the RHS
b<- c(u,u1)
b
#Forming vector to hold the initial coefficients a and b
c<-c(a,b)
c
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
#The equations to be solved simultaneously for a nd b
showEqn(M3,b)
#Assigning the solution for the equations to c
c <- Solve(M3, b, fractions = TRUE)
#Returning the values for a and b
a
b
Document Page
QUESTION THREE
#Question Three
#Prompting the user to input the value of n for the parameter2
readinteger <- function()
{
n <- readline(prompt="Input n (n can only be 0 0r 1): ")
if(!grepl("^[0-9]+$",n))
{
return(readinteger())
}
return(as.integer(n))
}
2 Smith, Larry. 1998.
Linear Algebra, Undergraduate Texts in Mathematics. Springer.
chevron_up_icon
1 out of 16
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]