diff options
Diffstat (limited to 'Numerical_Methods_by_E_Balaguruswamy')
57 files changed, 1670 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH10/EX10.1/Ex10_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH10/EX10.1/Ex10_1.R new file mode 100644 index 00000000..1dfe5184 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH10/EX10.1/Ex10_1.R @@ -0,0 +1,30 @@ +# Example 1 Chapter 10 Page no.: 326
+# Straight Line fitting
+
+x<-c(1,2,3,4,5) # Define table
+y<-c(3,4,5,6,8)
+#Square of X values
+x2<-x^2
+#Product of X and Y
+xy<-x*y
+#Sum of X
+sx<-sum(x)
+#Sum of y
+sy<-sum(y)
+#Sum of x^2
+sx2<-sum(x2)
+#Sum of x*y
+sxy<-sum(xy)
+# number of elements
+n =5
+# calculating slope
+b <- ((n*sxy) -(sx*sy))/((n*sx2)-(sx^2))
+# calculating intercept
+a <- (sy)/n - (b*(sx))/n
+
+cat("The Linear equation y=",a,"+",b,"x")
+
+#Plotting curve and fitting the Straight line
+plot(x,y)
+
+abline(a=a,b=b)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.5/Ex11_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.5/Ex11_5.R new file mode 100644 index 00000000..78868bc6 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.5/Ex11_5.R @@ -0,0 +1,39 @@ +# Example 5 Chapter 11 Page no.: 358
+# Estimating velocity
+
+T <- c(5,6,7,8,9) # Time
+S <- c(10.0,14.5,19.5,25.5,32.0) # Distance
+
+#distance time function
+ss <- function(t1){
+ s <- S[t1-4]
+ return(s)
+}
+
+#Difference value h
+h1 <- 1
+
+# We have velocity as the first derivative of distance
+v <- function(t,h){
+ return(((-3*ss(t))+(4*ss(t+h))-ss(t+2*h))/(2*h))
+}
+
+#First given time is 5s
+t2 <- 5
+cat("Velocity of car at t=5s is",v(t2,h1),"km/s")
+
+# Central difference formula gives
+vcf <- function(t,h){
+ return((ss(t+h)-ss(t-h))/(2*h))
+}
+#second time given is 7s
+t3 <- 7
+cat("Velocity of car at t=",t3,"s is",vcf(t3,h1),"km/s")
+
+# Backward diffeerence formula
+vb <- function(t,h){
+ return(((3*ss(t))-(4*ss(t-h))+ss(t-2*h))/(2*h))
+}
+#Third given time is 9s
+t4 <- 9
+cat("Velocity of car at t=",t4,"s is",vb(t4,h1),"km/s")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.6/Ex11_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.6/Ex11_6.R new file mode 100644 index 00000000..fb55e716 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.6/Ex11_6.R @@ -0,0 +1,25 @@ +# Example 6 Chapter 11 Page no.: 359
+# Estimating Acceleration
+
+T <- c(5,6,7,8,9) # Time
+S <- c(10.0,14.5,19.5,25.5,32.0) # Distance
+
+#distance time function
+ss <- function(t1){
+ s <- S[t1-4]
+ return(s)
+}
+
+#Difference value h
+h1 <- 1
+
+#Acceleration is given by second derivative of distance
+
+a <- function(t,h){
+ return((ss(t+h)-(2*ss(t))+ss(t-h))/(h^2))
+}
+
+h1 <- 1
+t1 <- 7 # given time
+
+cat("Acceleration of car at t=",t1,"s is",a(t1,h1),"km/s^2")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.8/Ex11_8.R b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.8/Ex11_8.R new file mode 100644 index 00000000..fad41028 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH11/EX11.8/Ex11_8.R @@ -0,0 +1,40 @@ +# Example 8 Chapter 11 Page no.: 364
+# Richardson Extrapolation
+
+f <- function(x){
+ return(exp(x))
+}
+h1<- 0.5
+r1 <- 1/2
+x1<- 0.5
+
+#First order forward difference formula
+D1 <- function(x,h) {
+ return((f(x+h) - f(x-h))/(2*h))
+}
+
+Dr1 <- function(x,r,h){
+ return((f(x+(r*h)) - f(x-(r*h)))/(2*r*h))
+}
+
+f1 <- function(x,r,h){
+ return((Dr1(x,r,h)-(r^2)*D1(x,h))/(1-(r^2)))
+}
+F <- signif(f1(x1,r1,h1), digits = 5)
+
+cat("The value of function e^x at",x1," with parameters")
+cat("h=",h1)
+cat("r=",r1)
+cat("is",F)
+
+# The value deviates slighly from the value of the textbook becaues of approximation in the text
+
+r2 <- 2
+F1 <- signif(f1(x1,r2,h1), digits = 5)
+
+cat("The value of function e^x at",x1," with parameters")
+cat("h=",h1)
+cat("r=",r2)
+cat("is",F1)
+
+cat(" This shows that estimate with r=",r1,"is better than the estimate with r=",2)
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.10/Ex12_10.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.10/Ex12_10.R new file mode 100644 index 00000000..30ce157f --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.10/Ex12_10.R @@ -0,0 +1,22 @@ +# Example 10 Chapter 12 Page no.: 400
+#Gauss-Legendre Three point formula
+
+# Installing and attaching 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#Gauss-Legendre nodes and weights
+f <- function(x) {
+ (x^4)+1
+}
+
+# Given values
+n1 = 3
+a1 = 2
+b1 = 4
+
+cc <- gaussLegendre(n1, a1, b1)
+Q <- sum(cc$w * f(cc$x))
+cat("The Gauss-Legendre Three point integral of (x^4)+1 is", Q)
+
+# The value in the textbook and the calculated value vary slightly due to approximations assumed in textbook.
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.3/Ex12_3.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.3/Ex12_3.R new file mode 100644 index 00000000..afc5ab90 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.3/Ex12_3.R @@ -0,0 +1,39 @@ +# Example 3 Chapter 12 Page no.: 381
+# Simpson's 1/3 Rule
+
+#CASE A
+
+#Given Function
+f <- function(x){
+ exp(x)
+}
+
+a1 <- -1
+b1 <- 1
+h1 <- (b1-a1)/2
+
+Is <- function(a,b,h){
+ return((h/3)*(f(a)+f(b)+4*f((a+b)/2)))
+}
+
+Is1 <- Is(a1,b1,h1)
+cat("The Integrant value of e^x between -1 and 1 is", Is1)
+
+
+#CASE B
+
+#Given function
+ff <- function(x){
+ sqrt(sin(x))
+}
+
+a2 <- 0
+b2 <- pi/2
+h2 <- (b2-a2)/2
+
+Isp <- function(k,l,m){
+ return((m/3)*(ff(k)+ff(l)+4*ff((k+l)/2)))
+}
+
+Is2 <- Isp(a2,b2,h2)
+cat("The Integrant value of sqrt(sin(x)) between 0 and pi/2 is", Is2)
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.5/Ex12_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.5/Ex12_5.R new file mode 100644 index 00000000..eb7c657d --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.5/Ex12_5.R @@ -0,0 +1,40 @@ +# Example 5 Chapter 12 Page no.: 386
+#Simpson's 3/8 Rule
+
+#CASE A
+#Given Function
+f <- function(x){
+ return((x^3)+1)
+}
+
+a1 <- 1
+b1 <- 2
+h1 <- (b1-a1)/3 #Since there are 4 sampling points the value of n is 3
+
+# Simpson's 3/8 function
+
+Is <- function(a,b,h){
+ return(((3*h)/8)*(f(a)+f(b)+(3*f((a+h)))+(3*f((a+(2*h))))))
+}
+
+Is1 <- Is(a1,b1,h1)
+cat("The Integrant value of (x^3)+1 between 1 and 2 is", Is1)
+
+# CASE B
+# Given Function
+ff <- function(x){
+ return(sqrt(sin(x)))
+}
+
+a2 <- 0
+b2 <- pi/2
+h2 <- (b2-a2)/3 #Since there are 4 sampling points the value of n is 3
+
+# Simpson's 3/8 function
+
+IS <- function(a,b,h){
+ return(((3*h)/8)*(ff(a)+ff(b)+(3*ff((a+h)))+(3*ff((a+(2*h))))))
+}
+
+IS1 <- IS(a2,b2,h2)
+cat("The Integrant value of sqrt(sin(x)) between 0 and pi/2 is", signif(IS1, digits = 6))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.6/Ex12_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.6/Ex12_6.R new file mode 100644 index 00000000..c2fa7c5e --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.6/Ex12_6.R @@ -0,0 +1,20 @@ +# Example 6 Chapter 12 Page no.: 387
+#Boole's five point Formula
+
+#Given function
+f <- function(x){
+ sqrt(sin(x))
+}
+
+a1 <- 0
+b1 <- pi/2
+h1 <- (b1-a1)/4 #Since there are 5 sampling points the value of n is 4
+
+# Boole's function
+
+Bl <- function(a,b,h){
+ return(((2*h)/45)*((7*f(a))+(7*f(b))+(32*f(a+h))+(12*f(a+(2*h)))+(32*f(a+(3*h)))))
+}
+
+BL1 <- Bl(a1,b1,h1)
+cat("The Boole integrant value of sqrt(sin(x)) between 0 and pi/2 is", signif(BL1, digits = 6))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.7/Ex12_7.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.7/Ex12_7.R new file mode 100644 index 00000000..45c19d99 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.7/Ex12_7.R @@ -0,0 +1,16 @@ +# Example 7 Chapter 12 Page no.: 391
+# Romberg Estimation
+
+# Installing and importing 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#Given function
+f <- function(x){
+ return(1/x)
+}
+
+#Romberg function
+u <- romberg(f,1 ,2)
+
+cat("The value of Romberg integration of 1/x is",u$value,"and relative error is",u$rel.error,"completed in",u$iter,"iterations" )
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.8/Ex12_8.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.8/Ex12_8.R new file mode 100644 index 00000000..a7d66c5d --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.8/Ex12_8.R @@ -0,0 +1,17 @@ +# Example 8 Chapter 12 Page no.: 397
+#Gauss-Legendre Two point formula
+
+#Given functions and values
+f <- function(x){
+ exp(x)
+}
+a1 <- -1
+b1 <- 1
+
+#Gauss-Legendre Two point formula
+I <- function(a,b){
+ return(f(a/sqrt(3))+f(b/sqrt(3)))
+}
+
+I1 <- I(a1,b1)
+cat("The Gauss-Legendre two point integral of exp(x) is", signif(I1, digits = 8))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.9/Ex12_9.R b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.9/Ex12_9.R new file mode 100644 index 00000000..c6d4b401 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH12/EX12.9/Ex12_9.R @@ -0,0 +1,20 @@ +# Example 9 Chapter 12 Page no.: 398
+#Gaussian Two point formula
+
+# Installing and attaching 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#Gauss-Legendre nodes and weights
+f <- function(x) {
+ exp(-x/2)
+}
+
+# Given values
+n1 = 2
+a1 =-2
+b1 = 2
+
+cc <- gaussLegendre(n1, a1, b1)
+Q <- sum(cc$w * f(cc$x))
+cat("The Gaussian two point integral of exp(-x/2) is", signif(Q, digits = 9))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.10/Ex13_10.R b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.10/Ex13_10.R new file mode 100644 index 00000000..320eae02 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.10/Ex13_10.R @@ -0,0 +1,53 @@ +# Example 10 Chapter 13 Page no.: 446
+# Milne-Simpson predictor-corrector method
+
+#for the solution
+cat(" Milne and Simpson method")
+h <- 0.25
+y <- c()
+x <- c()
+x[1] = 1
+y[1] = 2
+
+cat("h=",h)
+cat("x=",x)
+cat("y=",y)
+
+f <- function(x,y){
+ 2 * y / x
+}
+
+#Creating algorithm and displaying
+for (i in 2:10) {
+
+ print(signif(y[i-1],3))
+ m1 <- f(x[i-1],y[i-1])
+ m2 <- f(x[i-1] + 0.5*h, y[i-1] + 0.5*h*m1)
+ m3 <- f(x[i-1] + 0.5*h, y[i-1] + 0.5*h*m2)
+ m4 <- f(x[i-1] + h, y[i-1] + h*m3)
+ x[i] <- x[i-1] + h
+ y[i] <- y[i-1] + h * (m1 + m4 + 2*m2 + 2*m3) / 6
+
+
+}
+
+g <- c()
+
+for (i in 1:3) {
+ g[i] <- f(x[i+1],y[i+1])
+ print(g[i])
+
+}
+
+#Milne's formula
+Y1 <- y[1] + 4*h * (2*g[1] - g[2] + 2*g[3]) /3
+cat("Solution obtained using Milne's method is", Y1,3)
+
+g[4] <- f(x[5],Y1)
+print(g[4])
+
+#Simpson formula
+Y2 <- y[3] + h * ( g[2] + 4*g[3] + g[4]) /3
+cat("Solution obtained using Simpson's method is", Y2,3)
+
+#Values are not approximated here ao value slightly differs from the textbook value
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.4/Ex13_4.R b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.4/Ex13_4.R new file mode 100644 index 00000000..22d907da --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.4/Ex13_4.R @@ -0,0 +1,40 @@ +# Example 4 Chapter 13 Page no.: 420
+# Euler Method
+
+#for the first solution
+cat("Case A:")
+h <- 0.5
+x <- 1
+y <- 2
+cat("h=",h)
+#Creating algorithm and displaying
+for (i in 1:2) {
+
+ dy <- (3 * (x *x)) + 1
+ y <- y + (h * dy)
+ x <- x + h
+ cat("y(",x,")= ",y,"\n")
+}
+
+#for the second solution
+cat("Case B:")
+h <- 0.25
+x <- 1
+y <- 2
+cat("h=",h)
+
+print(x)
+print(y)
+
+#Creating algorithm and displaying
+for (i in 1:5) {
+
+ cat("y(",x,")= ",y,"\n")
+ x <- x + h
+ dy <- (3 * ((x-h)^2)) + 1
+ y <- y + (h * dy)
+
+
+}
+
+#For Case B, textbook answer is false.
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.6/Ex13_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.6/Ex13_6.R new file mode 100644 index 00000000..63140ef0 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.6/Ex13_6.R @@ -0,0 +1,43 @@ +# Example 6 Chapter 13 Page no.: 427
+# Heun Method and Euler Method
+
+#for the solution - Euler method
+cat("Case A: Euler's Method")
+h <- 0.25
+x <- 1
+y <- 2
+cat("h=",h)
+cat("x=",x)
+cat("y=",y)
+
+#Creating euler algorithm and displaying
+for (i in 1:5) {
+
+ cat("y(",x,")= ",y,"\n")
+ x <- x + h
+ f <- (2 * y / (x-h))
+ y <- y + (h * f)
+
+}
+
+#for the solution - Heun's method
+cat("Case B: Heun's Method")
+h <- 0.25
+x <- 1
+y <- 2
+cat("h=",h)
+
+#Creating heun algorithm and displaying
+for (i in 1:4) {
+
+
+ f1 <- (2 * y / (x))
+ m <- y + h*f1
+ f2 <- (2 * m / (x+h))
+ y <- y + ((h/2) * (f1 + f2))
+ x <- x + h
+ cat("y(",x,")= ",y,"\n")
+
+}
+
+#Values slightly differ but are more precise than what is given in the textbook
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.7/Ex13_7.R b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.7/Ex13_7.R new file mode 100644 index 00000000..f5a9d53e --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.7/Ex13_7.R @@ -0,0 +1,21 @@ +# Example 7 Chapter 13 Page no.: 433
+# Polygon Method
+
+#for the solution
+cat("Polygon Method")
+h <- 0.25
+x = 1
+y = 2
+cat("h=",h)
+cat("x=",x)
+cat("y=",y)
+
+#Creating algorithm and displaying
+for (i in 1:3) {
+
+ cat("y(",x,")= ",signif(y,3),"\n")
+ f1 <- (2 * y / x)
+ f2 <- (2 * (y + ( h * f1 / 2 ) ) / (x + ( h / 2 ) ))
+ x <- x + h
+ y <- y + (h * f2)
+}
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.8/Ex13_8.R b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.8/Ex13_8.R new file mode 100644 index 00000000..40f50985 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH13/EX13.8/Ex13_8.R @@ -0,0 +1,30 @@ +# Example 8 Chapter 13 Page no.: 439
+# Classic Runge Kutta Method
+
+#for the solution
+cat("Classic Runge Kutta Method")
+h <- 0.2
+x = 0
+y = 0
+
+cat("h=",h)
+cat("x=",x)
+cat("y=",y)
+
+#Given Function
+f <- function(x,y){
+ x^2 + y^2
+}
+
+#Creating algorithm and displaying
+for (i in 1:2) {
+
+ m1 <- f(x,y)
+ m2 <- f(x + 0.5*h, y + 0.5*h*m1)
+ m3 <- f(x + 0.5*h, y + 0.5*h*m2)
+ m4 <- f(x + h, y + h*m3)
+ x <- x + h
+ y <- y + h * (m1 + m4 + 2*m2 + 2*m3) / 6
+ cat("y(",x,")= ", signif(y,6),"\n")
+
+}
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH14/EX14.6/Ex14_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH14/EX14.6/Ex14_6.R new file mode 100644 index 00000000..a8f0c2ac --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH14/EX14.6/Ex14_6.R @@ -0,0 +1,25 @@ +# Example 6 Chapter 14 Page no.: 478
+# Power method
+
+# Include "matlib" library
+install.packages("matlib")
+library(matlib)
+
+#Given Matrix
+A<-matrix(c(1,2,0,2,1,0,0,0,-1),nrow = 3,ncol = 3,byrow = TRUE)
+A
+eigen<-powerMethod(A)
+
+EigenVector<-(eigen$vector[,1]-eigen$vector[3,1])
+
+EigenVector<-matrix(EigenVector,nrow = 3,ncol = 1,byrow = TRUE)
+
+EigenVector<-(EigenVector/EigenVector[1]) #This is Eigen vector Corresponding to largest Eigen Value.
+
+EigenValue<-eigen$value #This is Largest Eigen Value.
+
+sprintf("The largest eigen value Lampda1=%g",EigenValue)
+
+print("The Eigen Vector corresponding to eigen value")
+
+print(EigenVector)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.1/Ex3_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.1/Ex3_1.R new file mode 100644 index 00000000..30dba568 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.1/Ex3_1.R @@ -0,0 +1,32 @@ +# Example 1 Chapter 3 Page no.: 45
+# Binary number to Decimal
+
+#Importing 'DescTools' library
+#install.packages("DescTools")
+library(DescTools)
+
+a <- 1101 # Integer part of binary number
+b <- 0.1101 # Decimal part of binary number
+
+# conversion of Decimal part of binary
+f <- function(x){
+ length(gregexpr("[[:digit:]]", as.character(x))[[1]])
+}
+
+k <- f(b) -1
+A <- ((b %% 1)*(10^k))
+# Converting into vector
+A <- as.numeric(strsplit(as.character(A), "")[[1]])
+m <- length(A)
+d <- 0.5
+p1 <- c()
+for (i in 1:(m+1)) {
+ p <- signif(A[i]*d, digits = 10)
+ p1[i] <- p
+ p <- sum(p1, na.rm = TRUE)
+ d <- d*0.5
+}
+
+D <- p + BinToDec(a)
+
+cat(" The Decimal equivalent of 1101.1101 is",D)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.10/Ex3_10.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.10/Ex3_10.R new file mode 100644 index 00000000..c8973134 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.10/Ex3_10.R @@ -0,0 +1,12 @@ +# Example 10 Chapter 3 Page no.: 52
+# Floating point notation
+
+x <- 0.00596
+y <- 65.7452
+z <- -486.8
+
+cat(x,"is expressed as 0.596*10^(-2)")
+
+cat(y,"is expressed as 0.657452*10^(2)")
+
+cat(z,"is expressed as -0.4868*10^(3)")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.11/Ex3_11.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.11/Ex3_11.R new file mode 100644 index 00000000..8d21e8a1 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.11/Ex3_11.R @@ -0,0 +1,12 @@ +# Example 11 Chapter 3 Page no.: 53
+# Integer Arithmetics
+
+a <- 25
+b <- 12
+
+cat("Addition :",a,"+",b,"=",a+b)
+cat("Subtraction :",a,"-",b,"=",a-b)
+cat(" :",b,"-",a,"=",b-a)
+cat("Multiplication :",a,"x",b,"=",a*b)
+cat("Division :",a,"/",b,"=",as.integer(a/b))
+cat("Division :",b,"/",a,"=",as.integer(b/a))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.12/Ex3_12.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.12/Ex3_12.R new file mode 100644 index 00000000..90b861a7 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.12/Ex3_12.R @@ -0,0 +1,18 @@ +# Example 12 Chapter 3 Page no.: 53
+# Aritmetic rule
+
+cat(" Let a=5,b=7 and c=3")
+
+a=5
+b=7
+c=3
+
+k <- (a+b)/c
+j <- as.integer(a/c)+as.integer(b/c)
+
+if(k != j){
+ cat("(a+b)/c =",k,"\nWhich is not equal to (a/c)+(b/c)=",j)
+ cat(". \nThus, this Arithmetic rule fails.")
+}else{
+ cat("Both the results are same")
+}
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.2/Ex3_2.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.2/Ex3_2.R new file mode 100644 index 00000000..617ac2bc --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.2/Ex3_2.R @@ -0,0 +1,6 @@ +# Example 2 Chapter 3 Page no.: 46
+# COnversion of Hexadecimal number to Decimal number
+
+x<-'12af' #input value
+a<-strtoi(x,16L)
+cat("The Hex number 12AF is",a,"in Decimal system")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.4/Ex3_4.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.4/Ex3_4.R new file mode 100644 index 00000000..a414df5a --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.4/Ex3_4.R @@ -0,0 +1,8 @@ +# Example 4 Chapter 3 Page no.: 48
+# COnversion of Decimal number to Octal number
+
+x<-163
+a<-as.octmode(x)
+cat("The number 163 is ")
+a
+cat("in Octal system")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.6/Ex3_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.6/Ex3_6.R new file mode 100644 index 00000000..e1dfbb14 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.6/Ex3_6.R @@ -0,0 +1,21 @@ +# Example 6 Chapter 3 Page no.: 49
+# Octal Number to Hexadecimal
+
+
+oct <- 243 # Octal number
+decimalNumber <- 0
+i=0
+
+#Algorithm for Conversion of octal to decimal
+while(oct != 0)
+{
+ decimalNumber = decimalNumber + (oct %% 10)*(8^i)
+ i =i+1
+ oct = as.integer(oct/10)
+}
+
+H <- as.hexmode(decimalNumber[1])
+
+cat("The number 243 in Octal system is")
+H
+cat("in Hexadecimal system.")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.8/Ex3_8.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.8/Ex3_8.R new file mode 100644 index 00000000..4fdb5091 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.8/Ex3_8.R @@ -0,0 +1,12 @@ +# Example 8 Chapter 3 Page no.: 50
+# COnversion of integer to Binary
+
+
+x <- -13
+
+install.packages("binaryLogic")
+library("binaryLogic")
+
+y <- as.binary(x, size=0.7, n=0, logic=FALSE)
+
+cat("The integer",x,"can be represented as",print(y),"in binary format")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.9/Ex3_9.R b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.9/Ex3_9.R new file mode 100644 index 00000000..605a63de --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH3/EX3.9/Ex3_9.R @@ -0,0 +1,12 @@ +# Example 9 Chapter 3 Page no.: 51
+# COnversion of integer to 16 bit Binary
+
+
+x <- -32768
+
+install.packages("binaryLogic")
+library("binaryLogic")
+
+y <- as.binary(x, size=2, n=0, logic=FALSE)
+
+cat("The integer",x,"can be represented as",print(y),"in 16 Bit binary format")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.10/Ex4_10.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.10/Ex4_10.R new file mode 100644 index 00000000..b2d8c768 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.10/Ex4_10.R @@ -0,0 +1,35 @@ +# Example 10 Chapter 4 Page no.: 77
+# Error propagation
+
+# Given Values
+x <- 2.35
+y <- 6.74
+z <- 3.45
+
+#Given Arithmetic operation
+w <- x*y +z
+
+#Roundoff error in x,y &z
+erx <- 5E-3
+ery <- 5E-3
+erz <- 5E-3
+
+cat("Roundoff error in x,y &z is",erx,",",ery,"&",erz)
+
+#Error in x,y&z
+
+ex <- x*erx
+ey <- y*ery
+ez <- z*erz
+cat("Error in x=",ex)
+cat("Error in y=",ey)
+cat("Error in z=",ez)
+
+# Error in x*y
+exy <- x*ey + y*ex
+cat("Absolute Error in x*y=",exy)
+
+# Absolute error in computing w
+ew <- exy + ez
+cat("Absolute error in computing w=",ew)
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.3/Ex4_3.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.3/Ex4_3.R new file mode 100644 index 00000000..328cea79 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.3/Ex4_3.R @@ -0,0 +1,29 @@ +# Example 3 Chapter 4 Page no.: 64
+# Error in Conversion
+
+# Given Binary values
+x0.1 <- 0.00011001
+x0.4 <- 0.01100110
+
+#length of number
+f <- function(x){
+ length(gregexpr("[[:digit:]]", as.character(x))[[1]])
+}
+
+S <- x0.4 + x0.1
+k <- f(S) -1
+A <- ((S %% 1)*(10^k))
+# Converting into vector
+A <- as.numeric(strsplit(as.character(A), "")[[1]])
+m <- length(A)
+d <- 0.5
+p1 <- c()
+for (i in (m+1):1) {
+ p <- signif(A[i]*d, digits = 10)
+ p1[i] <- p
+ p <- sum(p1, na.rm = TRUE)
+ d <- d*0.5
+}
+cat("The decimal obtained when summing the binary is:",p)
+cat("But the true value is 0.5 when 0.4 and 0.1 are added together.")
+cat("Error:",0.5-p)
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.4/Ex4_4.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.4/Ex4_4.R new file mode 100644 index 00000000..9da6415f --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.4/Ex4_4.R @@ -0,0 +1,17 @@ +# Example 4 Chapter 4 Page no.: 66
+# Roundoff error
+
+x <- 752.6835 #Given value
+
+tx1 <- 0.7526*(10^3) #using 4 mantissa to store value
+tx2 <- 0.835*(10^-1) #using 4 mantissa to store value
+
+Tx <- tx1+tx2 # True value
+
+cat("CHOPPING METHOD")
+cat("Approximate x=",tx1,"\n Error=",tx2)
+
+cat("Symmetric Rounding")
+cat("Error = (g(x)-1)*(10^-1) \n"," =",(tx2*10 -1)*10^-1)
+
+cat("Approximate x=0.7527*(10^3)")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.5/Ex4_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.5/Ex4_5.R new file mode 100644 index 00000000..ef801a79 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.5/Ex4_5.R @@ -0,0 +1,31 @@ +# Example 5 Chapter 4 Page no.: 68
+# Truncation error
+
+install.packages("pracma")
+library("pracma")
+
+x <- 1/5
+
+# Numerical Error function
+e <- function(x,n){
+ E <- 0
+ for (i in n:6) {
+ E <- E + (x^i)/fact(i)
+ }
+ return(E)
+}
+cat(" CASE A:")
+n1 <-3
+cat("When first 3 terms are added n=",n1)
+cat("Truncation error =", e(x,n1))
+
+cat(" CASE B:")
+n2 <-4
+cat("When first",n2,"terms are added, n=",n2)
+cat("Truncation error =", e(x,n2))
+
+cat(" CASE C:")
+n3 <-5
+cat("When first",n3,"terms are added n=",n3)
+cat("Truncation error =", e(x,n3))
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.6/Ex4_6.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.6/Ex4_6.R new file mode 100644 index 00000000..0cd4e14e --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.6/Ex4_6.R @@ -0,0 +1,31 @@ +# Example 6 Chapter 4 Page no.: 68
+# Truncation error
+
+install.packages("pracma")
+library("pracma")
+
+x <- -(1/5)
+
+# Numerical Error function
+e <- function(x,n){
+ E <- 0
+ for (i in n:6) {
+ E <- E + (x^i)/fact(i)
+ }
+ return(E)
+}
+cat(" CASE A:")
+n1 <-3
+cat("When first 3 terms are added n=",n1)
+cat("Truncation error =", e(x,n1))
+
+cat(" CASE B:")
+n2 <-4
+cat("When first",n2,"terms are added, n=",n2)
+cat("Truncation error =", e(x,n2))
+
+cat(" CASE C:")
+n3 <-5
+cat("When first",n3,"terms are added n=",n3)
+cat("Truncation error =", e(x,n3))
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.7/Ex4_7.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.7/Ex4_7.R new file mode 100644 index 00000000..949c7046 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.7/Ex4_7.R @@ -0,0 +1,33 @@ +# Example 7 Chapter 4 Page no.: 71
+# Absolute and Relative Error
+
+# Height of building as measured
+H1 <- 2950
+cat("Height of building as measured by Civil engineer =",H1,"cm.")
+
+#True Height of Building
+H2 <- 2945
+cat("True Height of Building=",H2,"cm.")
+
+# Height of each beam as measured
+B1 <- 35
+cat("Height of each beam as measured by Civil engineer=",B1,"cm.")
+
+# True Height of beam
+B2 <- 30
+cat("True Height of beam",B2,"cm.")
+
+#Absolute error in measuring building height
+e1 <- H1-H2
+cat("Absolute error in measuring building height=",e1,"cm")
+
+#Relative error
+er1 <- signif((e1/H2)*100, digits = 2)
+cat("Relative error in measuring building height",er1,"%")
+
+#Absolute error in measuring Beam height
+e2 <- B1-B2
+cat("Absolute error in measuring Beam height=",e2,"cm")
+#Relative error
+er2 <- signif((e2/B2)*100, digits = 2)
+cat("Relative error in measuring Beam height",er2,"%")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.9/Ex4_9.R b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.9/Ex4_9.R new file mode 100644 index 00000000..dcc46913 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH4/EX4.9/Ex4_9.R @@ -0,0 +1,28 @@ +# Example 9 Chapter 4 Page no.: 75
+# Error propagation
+
+# Given Values
+x <- 0.1234E4
+y <- 0.1232E4
+
+#Given Arithmetic operation
+z <- x-y
+
+#Roundoff error in x & y
+erx <- 0.5E-3
+ery <- 0.5E-3
+cat("Roundoff error in x & y is",erx,"&",ery)
+#Absolute Error in x & y
+ex <- x*erx
+ey <- y*ery
+cat("Absolute Error in x=",ex)
+cat("Absolute Error in y=",ey)
+
+# Absolute error in computing z
+ez <- ex+ey
+cat("Absolute error in computing z=",ez)
+
+
+#Relative Error in computing z
+Erz <- (ez/z)*100
+cat("Relative Error in computing z is",Erz,"%.")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.1/Ex6_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.1/Ex6_1.R new file mode 100644 index 00000000..59b7fac0 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.1/Ex6_1.R @@ -0,0 +1,22 @@ +# Example 1 Chapter 6 Page no.: 126
+# Initial Guess Value
+
+#Given Polynomial
+f <- function(x){
+ 2*(x^4)-8*(x^2)+2*x+12
+}
+
+n1 <- 4
+a1 <- c(12,2,-8,2)
+x1 <- -a1[3]/2
+
+cat("The Maximum possible root is",x1)
+
+#Function to find root intervals
+f1 <-function(a,n){
+ xMax <- sqrt(((a[n-1]/a[n])^2)-2*(a[n-2]/a[n]))
+}
+
+mr <- f1(a1,n1)
+
+cat(" Real roots lie in the interval (",-mr,",",mr,").")
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.11/Ex6_11.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.11/Ex6_11.R new file mode 100644 index 00000000..f36ea8e8 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.11/Ex6_11.R @@ -0,0 +1,21 @@ +# Example 11 Chapter 6 Page no.: 161
+# Fixed point method
+
+# Installing and importing package 'spuRs'
+install.packages("spuRs")
+library("spuRs")
+
+#Given function when converted in terms of 'x'
+f <- function(x){
+ 2-(x^2)
+}
+cat("Let initial value be 0")
+x0 <- 0
+
+F1 <- fixedpoint(f,x0)
+cat("Root is",F1)
+
+cat(" Let us assume x0=-1")
+x0 <-1
+F2 <- fixedpoint(f,x0)
+cat("Another Root is",F2)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.12/Ex6_12.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.12/Ex6_12.R new file mode 100644 index 00000000..9c9499fa --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.12/Ex6_12.R @@ -0,0 +1,46 @@ +# Example 12 Chapter 6 Page no.: 162
+# Fixed point method Convergence and Divergence
+
+# Installing and importing package 'spuRs'
+install.packages("spuRs")
+library("spuRs")
+
+#Given function when converted in terms of 'x'
+f <- function(x){
+ 5/x
+}
+cat("Let initial value be 1")
+x0 <- 1
+
+F1 <- fixedpoint(f,x0)
+cat("This type of divergence is called OSCILLATORY DIVERGENCE")
+
+cat(" Let us assume another function x=(x^2)+x-5")
+f1 <- function(x){
+ (x^2)+x-5
+}
+cat("Let initial value be 0")
+x0 <- 0
+F2 <- fixedpoint(f1,x0)
+
+cat("This type of divergence is called MONOTONE DIVERGENCE as it diverges rapidly")
+
+#*********************************************
+
+# Error May occour when calculating convergence on f1.
+# It is due to rapid divergance.
+
+#*********************************************
+
+cat(" Let us assume another function 2x=(5/x)+x")
+f3 <- function(x){
+ (x+(5/x))/2
+}
+cat("Let initial value be 1")
+x0 <- 1
+F3 <- fixedpoint(f3,x0)
+
+cat("This time the function converges rapidly")
+cat("The square root of 5 is",signif(F3,digits = 5))
+
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.14/Ex6_14.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.14/Ex6_14.R new file mode 100644 index 00000000..554c0ecf --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.14/Ex6_14.R @@ -0,0 +1,37 @@ +# Example 14 Chapter 6 Page no.: 172
+# Newton-Raphson Method for system of nonlinear equations
+
+
+#First function
+u <- function(x,y) {
+ x^2+x*y-6
+}
+#Second function
+v <- function(x,y) {
+ (x^2)-(y^2) -3
+}
+
+#Initial values
+x=1
+y=1
+
+#Error
+e<-c(100, 100)
+
+#Computing roots
+while (e[1]>0.00001 & e[2]>0.00001){
+ J=matrix(data = c(2*x+y, x, 2*x, -2*y),nrow = 2,ncol = 2,byrow = TRUE)
+ deter=det(J)
+ u1=u(x,y)
+ v1=v(x,y)
+ x=x-((u1*J[2,2]-v1*J[1,2])/deter)
+ y=y-((v1*J[1,1]-u1*J[2,1])/deter)
+ e[1]=abs(2-x)
+ e[2]=abs(3-y)
+}
+
+Roots<-c(x, y)
+cat("Value of x converges to",Roots[1])
+cat("Value of y converges to",Roots[2])
+
+#Only 2 iterations has been shown in the textbook.
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.15/Ex6_15.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.15/Ex6_15.R new file mode 100644 index 00000000..3e21417a --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.15/Ex6_15.R @@ -0,0 +1,22 @@ +# Example 15 Chapter 6 Page no.: 176
+# Finding Quotient polynomial
+
+#Given Functon
+p <- function(x){
+ (x^3)-7*(x^2)+15*x-9
+}
+
+a <- c()
+b <- c()
+# It is given that p(x)=(x-3)q(x)
+x1 <-3
+a[4] <- 1
+a[3] <- -7
+a[2] <-15
+a[1] <--9
+b[4] <- 0
+for (i in 4:2) {
+ b[i-1] = a[i] + x1*b[i]
+}
+
+cat("The required polynomial q(x) is",b[3],"*x^2",b[2],"*x +",b[1],"=0")
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.16/Ex6_16.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.16/Ex6_16.R new file mode 100644 index 00000000..9324bde3 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.16/Ex6_16.R @@ -0,0 +1,48 @@ +# Example 16 Chapter 6 Page no.: 187
+# Bairstow's method
+
+#Given function
+f <- function(x) {
+ (x^3)+x+10
+}
+
+#Given values
+u= 1.8
+v= -1
+
+es=1
+#%
+n=4
+
+count=1
+
+ear=100
+eas=100
+
+a<-c(10,1,0,1)
+b<-matrix(0,n)
+c<-matrix(0,n)
+
+while ((ear>es) & (eas>es)){
+ b[n]=a[n]
+ b[n-1]=a[n-1]+u*b[n]
+ for (i in seq(n-2,1,-1)){
+ b[i]=a[i]+u*b[i+1]+v*b[i+2]
+ }
+ c[n]=b[n]
+ c[n-1]=b[n-1]+u*c[n]
+ for (i in seq((n-2),2,-1)){
+ c[i]=b[i]+u*c[i+1]+v*c[i+2]
+ }
+dv=((-b[1])+(b[2]*c[2]/c[3]))/(c[3]-(c[4]*c[2]/c[3]))
+du=(-b[2]-c[4]*dv)/c[3]
+u=u+du
+v=v+dv
+ear=abs(du/u)*1000
+eas=abs(dv/v)*1000
+cat("Iteration:",count,"\n u: ",u,"\n","v:",v,"\n","************************************\n")
+count=count+1;
+}
+cat("Final value of Quadratic quotients u & v are:\n","u: ",u,"\n v:",v,"\n","************************************\n")
+
+# Value in the textbook is of one iteration only and it is clearly given that the final answers are 2 and -5
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.17/Ex6_17.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.17/Ex6_17.R new file mode 100644 index 00000000..fff9affb --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.17/Ex6_17.R @@ -0,0 +1,39 @@ +# Example 17 Chapter 6 Page no.: 197
+# Muller's method
+
+#Given function
+
+f <- function(x) {
+ return((x^3)+2*(x^2)+10*x-20)
+ }
+
+#Initial values
+x0=0
+x1=1
+x2=2
+
+cat("iteration:",0,"\n","xr:",x2,"************************\n")
+
+for (i in 1:6){
+ h0=x1-x0
+ h1=x2-x1
+ d0=(f(x1)-f(x0))/(x1-x0)
+ d1=(f(x2)-f(x1))/(x2-x1)
+ a=(d1-d0)/(h1+h0)
+ b=a*h1+d1
+ c=f(x2)
+ d=(b^2 - 4*a*c)^0.5
+ if (abs(b+d)>abs(b-d)){
+ x3=x2+((-2*c)/(b+d))
+ }else {
+ x3=x2+((-2*c)/(b-d))
+ }
+ x0=x1
+ x1=x2
+ x2=x3
+ cat("iteration:",i,"\n")
+ cat("Value:", signif(x2,digits = 9) ,"\n")
+ cat("************************\n")
+}
+
+cat("The value of the function stablized at",i,"th iteration and was found to be",signif(x2,digits=9))
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.4/Ex6_4.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.4/Ex6_4.R new file mode 100644 index 00000000..63473184 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.4/Ex6_4.R @@ -0,0 +1,41 @@ +# Example 4 Chapter 6 Page no.: 132
+# Bisection Method
+
+#Given Function
+f <- function(x) {
+ (x^2)-(4*x)-10
+}
+
+curve(f, xlim=c(-3,3), col='blue', lwd=2, lty=2)
+abline(h=0)
+abline(v=0)
+
+#Values are chosen where curver crosses the x axis
+x1=-2
+x2=-1
+
+#using bisection method
+cat("BISECTION METHOD:")
+xr=(x1+x2)/2
+cat("Iteration:",1,"\n","xl:",x1,"\n","xu:",x2,"\n","xr:",xr,"\n","****************************************\n")
+
+for (i in 2:7){
+ if (f(x1)*f(xr)>0){
+ x1=xr
+ xr=(x1+x2)/2
+ } else if (f(x1)*f(xr)<0){
+ x2=xr
+ xr=(x1+x2)/2
+ }
+
+ if (f(x1)*f(xr)==0){
+ break
+ }
+ cat("Iteration:",i,"\n")
+ cat("xl:",x1,"\n")
+ cat("xu:",x2,"\n")
+ cat("xr:",xr,"\n")
+ cat("****************************************\n")
+}
+
+# The slight variation in answer is due to approximation
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.5/Ex6_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.5/Ex6_5.R new file mode 100644 index 00000000..eb9d2313 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.5/Ex6_5.R @@ -0,0 +1,37 @@ +# Example 5 Chapter 6 Page no.: 140
+# False Position Method
+
+#Given Function
+f <- function(x) {
+ (x^2)-(x)-2
+}
+
+# Given Values
+x1=1
+x2=3
+
+# False Position Function
+
+xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))
+cat("Iteration:",1,"\n","xl:",x1,"\n","xu:",x2,"\n","*************************")
+
+for (i in 2:11){
+ if (f(x1)*f(xr)>0){
+ x1=xr
+ xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))
+ }
+ else if (f(x1)*f(xr)<0){
+ x2=xr
+ xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))
+ }
+ if (f(x1)*f(xr)==0){
+ break
+ }
+ cat("Iteration:",i,"\n")
+ cat("xlow:",x1,"\n")
+ cat("xup: ",x2,"\n")
+ cat("xcur:",xr,"\n")
+ cat("*************************\n")
+}
+
+cat("After",i,"th iteration","the root approximation stablized and the root is",xr)
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.7/Ex6_7.R b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.7/Ex6_7.R new file mode 100644 index 00000000..8212cef0 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH6/EX6.7/Ex6_7.R @@ -0,0 +1,54 @@ +# Example 7 Chapter 6 Page no.: 147
+# Newton-Raphson Method
+
+install.packages("numDeriv")
+library("numDeriv")
+
+# Given Function
+u <- function(x) {
+ x^2-3*x+2
+}
+
+curve(u, xlim=c(-5,5), col='blue', lwd=2, lty=2, ylab='f(x)')
+abline(h=0)
+abline(v=0)
+
+# From the curve the points in the vicinity are noted
+a <- 0
+b <-1
+
+newton.raphson <- function(f, a, b, tol = 1e-5, n = 1000) {
+ require(numDeriv) # Package for computing f'(x)
+
+ x0 <- a # Set start value to supplied lower bound
+ k <- n # Initialize for iteration results
+
+ # Check the upper and lower bounds to see if approximations result in 0
+ fa <- f(a)
+ if (fa == 0.0) {
+ return(a)
+ }
+
+ fb <- f(b)
+ if (fb == 0.0) {
+ return(b)
+ }
+
+ for (i in 1:n) {
+ dx <- genD(func = f, x = x0)$D[1] # First-order derivative f'(x0)
+ x1 <- x0 - (f(x0) / dx)
+ k[i] <- x1
+ # Checking difference between values
+ if (abs(x1 - x0) < tol) {
+ root.approx <- tail(k, n=1)
+ res <- list('root approximation' = root.approx, 'iterations' = k)
+ return(res)
+ }
+ # If Newton-Raphson has not yet reached convergence set x1 as x0 and continue
+ x0 <- x1
+ }
+ print('Too many iterations in method')
+}
+
+N <- newton.raphson(u, a, b)
+cat("The root closer to the point x=0 is",N)
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.1/Ex7_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.1/Ex7_1.R new file mode 100644 index 00000000..31f25f49 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.1/Ex7_1.R @@ -0,0 +1,12 @@ +# Example 1 Chapter 7 Page no.: 211
+# Solving System of Equations
+
+#Define Sysytem of Equations in Matrix form
+A<-matrix(c(3,2,1,2,3,2,1,2,3),nrow = 3,ncol = 3,byrow = TRUE)
+A
+b<-matrix(c(10,14,14),nrow = 3,ncol = 1)
+b
+
+k <- solve(A,b)
+cat("Solution:\n x=",k[1],"\n y=",k[2],"\n z=",k[3])
+
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.5/Ex7_5.R b/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.5/Ex7_5.R new file mode 100644 index 00000000..58ded1ad --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.5/Ex7_5.R @@ -0,0 +1,29 @@ +# Example 5 Chapter 7 Page no.: 234
+# Dolittle Algorithm
+
+#import 'matrixcalc' library
+install.packages("matrixcalc")
+library(matrixcalc)
+
+#Define system of equations in matrix form
+A<-matrix(c(3,2,1,2,3,2,1,2,3),nrow = 3,ncol = 3,byrow = TRUE)
+A
+B<-matrix(c(10,14,14),nrow = 3,ncol = 1)
+B
+
+#LU Decomposition
+luA<-lu.decomposition(A)
+L<-luA$L
+L
+U<-luA$U
+U
+
+z1<-B[1]
+z2<-B[2]-L[2,1]*z1
+z3<-B[3]-L[3,1]*z1-L[3,2]*z2
+
+#By Back Substitution we get,
+x3=z3/(U[3,3])
+x2=(z2-U[2,3]*x3)/U[2,2]
+x1=(z1-U[1,2]*x2-U[1,3]*x3)/U[1,1]
+sprintf("Values of x1, x2 and x3 are %f,%f and %f",x1,x2,x3)
diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.1/Ex8_1.R b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.1/Ex8_1.R new file mode 100644 index 00000000..86ec80bc --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.1/Ex8_1.R @@ -0,0 +1,19 @@ +# Example 1 Chapter 8 Page no.: 254
+# Jacobi Iteration Method
+
+# Installing and importing 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#System of equations in matrix form
+A1 <- matrix(c(2,1,1,3,5,2,2,1,4), nrow = 3, byrow = T)
+B1 <- c(5,15,8)
+
+#Jacobi method
+C <- c()
+C <- itersolve(A1, B1, x0 = NULL, nmax = 4, tol = 1e-05 , method = "Jacobi")
+cat("The values given in the text is only upto 4 iterations, which are:", C[["x"]])
+
+C1 <- c()
+C1 <-itersolve(A1, B1, x0 = NULL, nmax = 1000, tol = 1e-05 , method = "Jacobi")
+cat("The final solution after",C1[["iter"]],"iterations are:", C1[["x"]])
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.2/Ex8_2.R b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.2/Ex8_2.R new file mode 100644 index 00000000..759154a8 --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.2/Ex8_2.R @@ -0,0 +1,19 @@ +# Example 2 Chapter 8 Page no.: 261
+# Gauss-Seidel Iteration
+
+# Installing and importing 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#System of equations in matrix form
+A1 <- matrix(c(2,1,1,3,5,2,2,1,4), nrow = 3, byrow = T)
+B1 <- c(5,15,8)
+
+#Gauss-Siedel method
+C <- c()
+C <- itersolve(A1, B1, x0 = NULL, nmax = 2, tol = 1e-05 , method = "Gauss-Seidel")
+cat("The values given in the text is only upto 2 iterations, which are:", C[["x"]])
+
+C1 <- c()
+C1 <-itersolve(A1, B1, x0 = NULL, nmax = 1000, tol = 1e-07 , method = "Gauss-Seidel")
+cat("The final solution after",C1[["iter"]],"iterations are:", C1[["x"]])
\ No newline at end of file diff --git a/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.3/Ex8_3.R b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.3/Ex8_3.R new file mode 100644 index 00000000..ffaee94d --- /dev/null +++ b/Numerical_Methods_by_E_Balaguruswamy/CH8/EX8.3/Ex8_3.R @@ -0,0 +1,16 @@ +# Example 3 Chapter 8 Page no.: 269
+# Gauss-Seidel method
+
+# Installing and importing 'pracma' library
+install.packages("pracma")
+library("pracma")
+
+#System of equations in matrix form
+A1 <- matrix(c(3,1,1,-3), nrow = 2, byrow = T)
+B1 <- c(5,5)
+x1 <- c(0,0)
+#Gauss-Siedel method
+C1 <- c()
+C1 <-itersolve(A1, B1, x0 = x1, nmax = 1000, tol = 1e-04 , method = "Gauss-Seidel")
+cat("The final solution after",C1[["iter"]],"iterations are:", C1[["x"]])
+cat("Which can be approximated to x1= 2 and x2= -1")
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.
+
+
|