level 1
南殇🌸寂寥
楼主
这个是第一道
Introduction
Consider the problem of finding a root of the equation h(x)=0, where h(x) is a continuous function of a single variable x. The Newton-Raphson method can be used to find such a root, if you can evaluate the gradient of the function h(x). The false position or regula falsi algorithm is a simple alternative method for which the gradient does not need to be evaluated. To use it, it is necessary to start with two values L and U ("lower" and "upper") for whichL<U and h(L)h(U) < 0: in this case, h(L) and h(U) must have different signs and, because h(x) is continuous, there must be at least one value of xbetween L and U with h(x)=0.
Starting with such a pair of values L and U, the next step is to calculate a value M ("middle") as

(if you were to draw a graph ofh(x) against x and connect the points (L,h(L)) and (U,h(U)) with a straight line, then M would be the point between L and U where the line intersects the axis). If h(M)=0 then M is the required root. Otherwise, if h(L)h(M)<0 then there must be a root between L and M so the upper bound U can be replaced with M; and if h(U)h(M)<0 then the lower bound L can be replaced with M. The procedure is then repeated until at least one of the following conditions is satisfied:
A value of M is calculated with |h(M)|< ε;
|U-L| < ε for some small value of ε.
If the first condition is satisfied, M can be taken as the estimated root; if the second is satisfied, any value in the range (L,U) can be taken as the estimated root which is now known to be within ε of the true value.
Your task
Write an R function called polysolve(), to find a root of the equation h(x)=0 using the regula falsi algorithm where h(x) is a degree p polynomial: h(x) = a0 + a1x + a2x2 + … + apxp with p > 0 and ap ≠ 0. The arguments to your function should be: a, a vector of coefficients such that a[1] represents a0,a[2] represents a1, and so on; lower, the initial value of L; upper, the initial value of U; and tol, the value of ε. The default value of tol should be1e-6. Your function should start by checking that the values of lower and upper do indeed bracket a root: if so, it should return the estimated root as a single value. Otherwise it should return the value NA with an appropriate warning message. Your function must not use any of R's own root-finding routines such as polyroot(), uniroot() or anything similar.
2017年02月13日 18点02分
1
Introduction
Consider the problem of finding a root of the equation h(x)=0, where h(x) is a continuous function of a single variable x. The Newton-Raphson method can be used to find such a root, if you can evaluate the gradient of the function h(x). The false position or regula falsi algorithm is a simple alternative method for which the gradient does not need to be evaluated. To use it, it is necessary to start with two values L and U ("lower" and "upper") for whichL<U and h(L)h(U) < 0: in this case, h(L) and h(U) must have different signs and, because h(x) is continuous, there must be at least one value of xbetween L and U with h(x)=0.
Starting with such a pair of values L and U, the next step is to calculate a value M ("middle") as
(if you were to draw a graph ofh(x) against x and connect the points (L,h(L)) and (U,h(U)) with a straight line, then M would be the point between L and U where the line intersects the axis). If h(M)=0 then M is the required root. Otherwise, if h(L)h(M)<0 then there must be a root between L and M so the upper bound U can be replaced with M; and if h(U)h(M)<0 then the lower bound L can be replaced with M. The procedure is then repeated until at least one of the following conditions is satisfied:A value of M is calculated with |h(M)|< ε;
|U-L| < ε for some small value of ε.
If the first condition is satisfied, M can be taken as the estimated root; if the second is satisfied, any value in the range (L,U) can be taken as the estimated root which is now known to be within ε of the true value.
Your task
Write an R function called polysolve(), to find a root of the equation h(x)=0 using the regula falsi algorithm where h(x) is a degree p polynomial: h(x) = a0 + a1x + a2x2 + … + apxp with p > 0 and ap ≠ 0. The arguments to your function should be: a, a vector of coefficients such that a[1] represents a0,a[2] represents a1, and so on; lower, the initial value of L; upper, the initial value of U; and tol, the value of ε. The default value of tol should be1e-6. Your function should start by checking that the values of lower and upper do indeed bracket a root: if so, it should return the estimated root as a single value. Otherwise it should return the value NA with an appropriate warning message. Your function must not use any of R's own root-finding routines such as polyroot(), uniroot() or anything similar.