summaryrefslogtreecommitdiff
path: root/Numerical_Methods_by_E_Balaguruswamy/CH9
diff options
context:
space:
mode:
Diffstat (limited to 'Numerical_Methods_by_E_Balaguruswamy/CH9')
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.1/Ex9_1.R40
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.11/Ex9_11.R24
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.12/Ex9_12.R22
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.2/Ex9_2.R40
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.3/Ex9_3.R29
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.4/Ex9_4.R38
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.5/Ex9_5.R39
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.8/Ex9_8.R58
-rw-r--r--Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.9/Ex9_9.R61
9 files changed, 351 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.1/Ex9_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.1/Ex9_1.R
new file mode 100644
index 00000000..9174a43d
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.1/Ex9_1.R
@@ -0,0 +1,40 @@
+#Example 1 Chapter 9 Page no.: 277
+# Polynomial Forms
+
+#Consider the power form of p(x) for n=1, p(x)=a0 + a1*X,
+
+#Given that p(100)= +3/7, p(101)= -4/7
+p_100= 3/7
+p_101= -4/7
+
+
+cat(paste(p_100,"is the given p(100) and",p_101,"is the given p(101)"))
+
+
+A<- matrix(c(1,100,1,101), nrow=2,ncol=2, byrow=T)
+A
+b<-matrix(c(p_100,p_101))
+b
+sol= solve(A,b)
+a0<-round(sol[1], 1)
+a1<-sol[2]
+print(paste("a0 is :",a0 ))
+print(paste("a1 is :",a1 ))
+
+#comparing calculated value with given ones
+print("power form equation : ")
+substitute(a0+a1*x, list(a0=sol[1], a1=sol[2]))
+
+#substitue x=100
+x1<-100
+substitute(a0+a1*x, list(a0=a0, a1=a1, x=x1))
+New_p100 = a0 + (a1*x1)
+#substitute x=101
+x2<-101
+substitute(a0+a1*x, list(a0=a0, a1=a1, x=x2))
+New_p101= a0 + (a1*x2)
+
+cat(paste(signif(New_p100,5),"is the new p(100) and", signif(New_p101,5),"is the new p(101)"))
+
+
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.11/Ex9_11.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.11/Ex9_11.R
new file mode 100644
index 00000000..31463be2
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.11/Ex9_11.R
@@ -0,0 +1,24 @@
+#Example 11 Chapter 9 Page no.: 306
+#Cubic splines
+
+#Installing and importing library 'pracma'
+install.packages("pracma")
+library("pracma")
+
+#Given Data
+mydata<-data.frame(i= c(0,1,2),
+ xi= c(4,9,16),
+ fi= c(2,3,4),
+stringsAsFactors = TRUE
+
+)
+mydata
+
+x<-mydata$xi
+f<-mydata$fi
+
+#function to find spline at xi
+C <- signif(cubicspline(x, f, xi = 7),5)
+
+cat("Cubic spline of f(7) is :",C)
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.12/Ex9_12.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.12/Ex9_12.R
new file mode 100644
index 00000000..715cf2d7
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.12/Ex9_12.R
@@ -0,0 +1,22 @@
+#Example 12 Chapter 9 Page no.: 313
+#Estimating Function values using Cubic splines.
+
+#Installing and importing library 'pracma'
+install.packages("pracma")
+library("pracma")
+
+#Given Data
+mydata<-data.frame(i= c(0,1,2,3),
+ xi= c(1,2,3,4),
+ fi= c(0.5, 0.3333, 0.25, 0.20),
+ stringsAsFactors = TRUE
+)
+mydata
+
+x<-mydata$xi
+f<-mydata$fi
+
+#function to find spline at xi
+C <- signif(cubicspline(x, f, xi = 2.5),5)
+
+cat("Cubic spline of f(2.5) is :",C) \ No newline at end of file
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.2/Ex9_2.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.2/Ex9_2.R
new file mode 100644
index 00000000..a7267959
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.2/Ex9_2.R
@@ -0,0 +1,40 @@
+#Example 2 Chapter 9 Page no.: 278
+# Shifted Power form
+
+#p(x)= a0+ a1(x-C)
+
+print("Given: ")
+C=100
+p_100= 3/7
+p_101= -4/7
+#solving for x=100 and x=101
+x1=100
+x2=101
+
+substitute(a0+a1(x-Centre), list(Centre=C))
+
+A<-matrix(c(1,(x1-C),1,c(x2-C)),nrow=2,ncol=2,byrow=T )
+A
+b<-matrix(c(p_100,p_101))
+b
+sol<-solve(A,b)
+
+a0=sol[1]
+a1=sol[2]
+
+substitute(a0+ a1*(x-Centre), list(a0=a0,a1=a1,Centre=C))
+#solving for new equations
+New_p100<- a0+ a1*(x1-C)
+cat("New p(100) is: ", signif(New_p100,5))
+New_p101<- a0+ a1*(x2-C)
+cat("New p(101) is: ", signif(New_p101,5))
+
+#Verifiying the new polynomial values obtained with older values")
+all.equal(New_p100,p_100)
+all.equal(New_p101,p_101)
+
+
+
+
+
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.3/Ex9_3.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.3/Ex9_3.R
new file mode 100644
index 00000000..11388f7a
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.3/Ex9_3.R
@@ -0,0 +1,29 @@
+#Example 3 Chapter 9 Page no.: 280
+#Determining square root using linear interpolation
+
+
+#GIven table
+mydata<- data.frame(x=c(1,2,3,4,5),
+ fx=c(sqrt(1), sqrt(2), sqrt(3), sqrt(4), sqrt(5)),
+ stringsAsFactors = TRUE
+)
+mydata
+
+val<-mydata$x
+fun<-mydata$fx
+
+#since 2.5 lie between 2 & 3;
+var=2.5
+x1<- val[2]
+x2<- val[3]
+f1<- fun[2]
+f2<- fun[3]
+
+Ans<- signif(f1 + (var-x1)*((f2-f1)/(x2-x1)),5)
+cat("Calculated square root of 2.5 is :", Ans)
+
+Original<- signif(sqrt(var),5)
+cat("Original square root of 2.5 is :", Original)
+
+error<- Original-Ans
+cat("Error is :", error)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.4/Ex9_4.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.4/Ex9_4.R
new file mode 100644
index 00000000..68eda725
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.4/Ex9_4.R
@@ -0,0 +1,38 @@
+#Example 4 Chapter 9 Page no.: 282
+#Determining square root using Lagrange's interpolation
+
+#Given Table
+mydata<- data.frame(x=c(1,2,3,4,5),
+ fx=c(sqrt(1), sqrt(2), sqrt(3), sqrt(4), sqrt(5)),
+ stringsAsFactors = TRUE
+)
+mydata
+
+val<-mydata$x
+fun<-mydata$fx
+
+x0<-val[2]
+x1<-val[3]
+x2<-val[4]
+
+f0<-fun[2]
+f1<-fun[3]
+f2<-fun[4]
+
+var=2.5
+
+I0<- ((var-x1)*(var-x2))/((x0-x1)*(x0-x2))
+I0
+I1<- ((var-x0)*(var-x2))/((x1-x2)*(x1-x0))
+I1
+I2<-((var-x0)*(var-x1))/((x2-x0)*(x2-x1))
+I2
+
+Ans<- signif((f0*I0) + (f1*I1) + (f2*I2),5)
+cat("Calculated square root of 2.5 is :", Ans)
+
+Original<- signif(sqrt(var),5)
+cat("Original square root of 2.5 is :", Original)
+
+error<- Original-Ans
+cat("Error is :", error)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.5/Ex9_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.5/Ex9_5.R
new file mode 100644
index 00000000..1813dcb5
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.5/Ex9_5.R
@@ -0,0 +1,39 @@
+#Example 5 Chapter 9 Page no.: 283
+#Lagrange's Polynomial
+
+# Installing and importing required packages
+
+install.packages("polynom")
+install.packages("stats")
+library("polynom")
+library("stats")
+
+#Given data
+mydata<- data.frame( i=c(0,1,2,3),
+ xi=c(0,1,2,3),
+ ex=c(0,1.7183,6.3891,19.0855),
+
+ stringsAsFactors = TRUE
+)
+mydata
+
+q<-poly.calc(mydata$xi, mydata$ex) #function to calculate lagrange interpolation polynomial
+q
+beta<-coef(q) #obtain coefficients
+beta
+y1<-beta[2]
+y2<-beta[3]
+y3<-beta[4]
+
+#substituting x=1.5 in above equation :
+x=1.5
+uno<-x
+cube<-x*x*x
+square<-x*x
+
+pot<-(y3*cube)+ (y2*square) + (y1*uno)
+pot
+Ans<- pot+1
+
+cat("Value of e^1.5 is :", signif(Ans,5))
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.8/Ex9_8.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.8/Ex9_8.R
new file mode 100644
index 00000000..af1fb019
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.8/Ex9_8.R
@@ -0,0 +1,58 @@
+#Example 8 Chapter 9 Page no.: 297
+#Newton-Gregory forward difference formula
+
+#Given Data
+mydata<- data.frame(t=c(10,20,30,40,50),
+ s=c(0.1736, 0.3420, 0.5000, 0.6428, 0.7660),
+ stringsAsFactors = TRUE
+)
+mydata
+
+theta<-mydata$t
+sin<- mydata$s
+s0<-sin[1]
+s1<-sin[2]
+s2<-sin[3]
+s3<-sin[4]
+s4<-sin[5]
+
+x0<-theta[1]
+x1<-theta[2]
+x2<-theta[3]
+x3<-theta[4]
+x4<-theta[5]
+
+h=x1-x0
+h
+
+x=25
+a1<- (x-x0)/h #for forward interpolation ,start from top
+a2<- (x-x1)/h
+a3<- (x-x2)/h
+a4<- (x-x3)/h
+
+f1<-s1-s0
+f2<-s2-s1
+f3<-s3-s2
+f4<-s4-s3
+delta1<- f2-f1
+delta2<- f3-f2
+delta3<- f4-f3
+
+get1<-delta1-delta2
+get2<-delta2-delta3
+
+last<- get2-get1
+
+p1<- s0+ a1*(f1)/1
+p1
+p2<- p1+ a1*a2*(delta1)/2
+p2
+p3<- p2+ a1*a2*a3*(get1)
+p3
+p4<- p3+ a1*a2*a3*a4*(last)
+p4
+
+cat("using Newton-Gregory forward difference formula value of sin(25) was obtained to be :",p4)
+
+#Differed value in textbook is due to approximation upto 4 decimal places.
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.9/Ex9_9.R b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.9/Ex9_9.R
new file mode 100644
index 00000000..ee3dd35e
--- /dev/null
+++ b/Numerical_Methods_by_E_Balaguruswamy/CH9/EX9.9/Ex9_9.R
@@ -0,0 +1,61 @@
+#Example 9 Chapter 9 Page no.: 299
+#Newton-Gregory backward difference formula
+
+#Given Data
+mydata<- data.frame(t=c(10,20,30,40,50),
+ s=c(0.1736, 0.3420, 0.5000, 0.6428, 0.7660),
+ stringsAsFactors = TRUE
+)
+mydata
+
+theta<-mydata$t
+sin<- mydata$s
+s0<-sin[1]
+s1<-sin[2]
+s2<-sin[3]
+s3<-sin[4]
+s4<-sin[5]
+
+x0<-theta[1]
+x1<-theta[2]
+x2<-theta[3]
+x3<-theta[4]
+x4<-theta[5]
+
+h=x1-x0
+h
+
+x=25
+a1<- (x-x4)/h #for backward interpolation ,start from bottom
+a2<- (x-x3)/h
+a3<- (x-x2)/h
+a4<- (x-x1)/h
+a5<- (x-x0)/h
+
+f1<-s1-s0
+f2<-s2-s1
+f3<-s3-s2
+f4<-s4-s3
+delta1<- f2-f1
+delta2<- f3-f2
+delta3<- f4-f3
+
+get1<-delta1-delta2
+get2<-delta2-delta3
+
+last<- get2-get1
+
+p1<- s4+ a1*(f4)/1
+p1
+p2<- p1+ a1*a2*(delta3)/2
+p2
+p3<- p2+ a1*a2*a3*(get2)
+p3
+p4<- p3+ a1*a2*a3*a4*(last)
+p4
+
+cat("using Newton-Gregory Backward difference formula value of sin(25) was obtained to be :",p4)
+
+#Differed value in textbook is due to approximation upto 4 decimal places.
+
+