4. 미적분 응용하기-뉴턴랩슨 메서드
# f(x) = 0인 x를 찾기 >>> newton = function(f, tol = 1e-7, x0 = 1, N = 100){ >>> h = 1e-7; >>> i = 1 >>> x1 = x0; >>> p = numeric(N) >>> while(i <= N) { >>> df.dx = (f(x0 + h)-f(x0)) / h >>> x1 = (x0 - (f(x0) / df.dx)) >>> p[i] = x1 >>> i = i + 1 >>> if(abs(x1 - x0) < tol) break >>> x0 = x1 >>> } >>> return(p[1 : (i - 1)]) >>> } # 함수 적용 >>> f <- function(x) (x^2 - 2) # x2-2 함수에 적용 >>> newton(f) 1.500000 1.416667 1.414216 1.414214 1.414214 >>> f <- function(x) (x^3 + 3 * x^2 - 6 * x - 8) # x3+3x2-6x-8 함수에 적용 >>> newton(f) 4.333333 2.974769 2.270963 2.030331 2.000449 2.000000 2.000000 2.000000