diff options
Diffstat (limited to 'Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4')
15 files changed, 399 insertions, 0 deletions
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.3/Ex4.1_3.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.3/Ex4.1_3.r new file mode 100644 index 00000000..be7a53ff --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.3/Ex4.1_3.r @@ -0,0 +1,10 @@ +# Example : 3 Chapter : 4.1 Page No: 197 +# Rows of given matrix are perpendicular to the vector in nullspace +A<-matrix(c(1,5,3,2,4,7),nrow=2) +x<-c(1,1,-1) +for(i in 1:nrow(A)){ + dot_product<-sum(A[i,]*x) + if(dot_product==0){ + print(paste("Row ",i,"is perpendicular to x")) + } +}
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.a/Ex4_4.1A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.a/Ex4_4.1A.r new file mode 100644 index 00000000..1bfe0abc --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.a/Ex4_4.1A.r @@ -0,0 +1,11 @@ +# Example : 4.1A Chapter : 4.1 Page No: 201 +# Dimensions of the subspaces in the given space +dim_R<-9 +dim_S<-6 +print("Possible dimensions of the subspaces orthogonal to S") +x<-dim_R-dim_S +orthogonal_dimensions<-c(0:x) +print(orthogonal_dimensions) +print(paste("possible dimensions of orthogonal complement subspaces to S",dim_R-dim_S)) +print(paste("The smallest matrix A in S is ",dim_S," by ",dim_R)) +print(paste("The Null space matrix N is ",dim_R," by ",dim_R-dim_S)) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.b/Ex4_4.1B.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.b/Ex4_4.1B.r new file mode 100644 index 00000000..21632e76 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.1.b/Ex4_4.1B.r @@ -0,0 +1,71 @@ +# 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 : 4.1B Chapter : 4.1 Page No: 201
+# Null space Basis of a plane subspace
+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,-3,-4),ncol=3)
+print("Given Plane x-3y-4z=0 is a null space of following 1*3 matrix")
+print(A)
+#to make matrix copatible for our function
+A<-rbind(A,c(0,0,0),c(0,0,0))
+N<-nullspacebasis(A)
+print("SPecial solutions or nullspace basis of given plane subspace is")
+print(N)
+A<-A[-c(2,3),]
+print("Row space is ")
+print(A)
+temp<-cbind(N,c(1,-3,-4))
+x<-c(1,1,-1)
+print("vector 6,4,5 is split into vn + vs as 1 of each vector in nullspace basis and -1 of rowspace basis")
+v<-temp%*%x
+print(v)
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.1/Ex4.2_1.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.1/Ex4.2_1.r new file mode 100644 index 00000000..84b15c6d --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.1/Ex4.2_1.r @@ -0,0 +1,23 @@ +# Example : 1 Chapter : 4.2 Page No: 208
+# Projection of the vector onto line
+
+#Answers to this problem are displayed in the form of x/y in textbook
+#Here the same answers are in decimal formats
+
+projection<-function(b,a){
+ xhat<-(sum(a*b))/(sum(a*a))
+ p<-xhat*a
+ return(p)
+}
+b<-c(1,1,1)
+a<-c(1,2,2)
+p<-projection(b,a)
+print("The projection vector p i.e., b on a is ")
+print(p)
+e<-b-p
+print("The error vector is ")
+print(e)
+if(sum(e*a)==0){
+ print("Vector e is perpendicular to a")
+}
+#The answer may slightly vary due to rounding off values
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.2/Ex4.2_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.2/Ex4.2_2.r new file mode 100644 index 00000000..134d20fa --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.2/Ex4.2_2.r @@ -0,0 +1,17 @@ +# Example : 2 Chapter : 4.2 Page No: 209
+# Find the projection matrix onto the line
+
+projection_matrix<-function(a){
+ a<-matrix(c(a),ncol=1)
+ P<-a%*%t(a)
+ temp<-t(a)%*%a
+ temp<-1/temp
+ t<-temp[1,1]
+ P<-t*P
+ return(P)
+}
+a<-c(1,2,2)
+P<-projection_matrix(a)
+print("The projection matrix is ")
+print(P)
+#The answer may slightly vary due to rounding off values
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.3/Ex4.2_3.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.3/Ex4.2_3.r new file mode 100644 index 00000000..7e621c84 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.3/Ex4.2_3.r @@ -0,0 +1,27 @@ +# Example : 3 Chapter : 4.2 Page No: 211
+# Find the best possible solution, projection vector and projection matrix
+
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ b<-matrix(c(b),ncol=1)
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ p<-A%*%xhat
+ e<-b-p
+ ATA1<-solve(ATA)
+ P<-A%*%ATA1
+ P<-P%*%t(A)
+ print("The best possible solution is ")
+ print(xhat)
+ print("The projection vector is ")
+ print(p)
+ print("The error vector e is")
+ print(e)
+ print("The projection matrix is ")
+ print(P)
+}
+
+A<-matrix(c(1,1,1,0,1,2),ncol=2)
+b<-c(6,0,0)
+solution(A,b)
+#The answer may slightly vary due to rounding off values
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.a/Ex4_4.2A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.a/Ex4_4.2A.r new file mode 100644 index 00000000..9d44f4f5 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.a/Ex4_4.2A.r @@ -0,0 +1,32 @@ +# Example : 4.2A Chapter : 4.2 Page No: 213
+# Projection onto the line and onto the plane
+
+projection_line<-function(a,b){
+ p<-((sum(a*b))/(sum(a*a)))*a
+ e<-b-p
+ print("The projection vector is ")
+ print(p)
+ print("The error vector is ")
+ print(e)
+}
+
+projection_plane<-function(A,b){
+ b<-matrix(c(b),ncol=1)
+ ATA<-t(A)%*%A
+ ATA1<-solve(ATA)
+ P<-A%*%ATA1
+ P<-P%*%t(A)
+ p<-P%*%b
+ e<-b-p
+ print("The projection vector is ")
+ print(p)
+ print("The error vector is ")
+ print(e)
+}
+
+b<-c(3,4,4)
+a<-c(2,2,1)
+projection_line(a,b)
+A<-matrix(c(a,1,0,0),ncol=2)
+projection_plane(A,b)
+#The answer may slightly vary due to rounding off values
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.b/Ex4_4.2B.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.b/Ex4_4.2B.r new file mode 100644 index 00000000..3fa8dab9 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.2.b/Ex4_4.2B.r @@ -0,0 +1,15 @@ +# Example : 4.2B Chapter : 4.2 Page No: 213
+# Find best Possible solution
+
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ x<-solve(ATA,ATb)
+ return(x)
+}
+
+A<-matrix(c(1,1,1),ncol=1)
+b<-matrix(c(70,80,120),ncol=1)
+x<-solution(A,b)
+print("The best possible solution is ")
+print(x)
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.1/Ex4.3_1.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.1/Ex4.3_1.r new file mode 100644 index 00000000..13017941 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.1/Ex4.3_1.r @@ -0,0 +1,27 @@ +# Example : 1 Chapter : 4.3 Page No: 218
+# Fit a straight line
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ return(xhat)
+}
+fit_line<-function(D){
+ num_of_points<-nrow(D)
+ t<-c()
+ for(i in 1:num_of_points){
+ t<-c(t,1)
+ }
+ t<-c(t,D[,1])
+ A<-matrix(c(t),ncol=2)
+ b<-D[,2]
+ b<-matrix(c(b),ncol=1)
+ x<-solution(A,b)# The system has no solution, we need to find the best solution
+ return(x)
+}
+Data<-matrix(c(0,6,1,0,2,0),ncol=2,byrow=T)
+x<-fit_line(Data)
+C<-x[1]
+D<-x[2]
+print(paste("The best straight line is b= ",C," + ",D,"t"))
+
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.2/Ex4.3_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.2/Ex4.3_2.r new file mode 100644 index 00000000..59596010 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.2/Ex4.3_2.r @@ -0,0 +1,27 @@ +# Example : 2 Chapter : 4.3 Page No: 222
+# Fit a straight line
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ return(xhat)
+}
+fit_line<-function(D){
+ num_of_points<-nrow(D)
+ t<-c()
+ for(i in 1:num_of_points){
+ t<-c(t,1)
+ }
+ t<-c(t,D[,1])
+ A<-matrix(c(t),ncol=2)
+ b<-D[,2]
+ b<-matrix(c(b),ncol=1)
+ x<-solution(A,b)# The system has no solution, we need to find the best solution
+ return(x)
+}
+Data<-matrix(c(-2,0,2,1,2,4),ncol=2)
+x<-fit_line(Data)
+C<-x[1]
+D<-x[2]
+print(paste("The best straight line is b= ",C," + ",D,"t"))
+#The answer may slightly vary due to rounding off values.
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.3/Ex4.3_3.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.3/Ex4.3_3.r new file mode 100644 index 00000000..37f87356 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.3/Ex4.3_3.r @@ -0,0 +1,29 @@ +# Example : 3 Chapter : 4.3 Page No: 224
+# Fit a Parabola
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ return(xhat)
+}
+fit_parabola<-function(D){
+ num_of_points<-nrow(D)
+ t<-c()
+ for(i in 1:num_of_points){
+ t<-c(t,1)
+ }
+ t<-c(t,D[,1])
+ t<-c(t,D[,1]*D[,1])
+ A<-matrix(c(t),ncol=3)
+ b<-D[,2]
+ b<-matrix(c(b),ncol=1)
+ x<-solution(A,b)# The system has no solution, we need to find the best solution
+ return(x)
+}
+Data<-matrix(c(0,1,2,6,0,0),ncol=2)
+x<-fit_parabola(Data)
+C<-x[1]
+D<-x[2]
+E<-x[3]
+print(paste("The best Parabola that fitt in is b= ",C,"+",D,"t+",E,"t2"))
+
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.a/Ex4_4.3A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.a/Ex4_4.3A.r new file mode 100644 index 00000000..7cc53f33 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.a/Ex4_4.3A.r @@ -0,0 +1,31 @@ +# Example : 4.3A Chapter : 4.3 Page No: 225
+# Fit a straight line
+
+#1,2,3 solutions can be done without any need of computation
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ return(xhat)
+}
+fit_line<-function(D){
+ num_of_points<-nrow(D)
+ t<-c()
+ for(i in 1:num_of_points){
+ t<-c(t,1)
+ }
+ t<-c(t,D[,1])
+ A<-matrix(c(t),ncol=2)
+ b<-D[,2]
+ b<-matrix(c(b),ncol=1)
+ x<-solution(A,b)# The system has no solution, we need to find the best solution
+ return(x)
+}
+t<-c(1:10)
+b<-c(0,0,0,0,0,0,0,0,0,40)
+Data<-matrix(c(t,b),ncol=2)
+x<-fit_line(Data)
+C<-x[1]
+D<-x[2]
+print(paste("The best straight line is b= ",C," + ",D,"t"))
+#The answer may slightly vary due to rounding off values.
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.b/Ex4_4.3B.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.b/Ex4_4.3B.r new file mode 100644 index 00000000..33ed77c2 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.3.b/Ex4_4.3B.r @@ -0,0 +1,31 @@ +# Example : 4.3B Chapter : 4.3 Page No: 226
+# Fit a Parabola
+solution<-function(A,b){
+ ATA<-t(A)%*%A
+ ATb<-t(A)%*%b
+ xhat<-solve(ATA,ATb)
+ return(xhat)
+}
+fit_parabola<-function(D){
+ num_of_points<-nrow(D)
+ t<-c()
+ for(i in 1:num_of_points){
+ t<-c(t,1)
+ }
+ t<-c(t,D[,1])
+ t<-c(t,D[,1]*D[,1])
+ A<-matrix(c(t),ncol=3)
+ b<-D[,2]
+ b<-matrix(c(b),ncol=1)
+ x<-solution(A,b)# The system has no solution, we need to find the best solution
+ return(x)
+}
+t<-c(-2,-1,0,1,2)
+b<-c(0,0,1,0,0)
+Data<-matrix(c(t,b),ncol=2)
+x<-fit_parabola(Data)
+C<-x[1]
+D<-x[2]
+E<-x[3]
+print(paste("The best Parabola that fitt in is b= ",C,"+",D,"t+",E,"t2"))
+#The answer may slightly vary due to rounding off values.
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.4/Ex4.4_4.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.4/Ex4.4_4.r new file mode 100644 index 00000000..6574a53d --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.4/Ex4.4_4.r @@ -0,0 +1,21 @@ +# Example : 4 Chapter : 4.4 Page No: 233
+# Projections of the vector onto line,plane if basis are given as orthonormal vectors
+Q<-matrix(c(-1,2,2,2,-1,2,2,2,-1),ncol=3)
+Q<-(1/3)*Q
+q1<-Q[,1]
+q2<-Q[,2]
+q3<-Q[,3]
+b<-c(0,0,1)
+p1<-sum(q1*b)*q1
+p2<-sum(q2*b)*q2
+p3<-sum(q3*b)*q3
+print("Projection of b onto q1")
+print(p1)
+print("Projection of b onto q2")
+print(p2)
+print("Projection of b onto q3")
+print(p3)
+print("Projection of b onto plane of q1 and q2")
+print(p1+p2)
+print("Projection of b onto space of q1,q2, and q3")
+print(p1+p2+p3) # same as vector b
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.5/Ex4.4_5.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.5/Ex4.4_5.r new file mode 100644 index 00000000..f94e8c09 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH4/EX4.4.5/Ex4.4_5.r @@ -0,0 +1,27 @@ +# Example : 5 Chapter : 4.4 Page No: 233
+# Gram-Schmidt method to convert matrix into its orthogonal form
+magnitude<-function(a){
+ x<-0
+ for(i in 1:length(a)){
+ x<-x+a[i]*a[i]
+ }
+ x<-sqrt(x)
+ return(x)
+}
+a<-c(1,-1,0)
+b<-c(2,0,-2)
+c<-c(3,-3,3)
+A<-a
+B<-b-(sum(A*b)/sum(A*A))*A
+C<-c-(sum(A*c)/sum(A*A))*A-(sum(B*c)/sum(B*B))*B
+print("Orthogonal vectors corresponding to a,b,c are")
+print(A)
+print(B)
+print(C)
+q1<-(1/magnitude(A))*A
+q2<-(1/magnitude(B))*B
+q3<-(1/magnitude(C))*C
+Q<-matrix(c(q1,q2,q3),ncol=3)
+print("Orthogonal matrix with orthonormal vectors of a,b,c ")
+print(Q)
+#The answer may slightly vary due to rounding off values
\ No newline at end of file |