6
$\begingroup$

How to find Centroid of the following curve?

curve = 
 Plot[Cos[x]/(x + Log[x]), {x, 1, 8}, Ticks -> None, PlotStyle -> {Blue, Thick}]

enter image description here

I tried:

Graphics[{curve, Red, Point[RegionCentroid[curve]]}]

but it doesn't work

$\endgroup$
  • 1
    $\begingroup$ Have you tried with ParametricRegion[]? RegionCentroid[] works on regions (as the function name implies) and not on Graphics[] objects. $\endgroup$ – J. M. will be back soon Dec 19 '16 at 11:28
  • 1
    $\begingroup$ Though it does work on graphics primitives (second example in the RegionCentroid documentation) $\endgroup$ – lowriniak Dec 19 '16 at 11:42
8
$\begingroup$

For those, who wonder what we would do without RegionCentroid, there is the simple way of calculating the centre by normal integration. When we consider your function $f$ as parametric curve the situation is clear.

$$x(t)=t\\y(t)=\frac{\cos(t)}{t+\log(t)}$$

Furthermore, we need the arc length $L(f)$ of the curve which can be calculated with

$$ds=\sqrt{x'(t)^2+y'(t)^2}\;dt$$

Then your centre $(x_m, y_m)$ is given by

$$x_m\cdot L(f)=\int x\;ds\qquad y_m\cdot L(f)=\int y\;ds$$

For your particular case this means the x-coordinate of your centroid is given by

$$x_m = \left(\int_1^8 t\; ds\right)/(\int_1^8ds)$$

In Mathematica code

x[t_] := t;
y[t_] := Cos[t]/(t + Log[t]);

ds = Sqrt[x'[t]^2 + y'[t]^2];

NIntegrate[{x[t], y[t]} ds, {t, 1, 8}]/NIntegrate[ds, {t, 1, 8}]

(* {4.38467, -0.00815125} *)
$\endgroup$
  • 2
    $\begingroup$ It should be:NIntegrate[{x*Sqrt[1 + dydx^2], y[x]*Sqrt[1 + dydx^2]}, {x, 1, 8}]/ NIntegrate[Sqrt[1 + dydx^2], {x, 1, 8}] ,{4.38467, -0.00815125} $\endgroup$ – Mariusz Iwaniuk Dec 19 '16 at 12:38
  • $\begingroup$ @MariuszIwaniuk I'm sorry. I didn't look it up. I'm fixing the post. Thanks for being attentive. $\endgroup$ – halirutan Dec 19 '16 at 12:59
7
$\begingroup$

To expand on JMs comment, you will need to use the correct predicates to build the region (I used ImplicitRegion because I find it most flexible):

region = ImplicitRegion[y == (Cos[x]/(x + Log[x])) && x > 1 && x < 8, {x, y}];

Then you can use RegionCentroid on it:

centroid = RegionCentroid[DiscretizeRegion@region];
Show[RegionPlot[region], ListPlot[centroid], PlotLabel -> centroid]

Centroid

$\endgroup$
6
$\begingroup$

Simplest is the numerical approach for Graphics objects, which is to use DiscretizeGraphics:

curve = Plot[Cos[x]/(x + Log[x]), {x, 1, 8}, Ticks -> None, PlotStyle -> {Blue, Thick}];
RegionCentroid[DiscretizeGraphics[curve]]

{4.3846982827317245, -0.008143914969936251}

Note, while you can get lucky with analytical results too sometimes:

RegionCentroid[ParametricRegion[{x, x^2}, {{x, 1, 8}}]]

$$\left\{\frac{257 \sqrt{257}-5 \sqrt{5}}{3 \left(-2 \sqrt{5}+16 \sqrt{257}-\sinh ^{-1}(2)+\sinh ^{-1}(16)\right)},\frac{-18 \sqrt{5}+8208 \sqrt{257}+\sinh ^{-1}(2)-\sinh ^{-1}(16)}{16 \left(-2 \sqrt{5}+16 \sqrt{257}-\sinh ^{-1}(2)+\sinh ^{-1}(16)\right)}\right\}$$

Generally it is not the case:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.