1
$\begingroup$

If I have the following data:

 lengthscale1SB = {{0, 3.2921487465109793`}};
lengthscale2SB = {{100, 2.9436520687559216`}};
lengthscalemix1SB = {{79, 2.495395024671054`}};
lengthscalemix2SB = {{30, 3.333075533653907`}};
lengthscalemix3SB = {{45, 3.1442955345260786`}};
lengthscalemix4SB = {{88, 2.009572697828747`}};
lengthscalemix5SB = {{15, 3.3506633046912224`}};

How can I fit it to a cubic spline?

The idea is to try to fit or provide a guide to the eye line to that data perhaps similar to this:

enter image description here

(* Plot can be generated as:

Show[
 ListPlot[List /@ {lengthscale1SB[[1]]}, Frame -> True, 
  FrameStyle -> 16, Axes -> False, GridLines -> Automatic, 
  GridLinesStyle -> Lighter[Gray, .8], 
  FrameTicks -> {Automatic, Automatic}, ImageSize -> Large, 
  LabelStyle -> {Black, Bold, 14}, PlotStyle -> {Red, Cyan, Gray}, 
  PlotLegends -> 
   Placed[PointLegend[Automatic, Defer /@ qDSClabel, 
     LegendMarkers -> {Row[{Style["\[FilledCircle]", 12], 
         Style["\[FilledUpTriangle]", 16]}, Spacer[2]], 12}, 
     LegendMarkerSize -> {30, 20}, Spacings -> {.8, 0}], {0.12, 
     0.25}]],
 
 ListPlot[List /@ {lengthscale2SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 ListPlot[List /@ {lengthscalemix1SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 ListPlot[List /@ {lengthscalemix2SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 ListPlot[List /@ {lengthscalemix3SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 ListPlot[List /@ {lengthscalemix4SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 ListPlot[List /@ {lengthscalemix5SB[[1]]}, 
  PlotStyle -> {Red, Cyan, Gray}],
 
 PlotRange -> {{-1, 101}, All}
 
 ]

*)

$\endgroup$
5
  • 1
    $\begingroup$ Did you see the Splines methods: reference.wolfram.com/language/guide/Splines.html? $\endgroup$
    – Moo
    Commented Jul 1, 2021 at 21:06
  • 2
    $\begingroup$ There’s a resource function for that! It is called CubicSplineInterpolation. From the documentation page, it appears that many cubic spline methods are implemented within it! $\endgroup$ Commented Jul 1, 2021 at 23:29
  • $\begingroup$ I guess, the simplest way is to use the BSplineCurve@jointarray in Epilog. Where the jointarray is a list with all desired points included. $\endgroup$
    – Rom38
    Commented Jul 2, 2021 at 7:58
  • $\begingroup$ @Rom38 could you give me an example of how to do that? I specifically would want to do it with a Spline function/curve. $\endgroup$
    – John
    Commented Jul 2, 2021 at 13:59
  • $\begingroup$ Just to second @CATrevillian 's comment: If you want to connect the points with a smooth curve (and hopefully have some feeling the resulting curve makes sense), then resource function CubicSplineInterpolation is what you want. If the data is put into a single list named data, then the following will do what you want: f = ResourceFunction["CubicSplineInterpolation"][data]; Show[ListPlot[data], Plot[f[x], {x, 0, 100}]]. $\endgroup$
    – JimB
    Commented May 18, 2022 at 2:44

1 Answer 1

2
$\begingroup$

The usage of BSpline is simple:

  1. Just mix your points into the joint array

    ar1=SortBy[
    Flatten[{
        lengthscale1SB,
        lengthscale2SB,
        lengthscalemix1SB,
        lengthscalemix2SB, 
        lengthscalemix3SB,
        lengthscalemix4SB,
        lengthscalemix5SB,1],
    First];
    
  2. Draw it by BSplineCurve of desired order

    ListPlot[ar1,
     Frame -> True,
     PlotStyle -> Blue,
     Epilog -> {Dashed,       
                Red,BSplineCurve[ar1[[1 ;; -2]],SplineDegree->3],
                Black,Line@ar1[[-2 ;; -1]]}]
    

enter image description here

  1. You can access the spline points by

    bsf = BSplineFunction[ar1[[1 ;; -2]], 
                          SplineDegree -> 3]
    bsf[0.5]
    

{38.0937, 3.21896}

Here 0.5 is parametric argument for spline that should be in the range [0,1]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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