For an assignment I have to use numerical integration technique to calculate volume with cylindrical surface
Ω={(x,y,z) in R³ with (x−0.5)² +(y−0.5)² ≤ 0.5²
and 0 ≤ z ≤ |ln(x+y)|}.
I have used Monte Carlo technique to calculate the volume. But to be sure the answer is correct I want to check the exact volume using Maple. I have been searching online on how to do it but couldn't find it.
So the question is, is there a way to calculate exact volume using Maple for that object or integral like
this:
The volume is the integral of
V = int_0^1
int_{0.5-sqrt(0.5^2-(x-0.5)^2)}^{0.5+sqrt(0.5^2-(x-0.5)^2)}
int_0^{abs(log(x+y))}
1 dz dy dx
or, after a change of variables,
V = int_-1^1
int_{-sqrt(1-x^2)}^{+sqrt(1-x^2)}
0.25 * abs(log(x/2+y/2+1)) dy dx
Computer algebra systems can compute the value to
V = 0.25502
(e.g., https://www.wolframalpha.com/input/?i=int_-1%5E1+int_%7B-sqrt(1-x%5E2)%7D%5E%7B%2Bsqrt(1-x%5E2)%7D+1%2F4+*+abs(log(x%2F2%2By%2F2%2B1))+dy+dx).
Related
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.
I am looking for an algorithm in R to intersect a convex polytope with a line segment. I found several post here on stack exchange for in the plane but I am wondering if this algorithms exists in higher dimensions. My google searches didn't really produce a lot of answers.
The line segment is composed of a point inside and a point outside the convex polytope. Are there algorithms in R available that can do this in dimension N<=10 ? Or does anyone know a reference so I can implement the algorithm myself? Is there information on the complexity of finding the polytope and the intersection ?
For problems in computational geometry, dimension d > 3 usually might as well be d arbitrary. If you have the polytope as a collection of intersected halfspaces, then likely the only sensible thing to do is to intersect the line segment with each of the separating hyperplanes (by solving a system of d linear equations) and take the intersection closest to the point inside.
If you have only the vertices of the polytope or even just a set of vertices whose convex closure is the polytope, then the easiest approach given R's libraries probably is linear programming. (Conceivably you could compute the facets using an algorithm to find high-dimensional convex hulls, but there could be Theta(n^floor(d/2)) of them, where n is the number of vertices.) I'm not familiar with LP solvers in R, so I'll write down the program mathematically. It shouldn't be too hard to translate. Let p_0 be the point outside and p_1 be the point inside and v_i be the ith point defining the polytope.
maximize alpha_0
subject to
for 1 <= j <= d,
p_0[j] alpha_0 + p_1[j] alpha_1 - sum_{1 <= i <= n} v_i[j] beta_i = 0
alpha_0 + alpha_1 = 1
sum_{1 <= i <= n} beta_i = 1
alpha_0 >= 0
alpha_1 >= 0
for 1 <= i <= n,
beta_i >= 0
The intersection is defined by the point p_0 alpha_0 + p_1 alpha_1 (unless the program is infeasible, in which case there is no intersection).
The density of my points x ∈ [0,R] is exponential: ρ(x)~e^x
How can I sample N points from there?
Taking your request at face value, if you want a density function that grows exponentially for x ∈ [0,R] the cumulative distribution function turns out to be (exp(x) - 1) / (exp(R) - 1). To generate this via inversion, set the CDF equal to a Uniform(0,1) and solve for x. The inversion turns out to be:
ln(1 + (exp(R) - 1) * U)
where U represents a call to the Uniform(0,1) PRNG.
If what you actually want is a truncated form of what most probability folks know as the exponential distribution, we need to determine an upper bound for the random number corresponding to your truncation point R. In that case, the inversion is:
-ln(1 - [1 - exp(-lambda * R)] * U) / lambda
As before, U represents a call to the Uniform(0,1) PRNG. This will generate exponentials at rate lambda, truncated at a max of R.
Use inverse sampling: you generate uniform distributed values and map them to the output of the cdf of your distribution.
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
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.