github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/interp/interp_example_test.go (about)

     1  // Copyright ©2020 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package interp_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"os"
    11  	"text/tabwriter"
    12  
    13  	"github.com/jingcheng-WU/gonum/interp"
    14  )
    15  
    16  func ExamplePredictor() {
    17  	// An example of fitting different interpolation
    18  	// algorithms to (X, Y) data with widely varying slope.
    19  	//
    20  	// Cubic interpolators have to balance the smoothness
    21  	// of the generated curve with suppressing ugly wiggles
    22  	// (compare the output of AkimaSpline with that of
    23  	// FritschButland).
    24  	xs := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
    25  	ys := []float64{0, 0.001, 0.002, 0.1, 1, 2, 2.5, -10, -10.01, 2.49, 2.53, 2.55}
    26  
    27  	var pc interp.PiecewiseConstant
    28  	var pl interp.PiecewiseLinear
    29  	var as interp.AkimaSpline
    30  	var fb interp.FritschButland
    31  
    32  	predictors := []interp.FittablePredictor{&pc, &pl, &as, &fb}
    33  	for i, p := range predictors {
    34  		err := p.Fit(xs, ys)
    35  		if err != nil {
    36  			panic(fmt.Sprintf("Error fitting %d-th predictor: %v", i, err))
    37  		}
    38  	}
    39  
    40  	n := len(xs)
    41  	dx := 0.25
    42  	nPts := int(math.Round(float64(n-1)/dx)) + 1
    43  
    44  	w := tabwriter.NewWriter(os.Stdout, 8, 0, 1, ' ', tabwriter.AlignRight)
    45  	fmt.Fprintln(w, "x\tPC\tPL\tAS\tFB\t")
    46  	for i := 0; i < nPts; i++ {
    47  		x := xs[0] + float64(i)*dx
    48  		fmt.Fprintf(w, "%.2f", x)
    49  		for _, predictor := range predictors {
    50  			y := predictor.Predict(x)
    51  			fmt.Fprintf(w, "\t%.2f", y)
    52  		}
    53  		fmt.Fprintln(w, "\t")
    54  	}
    55  	fmt.Fprintln(w)
    56  	w.Flush()
    57  	// Output:
    58  	//        x      PC      PL      AS      FB
    59  	//     0.00    0.00    0.00    0.00    0.00
    60  	//     0.25    0.00    0.00    0.00    0.00
    61  	//     0.50    0.00    0.00    0.00    0.00
    62  	//     0.75    0.00    0.00    0.00    0.00
    63  	//     1.00    0.00    0.00    0.00    0.00
    64  	//     1.25    0.00    0.00    0.00    0.00
    65  	//     1.50    0.00    0.00    0.00    0.00
    66  	//     1.75    0.00    0.00    0.00    0.00
    67  	//     2.00    0.00    0.00    0.00    0.00
    68  	//     2.25    0.10    0.03   -0.01    0.01
    69  	//     2.50    0.10    0.05   -0.01    0.03
    70  	//     2.75    0.10    0.08    0.02    0.06
    71  	//     3.00    0.10    0.10    0.10    0.10
    72  	//     3.25    1.00    0.33    0.26    0.22
    73  	//     3.50    1.00    0.55    0.49    0.45
    74  	//     3.75    1.00    0.78    0.75    0.73
    75  	//     4.00    1.00    1.00    1.00    1.00
    76  	//     4.25    2.00    1.25    1.24    1.26
    77  	//     4.50    2.00    1.50    1.50    1.54
    78  	//     4.75    2.00    1.75    1.75    1.79
    79  	//     5.00    2.00    2.00    2.00    2.00
    80  	//     5.25    2.50    2.12    2.22    2.17
    81  	//     5.50    2.50    2.25    2.37    2.33
    82  	//     5.75    2.50    2.38    2.47    2.45
    83  	//     6.00    2.50    2.50    2.50    2.50
    84  	//     6.25  -10.00   -0.62    0.83    0.55
    85  	//     6.50  -10.00   -3.75   -2.98   -3.75
    86  	//     6.75  -10.00   -6.88   -7.18   -8.04
    87  	//     7.00  -10.00  -10.00  -10.00  -10.00
    88  	//     7.25  -10.01  -10.00  -11.16  -10.00
    89  	//     7.50  -10.01  -10.00  -11.55  -10.01
    90  	//     7.75  -10.01  -10.01  -11.18  -10.01
    91  	//     8.00  -10.01  -10.01  -10.01  -10.01
    92  	//     8.25    2.49   -6.88   -7.18   -8.06
    93  	//     8.50    2.49   -3.76   -2.99   -3.77
    94  	//     8.75    2.49   -0.63    0.82    0.53
    95  	//     9.00    2.49    2.49    2.49    2.49
    96  	//     9.25    2.53    2.50    2.50    2.51
    97  	//     9.50    2.53    2.51    2.51    2.52
    98  	//     9.75    2.53    2.52    2.52    2.52
    99  	//    10.00    2.53    2.53    2.53    2.53
   100  	//    10.25    2.55    2.53    2.54    2.54
   101  	//    10.50    2.55    2.54    2.54    2.54
   102  	//    10.75    2.55    2.54    2.55    2.55
   103  	//    11.00    2.55    2.55    2.55    2.55
   104  }