// Text p. 45 shows quadratic is special case of Func3. // This indicates a test case for Func3 func TestFunc3(t *testing.T) { f0 := func(x float64) float64 { return x * x } f1 := func(x float64) float64 { return x } f2 := func(x float64) float64 { return 1 } a, b, c := fit.Func3(qdata, f0, f1, f2) if a != -1 || b != -2 || c != 2 { t.Fatal(a, b, c) } }
func ExampleFunc3() { // Example 4.c, p. 44. data := []struct{ X, Y float64 }{ {3, .0433}, {20, .2532}, {34, .3386}, {50, .3560}, {75, .4983}, {88, .7577}, {111, 1.4585}, {129, 1.8628}, {143, 1.8264}, {160, 1.2431}, {183, -.2043}, {200, -1.2431}, {218, -1.8422}, {230, -1.8726}, {248, -1.4889}, {269, -.8372}, {290, -.4377}, {303, -.3640}, {320, -.3508}, {344, -.2126}, } // fix up data to have X in radians for i := range data { data[i].X *= math.Pi / 180 } f0 := math.Sin f1 := func(x float64) float64 { return math.Sin(2 * x) } f2 := func(x float64) float64 { return math.Sin(3 * x) } a, b, c := fit.Func3(data, f0, f1, f2) // output four decimal places corresponding to precision of Y values. fmt.Printf("%.4f, %.4f, %.4f\n", a, b, c) // Output: // 1.2000, -0.7700, 0.3900 }