Title: | searching for the root of equation |
---|---|
Description: | This is a package which can help you search for the root of a equation. |
Authors: | Zheng Sengui,Lu Xufen,Hou Qiongchen,Zheng Jianhui |
Maintainer: | Zheng Sengui<[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0 |
Built: | 2024-11-01 11:14:52 UTC |
Source: | https://github.com/cran/NLRoot |
Bisection Method to Find the Root of Nonlinear Equation
BFfzero(f, a, b, num = 10, eps = 1e-05)
BFfzero(f, a, b, num = 10, eps = 1e-05)
f |
the objective function which we will use to solve for the root |
a |
mininum of the interval which cantains the root from Bisection Method |
b |
maxinum of the interval which cantains the root from Bisection Method |
num |
the number of sections that the interval which from Bisection Method |
eps |
the level of precision that |x(k+1)-x(k)| should be satisfied in order to get the idear real root. eps=1e-5 when it is default |
Be careful to choose a & b. If not we maybe fail to find the root
a root of the objective function which between the interwal from a to b
Maintainer:Zheng Sengui<[email protected]>
Zheng Sengui,Lu Xufen,Hou Qiongchen,Zheng Jianhui
Luis Torgo (2003) Data Mining with R:learning by case studies. LIACC-FEP, University of Porto
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; BFfzero(f,0,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, a, b, num = 10, eps = 1e-05) { h = abs(b - a)/num i = 0 j = 0 a1 = b1 = 0 while (i <= num) { a1 = a + i * h b1 = a1 + h if (f(a1) == 0) { print(a1) print(f(a1)) } else if (f(b1) == 0) { print(b1) print(f(b1)) } else if (f(a1) * f(b1) < 0) { repeat { if (abs(b1 - a1) < eps) break x <- (a1 + b1)/2 if (f(a1) * f(x) < 0) b1 <- x else a1 <- x } print(j + 1) j = j + 1 print((a1 + b1)/2) print(f((a1 + b1)/2)) } i = i + 1 } if (j == 0) print("finding root is fail") else print("finding root is successful") }
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; BFfzero(f,0,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, a, b, num = 10, eps = 1e-05) { h = abs(b - a)/num i = 0 j = 0 a1 = b1 = 0 while (i <= num) { a1 = a + i * h b1 = a1 + h if (f(a1) == 0) { print(a1) print(f(a1)) } else if (f(b1) == 0) { print(b1) print(f(b1)) } else if (f(a1) * f(b1) < 0) { repeat { if (abs(b1 - a1) < eps) break x <- (a1 + b1)/2 if (f(a1) * f(x) < 0) b1 <- x else a1 <- x } print(j + 1) j = j + 1 print((a1 + b1)/2) print(f((a1 + b1)/2)) } i = i + 1 } if (j == 0) print("finding root is fail") else print("finding root is successful") }
Newton Downhill Method to Find the Root of Nonlinear Equation
NDHfzero(f, f1, x0 = 0, num = 1000, eps = 1e-05, eps1 = 1e-05)
NDHfzero(f, f1, x0 = 0, num = 1000, eps = 1e-05, eps1 = 1e-05)
f |
the objective function which we will use to solve for the root |
f1 |
the derivative of the objective function (say f) |
x0 |
the initial value of Newton iteration method or Newton downhill method |
num |
num the number of sections that the interval which from Brent's method devide into. num=1000 when it is default |
eps |
the level of precision that |x(k+1)-x(k)| should be satisfied in order to get the idear real root. eps=1e-5 when it is default |
eps1 |
the level of precision that |f(x)| should be satisfied, where x comes from the program. when it is not satisified we will fail to get the root |
eps1 of precision that |f(x)| should be satisfied, where x comes from the program. when it is not satisified we will fail to get the root
a root of the objective function
Maintainer:Zheng Sengui<[email protected]>
Zheng Sengui,Lu Xufen,Hou Qiongchen,Zheng Jianhui
Luis Torgo (2003) Data Mining with R:learning by case studies. LIACC-FEP, University of Porto
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; NDHfzero(f,f1,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, f1, x0 = 0, num = 1000, eps = 1e-05, eps1 = 1e-05) { a = x0 b = a - f(a)/f1(a) i = 0 while ((abs(b - a) > eps)) { c = 1 j = 0 while (abs(f(b)) >= abs(f(a))) { b = a - c * f(a)/f1(a) j = j + 1 c = 1/(2^j) } a = b b = a - f(a)/f1(a) c = 1 j = 0 while (abs(f(b)) >= abs(f(a))) { b = a - c * f(a)/f1(a) j = j + 1 c = 1/(2^j) } i = i + 1 } print(b) print(f(b)) if (abs(f(b)) < eps1) { print("finding root is successful") } else print("finding root is fail") }
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; NDHfzero(f,f1,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, f1, x0 = 0, num = 1000, eps = 1e-05, eps1 = 1e-05) { a = x0 b = a - f(a)/f1(a) i = 0 while ((abs(b - a) > eps)) { c = 1 j = 0 while (abs(f(b)) >= abs(f(a))) { b = a - c * f(a)/f1(a) j = j + 1 c = 1/(2^j) } a = b b = a - f(a)/f1(a) c = 1 j = 0 while (abs(f(b)) >= abs(f(a))) { b = a - c * f(a)/f1(a) j = j + 1 c = 1/(2^j) } i = i + 1 } print(b) print(f(b)) if (abs(f(b)) < eps1) { print("finding root is successful") } else print("finding root is fail") }
Newton iteration method to Find the Root of Nonlinear Equation.
NIMfzero(f, f1, x0 = 0, num = 100, eps = 1e-05, eps1 = 1e-05)
NIMfzero(f, f1, x0 = 0, num = 100, eps = 1e-05, eps1 = 1e-05)
f |
the objective function which we will use to solve for the root |
f1 |
the derivative of the objective function (say f) |
x0 |
the initial value of Newton iteration method or Newton downhill method |
num |
the number of sections that the interval which from Brent's method devide into. num=100 when it is default |
eps |
the level of precision that |x(k+1)-x(k)| should be satisfied in order to get the idear real root. eps=1e-5 when it is default |
eps1 |
the level of precision that |f(x)| should be satisfied, where x comes from the program. when it is not satisified we will fail to get the root |
the root we found out is based on the x0. So it is better to choose x0 carefully
the root of the function
Maintainer:Zheng Sengui<[email protected]>
Zheng Sengui,Lu Xufen,Hou Qiongchen,Zheng Jianhui
Luis Torgo (2003) Data Mining with R:learning by case studies. LIACC-FEP, University of Porto
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; NIMfzero(f,f1,0) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, f1, x0 = 0, num = 100, eps = 1e-05, eps1 = 1e-05) { a = x0 b = a - f(a)/f1(a) i = 0 while ((abs(b - a) > eps) & (i < num)) { a = b b = a - f(a)/f1(a) i = i + 1 } print(b) print(f(b)) if (abs(f(b)) < eps1) { print("finding root is successful") } else print("finding root is fail") }
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; NIMfzero(f,f1,0) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, f1, x0 = 0, num = 100, eps = 1e-05, eps1 = 1e-05) { a = x0 b = a - f(a)/f1(a) i = 0 while ((abs(b - a) > eps) & (i < num)) { a = b b = a - f(a)/f1(a) i = i + 1 } print(b) print(f(b)) if (abs(f(b)) < eps1) { print("finding root is successful") } else print("finding root is fail") }
Secant Method to Find the Root of Nonlinear Equation.
SMfzero(f, x1, x2, num = 1000, eps = 1e-05, eps1 = 1e-05)
SMfzero(f, x1, x2, num = 1000, eps = 1e-05, eps1 = 1e-05)
f |
the objective function which we will use to solve for the root |
x1 |
the initial value of Secant Method |
x2 |
the initial value of Secant Method |
num |
the number of sections that the interval which from Brent's method devide into. num=1000 when it is default |
eps |
the level of precision that |x(k+1)-x(k)| should be satisfied in order to get the idear real root. eps=1e-5 when it is default |
eps1 |
the level of precision that |f(x)| should be satisfied, where x comes from the program. when it is not satisified we will fail to get the root |
Be careful to choose x1 & x2.if not we maybe fail to get the root
the root of the function
Maintainer:Zheng Sengui<[email protected]>
Zheng Sengui,Lu Xufen,Hou Qiongchen,Zheng Jianhui
Luis Torgo (2003) Data Mining with R:learning by case studies. LIACC-FEP, University of Porto
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; SMfzero(f,0,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, x1, x2, num = 1000, eps = 1e-05, eps1 = 1e-05) { i = 0 while ((abs(x1 - x2) > eps) & (i < num)) { c = x2 - f(x2) * (x2 - x1)/(f(x2) - f(x1)) x1 = x2 x2 = c i = i + 1 } print(x2) print(f(x2)) if (abs(f(x2)) < eps1) { print("finding root is successful") } else print("finding root is fail") }
f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1}; SMfzero(f,0,2) ##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (f, x1, x2, num = 1000, eps = 1e-05, eps1 = 1e-05) { i = 0 while ((abs(x1 - x2) > eps) & (i < num)) { c = x2 - f(x2) * (x2 - x1)/(f(x2) - f(x1)) x1 = x2 x2 = c i = i + 1 } print(x2) print(f(x2)) if (abs(f(x2)) < eps1) { print("finding root is successful") } else print("finding root is fail") }