Mathematica Asked on May 1, 2021
I am trying to find the barycenter of some spectra using numerical integration. I tried to find the wavelength for which the integral splits the spectrum in two equal areas, but the result seems wrong. The code returns the value of about 670 nm, while I was expecting it to be around 663 nm.
Code:
Data = Import["https://pastebin.com/raw/kEQd5uPF", "Table"];
Do[Spec[2*i] = Data[[All, (2*i + 1) ;; (2*i + 2)]], {i, 0, 4}];
[Lambda] =
b /. NSolve[
Integrate[
Interpolation[Spec[0], InterpolationOrder -> 3][x], {x,
Min[Spec[0][[All, 1]]], b}] ==
Integrate[
Interpolation[Spec[0], InterpolationOrder -> 3][x], {x, b,
Max[Spec[0][[All, 1]]]}], b][[1]];
vertLines = {Thick, ColorData["VisibleSpectrum"][[Lambda]],
Line[{{[Lambda], 0}, {[Lambda], Max[Spec[0][[All, 2]]]}}]};
ListPlot[Spec[0], PlotRange -> All, Joined -> True,
Prolog -> vertLines]
In the picture, it looks as if the second area is smaller. How can I find the correct barycenter wavelength?
A pedestrian approach: implement a trapezoidal rule:
i = 0;
data = Data[[All, (2*i + 1) ;; (2*i + 2)]];
list = 1/2 Table[(data[[i, 2]] + data[[i + 1, 2]])*(data[[i, 1]] -
data[[i + 1, 1]]), {i, 1, Length@data - 1}] // Abs;
The integral of the absolute value is given by Total@list
. Find the position of the first point to reach half this sum:
First@data[[First@First@Position[Accumulate[list], _?(# > Total[list]/2 &), 1, 1]]]
(* 663.336 *)
You could refine this value by taking a pro rata with the previous term but given the scattered data, I don't think that's relevant.
Answered by anderstood on May 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP