how to find tangent at time t in natural cubic spline - math

I used Tim Lamber's algorithm to draw a natural spline cubic curve.
NatCubic class
NatCubic class compute the coefficients of the cubics (a b c d) of an array of points.
a + b*u + c*u^2 + d*u^3 0<=u <1
and Cubic class compute the points on the curve at time t in [0-1].
b in Cubic class is the derivatives at the knots at i
My question is: how to find the tangent at t >0 <1
Thank you!
PS :
for clarify my question, I search the tangent at time t, eg 0.5
to using pre-computed coeficients (abcd) of each control point.
This is to avoid calculating the point(t+1) to find the tangent by, y(i +1) - y(i-1)
sorry for my poor english.
you can see in this picture that I want to do

Sont know if I well understood your question.
(EDIT)
Slope = b + 2c*u + 3d*u^2
then for the tanget use
(y - yo) = m * (x - x0)
where y0 is your spline value at point x0
and m is the slope at x0

Related

Split a cubic Bézier curve at a point

This question and this question both show how to split a cubic Bézier curve at a particular parameterized value 0 ≤ t ≤ 1 along the curve, composing the original curve shape from two new segments. I need to split my Bézier curve at a point along the curve whose coordinate I know, but not the parameterized value t for the point.
For example, consider Adobe Illustrator, where the user can click on a curve to add a point into the path, without affecting the shape of the path.
Assuming I find the point on the curve closest to where the user clicks, how do I calculate the control points from this? Is there a formula to split a Bézier curve given a point on the curve?
Alternatively (and less desirably), given a point on the curve, is there a way to determine the parameterized value t corresponding to that point (other than using De Casteljau's algorithm in a binary search)?
My Bézier curve happens to only be in 2D, but a great answer would include the vector math needed to apply in arbitrary dimensions.
It is possible, and perhaps simpler, to determine the parametric value of a point on the curve without using De Casteljau's algorithm, but you will have to use heuristics to find a good starting value and similarly approximate the result.
One possible, and fairly simple way is to use Newton's method such that:
tn+1 = tn - ( bx(tn) - cx ) / bx'(tn)
Where bx(t) refers to the x component of some Bezier curve in polynomial form with the control points x0, x1, x2 and x3, bx'(t) is the first derivative and cx is a point on the curve such that:
cx = bx(t) | 0 < t < 1
the coefficients of bx(t) are:
A = -x0 + 3x1 - 3x2 + x3
B = 3x0 - 6x1 + 3x2
C = -3x0 + 3x1
D = x0
and:
bx(t) = At3 + Bt2 + Ct + D,
bx'(t) = 3At2 + 2Bt + C
Now finding a good starting value to plug into Newton's method is the tricky part. For most curves which do not contain loops or cusps, you can simply use the formula:
tn = ( cx - x0 ) / ( x3 - x0 ) | x0 < x1 < x2 < x3
Now you already have:
bx(tn) ≈ cx
So applying one or more iterations of Newton's method will give a better approximation of t for cx.
Note that the Newton Raphson algorithm has quadratic convergence. In most cases a good starting value will result in negligible improvement after two iterations, i.e. less than half a pixel.
Finally it's worth noting that cubic Bezier curves have exact solutions for finding extrema via finding roots of the first derivative. So curves which are problematic can simply be subdivided at their extrema to remove loops or cusps, then better results can be obtained by analyzing the resulting section in question. Subdividing cubics in this way will satisfy the above constraint.

Haversine formula

My question is about Haversine formula as arcsin x = arctan x/(sqrt(1-x^2), therefore the formula should have atan x and not atan2 x when we convert the formula from arcsin to arctan.
θ = 2 arcsin(sqrt(a)) = 2 arctan(sqrt(a)/(sqrt(1-a)))
But in most of the previews answers has an extra 2 after atan.
Can anyone explain it to me please. Thanks
atan(x) is the same as atan2(x,1). atan2(y,x) is the angle of the point (x,y), while atan(y/x) is the angle of the line through the origin and (x,y).
The only inverse trigonometric function present as FPU on x86 CPU/FPUinstruction is FPATAN which implements atan2. Thus also asin(x) is implemented as atan2(x,sqrt(1-x*x)), acos(x) as atan2(sqrt(1-x*x),x).

Hermit Spline Tangents estimation

Hermite Spline tangent estimation
I'm trying to come up with an algorithm or method that will allow me to estimate the tangent's magnitude (the direction is given) such as the interpolated spline will be the best fit for a given curve (blue dots)
In general points are in 3D space, but even a 2D solution will put me on track.
this is the Hermit Spline form:
Vector3D p0, p1, m0 (tangent at p0), m1 (tangent at p1)
p = (2*t^3 - 3*t^2 + 1)*p0 + (t^3-2*t^2+t)*m0 + (3*t^2 - 2*t^3)*p1 + (t^3-t^2)*m1;
My intuition suggest me some sort of least squares method using m0 and m1 magnitudes as the unknowns (could I apply least squares to each coordinates ecuation?) but somehow it should involve projecting and transforming that spline equation over the vector p1-p0 and instead of returning a 3D point, just the distance of that point to the base p1-p0. But I guess I got lost there :(
Perhaps the solution is easy and I would really love some light into this darkness.
Thanks in advance for your time!

What exactly does delta mean in the gradient descent algorithm?

As on the picture:
Could someone help me understand what exactly what delta means in the gradient descent algorithm?
This is a partial derivative with respect to theta_0.
The term is a derivative with respect to the theta 0.
Mark theta as coordinate on X-axis (let it be A)
Find corresponding coordinate on Y-axis (let it be B) so the point belongs to the function J
Draw tangent line to that function at the point (A, B)
The derivative is the slope of this tangent line.
The derivative is used to control two aspects of the cost function (J function) minimization:
direction - sign of the slope tells you in which direction you should move along the X-axis in order to converge J
rate - magnitude of the slope tells you how fast you should move

Possible infinite loop on math equation?

I have the following problem, and am having trouble understanding part of the equation:
Monte Carlo methods to estimate an integral is basically, take a lot of random samples and determined a weighted average. For example, the integral of f(x) can be estimated from N independent random samples xr by
alt text http://www.goftam.com/images/area.gif
for a uniform probability distribution of xr in the range [x1, x2]. Since each
function evaluation f(xr) is independent, it is easy to distribute this work
over a set of processes.
What I don't understand is what f(xr) is supposed to do? Does it feed back into the same equation? Wouldn't that be an infinite loop?
It should say f(xi)
f() is the function we are trying to integrate via the numerical monte carlo method, which estimates an integral (and its error) by evaluating randomly choosen points from the integration region.
Ref.
Your goal is to compute the integral of f from x1 to x2. For example, you may wish to compute the integral of sin(x) from 0 to pi.
Using Monte Carlo integration, you can approximate this by sampling random points in the interval [x1,x2] and evaluating f at those points. Perhaps you'd like to call this MonteCarloIntegrate( f, x1, x2 ).
So no, MonteCarloIntegrate does not "feed back" into itself. It calls a function f, the function you are trying to numerically integrate, e.g. sin.
Replace f(x_r) by f(x_r_i) (read: f evaluated at x sub r sub i). The r_i are chosen uniformly at random from the interval [x_1, x_2].
The point is this: the area under f on [x_1, x_2] is equal to (x_2 - x_1) times the average of f on the interval [x_1, x_2]. That is
A = (x_2 - x_1) * [(1 / (x_2 - x_1)) * int_{x_1}^{x_2} f(x)\, dx]
The portion in square brackets is the average of f on [x_1, x_2] which we will denote avg(f). How can we estimate the average of f? By sampling it at N random points and taking the average value of f evaluated at those random points. To wit:
avg(f) ~ (1 / N) * sum_{i=1}^{N} f(x_r_i)
where x_r_1, x_r_2, ..., x_r_N are points chosen uniformly at random from [x_1, x_2].
Then
A = (x_2 - x_1) * avg(f) ~ (x_2 - x_1) * (1 / N) * sum_{i=1}^{N} f(x_r_i).
Here is another way to think about this equation: the area under f on the interval [x_1, x_2] is the same as the area of a rectangle with length (x_2 - x_1) and height equal to the average height of f. The average height of f is approximately
(1 / N) * sum_{i=1}^{N} f(x_r_i)
which is value that we produced previously.
Whether it's xi or xr is irrelevant - it's the random number that we're feeding into function f().
I'm more likely to write the function (aside from formatting) as follows:
(x2-x1) * sum(f(xi))/N
That way, we can see that we're taking the average of N samples of f(x) to get an average height of the function, then multiplying by the width (x2-x1).
Because, after all, integration is just calculating area under the curve. (Nice pictures at http://hyperphysics.phy-astr.gsu.edu/Hbase/integ.html#c4.
x_r is a random value from the integral's range.
Substituting Random(x_1, x_2) for x_r would give an equivalent equation.

Resources