diff options
author | Prashant S | 2019-10-04 12:27:32 +0530 |
---|---|---|
committer | GitHub | 2019-10-04 12:27:32 +0530 |
commit | ac2986488a9731cff5cbb517d8f0ef98e2561d64 (patch) | |
tree | 7bb3f64824627ef179d5f341266a664fd0b69011 /Numerical_Methods_by_E_Balaguruswamy/CH6 | |
parent | cbb2770fb2f88246175add29623103a56ba338b8 (diff) | |
parent | b3f3a8ecd454359a2e992161844f2fb599f8238a (diff) | |
download | R_TBC_Uploads-master.tar.gz R_TBC_Uploads-master.tar.bz2 R_TBC_Uploads-master.zip |
Added R TBC
Diffstat (limited to 'Numerical_Methods_by_E_Balaguruswamy/CH6')
10 files changed, 367 insertions, 0 deletions
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 |