PlanckConstant=6.62606957 (10^(-34));
PlanckConstantReduced=(PlanckConstant/(2  π));
FineStructureConstant=7.2973525698 (10^(-3));
ElectronMass=9.10938291 (10^(-31));
GravitationalConstant=6.67384 (10^(-11));
ProtonMass=1.672621777 (10^(-27));
AtomicMassUnit=1.66053878283 (10^(-27));
BohrRadius=0.52917721092 (10^(-10));
BohrMagneton=927.400968 (10^(-26));
BoltzmannConstant=1.3806488 (10^(-23));
StandardGravityAcceleration=9.80665;
SpeedOfLight=299792458;
StefanBoltzmannConstant=5.670373 (10^(-8));
ElectronCharge=1.602176565 (10^(-19));
VacuumPermeability=4π (10^(-7));
DielectricConstant=(1/((SpeedOfLight^2) VacuumPermeability));
ElectronGyromagneticFactor=-2.00231930436153;
AvogadroConstant=6.02214129 (10^23);

scatteringLength[B_]:=Module[{abkg,resonanceB,resonancewB,as},(*Set background scattering length*)abkg=85.5*BohrRadius;(*BohrRadius should be defined beforehand*)(*Resonance positions and widths*)resonanceB={1.295,1.306,2.174,2.336,2.591,2.74,2.803,2.78,3.357,4.949,5.083,7.172,7.204,7.134,76.9};
resonancewB={0.009,0.010,0.0005,0.0005,0.001,0.0005,0.021,0.015,0.043,0.0005,0.130,0.024,0.0005,0.036,3.1};
(*Get scattering length*)as=abkg*Product[(1-resonancewB[[j]]/(B-resonanceB[[j]])),{j,1,Length[resonanceB]}];
(*Return scattering length*)as/BohrRadius]
Plot[scatteringLength[b], {b, 0, 8}, PlotRange->{{0,2.5},{0,150}},FrameLabel->{"B (G)","Scattering Length (x BohrRadius)"},PlotRangeClipping->True,LabelStyle->{FontSize->14},Frame->True, PlotTheme->"Classic",TicksStyle->{FontSize->14},PlotStyle->{Thick},GridLines->Automatic,ImageSize->{800,495},AspectRatio->Full]

Plot[scatteringLength[b], {b, 0, 8}, PlotRange->{{1.317,2.173},{0,150}},FrameLabel->{"B (G)","Scattering Length (x BohrRadius)"},PlotRangeClipping->True,LabelStyle->{FontSize->14},Frame->True, PlotTheme->"Classic",TicksStyle->{FontSize->14},PlotStyle->{Thick},GridLines->Automatic,ImageSize->{800,495},AspectRatio->Full]

scatteringLength[1.317];
scatteringLength[2.173];
BValues=Range[1.317,2.173,0.001]; (*Define range for B*)
asValues=scatteringLength/@BValues; (*Compute scattering length for each value of B*)
ListLinePlot[Transpose[{asValues,BValues}],FrameLabel->{"Scattering Length (x BohrRadius)","B (G)"},LabelStyle->{FontSize->14},Frame->True, PlotTheme->"Classic",TicksStyle->{FontSize->14},PlotStyle->{Thick},GridLines->Automatic,ImageSize->{800,495},AspectRatio->Full]

targetas=71.54;(*Define the target scattering length*)
closestIndex=First[Ordering[Abs[asValues-targetas],1]]; (*Find the closest value in a_sValues and return the corresponding B value*)
closestB=BValues[[closestIndex]]; (*Index of closest a_s value*)(*Retrieve the corresponding B value*)
closestB (*Print the closest B value*)