summaryrefslogtreecommitdiff
path: root/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2
diff options
context:
space:
mode:
authorPrashant S2019-10-04 12:27:32 +0530
committerGitHub2019-10-04 12:27:32 +0530
commitac2986488a9731cff5cbb517d8f0ef98e2561d64 (patch)
tree7bb3f64824627ef179d5f341266a664fd0b69011 /Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2
parentcbb2770fb2f88246175add29623103a56ba338b8 (diff)
parentb3f3a8ecd454359a2e992161844f2fb599f8238a (diff)
downloadR_TBC_Uploads-master.tar.gz
R_TBC_Uploads-master.tar.bz2
R_TBC_Uploads-master.zip
Merge pull request #1 from prashantsinalkar/masterHEADmaster
Added R TBC
Diffstat (limited to 'Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2')
-rw-r--r--Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r67
1 files changed, 67 insertions, 0 deletions
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r
new file mode 100644
index 00000000..e14008d0
--- /dev/null
+++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r
@@ -0,0 +1,67 @@
+# Packages used : pracma
+# To install pracma,type following in command line while connected to internet
+# install.packages("pracma")
+# package can be included by command " library(pracma) "
+# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html
+
+# Example : 2 Chapter : 3.4 Page No: 158
+# Find the particular solution and special solution of the given system
+
+library(pracma)
+nullspacebasis <- function(A){
+ R<-rref(A)
+ m<-nrow(A)
+ n<-ncol(A)
+ pivotcol<-c() #vector to store the column numbers of pivot columns
+ freecol<-c() #vector to store the column numbers of free columns
+ i<-1
+ j<-1
+
+ # to find which columns are pivot and which are free
+ while(i<=m & j<=n){
+ if(R[i,j]==1){
+ pivotcol<-c(pivotcol,j)
+ i<-i+1
+ j<-j+1
+ }
+ else{
+ j<-j+1
+ }
+ }
+ y<-length(pivotcol)
+ freecol<-c(1:n)
+ freecol<-freecol[!freecol%in%pivotcol]
+ x<-length(freecol)
+ N<-c()
+ #find the basis for null space based on Row reduced echelon form of given matrix
+ if(y==n){
+ return(N)
+ }
+ for(i in 1:x){
+ temp<-c(1:n)
+ for(j in 1:x){
+ temp[freecol[j]]<-0
+ }
+ temp[freecol[i]]<-1
+ temp[freecol[i]]
+ for(j in 1:y){
+ temp[pivotcol[j]]<-R[j,freecol[i]]*-1
+ }
+ N<-c(N,temp)
+ }
+ N<-matrix(N,nrow=n,ncol=x)
+ #Basis for the nullspace of given matrix
+ return(N)
+}
+A<-matrix(c(1,1,1,2,1,-1),nrow=2)
+b<-c(3,4)
+s<-nullspacebasis(A)
+Ab<-cbind(A,b)
+Rd<-rref(Ab)
+p<-Rd[,4]
+p<-c(p,0)
+print("Particualar solution is")
+print(p)
+print("special solution is ")
+print(s)
+print("The complete solution = particular solution + special solution")