How to find the roots of a function whose analytical form is not known, rather the function is available as a tabulated set of values? - c

To find the roots of a function, we can generally use bisection method or Newton's method. For a function f(x), this is possible only when we have an analytical expression for the x-dependence of f(x).
I am trying to find the roots of such a function where I don't know the exact form of the function, rather I have a tabulated data for the values of f(x) for each values of x in a particular range of x. I am writing my program in C and I am using a for-loop to calculate f(x) for each value of x by solving a non-linear equation using bisection method and tabulating the data. Now I need to find the roots of the function f(x).
Can anyone help me with any suitable method or algorithm for the problem?
Thanks in advance!

You know from where the sign changes that a root has to be between two points.
Take several nearby points, put a polynomial through them, and then solve for the root of that polynomial using Newton's method.
From your description it looks like you should be able to calculate your function at this new point. If so, then I would suggest that you calculate the value at this point, add the two nearest neighbors, calculate a parabola and solve for the root of that. If your function is smooth and has a non-zero derivative at the root, this step will make your estimate of the root several orders of magnitude more accurate.
(You can repeat again for even more accuracy. But the increased accuracy at this point may be on par with the numerical errors in your estimate of the value of the function.)

Related

How to obtain the derivative of Rodrigues vector and perform update in nonlinear least square?

I am now interested in the bundle adjustment in SLAM, where the Rodrigues vectors $R$ of dimension 3 are used as part of variables. Assume, without loss of generality, we use Gauss-Newton method to solve it, then in each step we need to solve the following linear least square problem:
$$J(x_k)\Delta x = -F(x_k),$$
where $J$ is the Jacobi of $F$.
Here I am wondering how to calculate the derivative $\frac{\partial F}{\partial R}$. Is it just like the ordinary Jacobi in mathematic analysis? I have this wondering because when I look for papers, I find many other concepts like exponential map, quaternions, Lie group and Lie algebra. So I suspect if there is any misunderstanding.
This is not an answer, but is too long for a comment.
I think you need to give more information about how the Rodrigues vector appears in your F.
First off, is the vector assumed to be of unit length.? If so that presents some difficulties as now it doesn't have 3 independent components. If you know that the vector will lie in some region (eg that it's z component will always be positive), you can work round this.
If instead the vector is normalised before use, then while you could then compute the derivatives, the resulting Jacobian will be singular.
Another approach is to use the length of the vector as the angle through which you rotate. However this means you need a special case to get a rotation through 0, and the resulting function is not differentiable at 0. Of course if this can never occur, you may be ok.

Calculate arc length of piecewise cubic spline?

I would like to calculate the arc length of an already-interpolated piecewise cubic spline, where each segment is defined by a normal cubic polynomial ax^3 + bx^2 + cx + d. I am not sure, however, what the best route to take is.
My first idea is to use numerical integration and the following arc length formula to calculate the arc length for each segment and then sum them up:
https://tutorial.math.lamar.edu/classes/calcii/arclength.aspx
I am not sure if this is the best approach, as I have minimal experience in numeric integration. If this is the approach to take, which numeric integration method should I use? If not, how can I accomplish this?
Thanks a lot
There is a closed-form expression in terms of the Elliptic integrals, but the exact computation is better done by Mathematica, and next you will need the elliptic functions handy.
The numerical method by polyline approximation (as in the link) is a little too elementary. For such a smooth function, Simpson's rule will be fine. https://en.wikipedia.org/wiki/Simpson%27s_rule

Numerical Integration in fortran with infinity as one of the limits

I am asked to normalize a probability distribution P=A(x^2)(e^-x) within 0 to infinity by finding the value for A. I know the algorithms to calculate the Numerical value of Integration, but how do I deal with one of the limits being Infinity.
The only way I have been able to solve this problem with some accuracy (I got full accuracy, indeed) is by doing some math first, in order to obtain the taylor series that represents the integral of the first.
I have been looking here for my sample code, but I don't find it. I'll edit my post if I get a working solution.
The basic idea is to calculate all the derivatives of the function exp(-(x*x)) and use the coeficients to derive the integral form (by dividing those coeficients by one more than the exponent of x of the above function) to get the taylor series of the integral (I recommend you to use the unnormalized version described above to get the simple number coeficients, then adjust the result by multiplying by the proper constants) you'll get a taylor series with good convergence, giving you precise values for full precision (The integral requires a lot of subdivision, and you cannot divide an unbounded interval into a finite number of intervals, all finite)
I'll edit this question if I get on the code I wrote (so stay online, and dont' change the channel :) )

R's isoreg function generating more knots than unique fitted values

I have been working with R's isoreg function and have experienced a problem: the function is generating more knots than unique fitted values.
From the R help,
iKnots [is an] integer vector giving indices where the fitted curve jumps, i.e., where the convex minorant has kinks
I believe I have an idea about the cause of the problem, and I have a reproducible example:
# Demonstrating the problem
set.seed(100)
x<-runif(88000,0,1)
x<-x[order(x)]
y<- c(rep(c(0.1000001,0.1000000),11000),rep(c(0.1000002,0.1000003),11000),rep(c(0.2000002,0.2000003),11000),rep(1,22000))
plot(test<-isoreg(x,y))
length(unique(test$yf))
length(test$iKnots)
# Evidence of a floating point arithmetic problem
unique(test$yf)
print(c(unique(test$yf)[1],unique(test$yf)[2]),digits=18)
unique(test$yf)[1]==unique(test$yf)[2]
print(c(unique(test$yf)[4],unique(test$yf)[5]),digits=18)
unique(test$yf)[4]==unique(test$yf)[5]}
Here is the plot produced by this example:
You can see that R's isoreg function is identifying many more knots than it should (where there are a lot of red Xs in the plot). However at other places, it correctly uses only 2 knots (the black lines).
It is clear that the problem is connected to R's floating point arithmetic. I also note that isoreg uses .Call to call a C routine to actually do the isotonic regression, so perhaps the problem lies with differences between the C and R languages.
I am using isoreg to calibrate model probabilities, and I would like to be as precise as possible. Therefore, I have 2 questions:
1) Is there some way I could alter the x and y variables used in the isoreg function to avoid this problem while maintaining as high precision as possible?
2) I can manually find the unique fitted values and the respective knots. However, is this ok? Can I assume that the algorithm found the best fit or could this problem invalidate that assumption?

Dijkstras algorithm with each edge having cost as well as distance

I have to modify dijikstras algorithm with each edge having two values-one as distance and other as cost.I have to find the most efficient way from source to destination.
Can anyone suggest a way for comparing the edges based on both the factors??
Thankyou
Multiply these factors? Or sum them up or build a custom function f(distance, cost) -> value which returns reasonable values
Use f(cost,distance) where f is a weight function which you use as your new cost and use the algorithm as before.

Resources