github.com/gopherd/gonum@v0.0.4/integrate/quad/example_test.go (about)

     1  // Copyright ©2015 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 quad_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"runtime"
    11  
    12  	"github.com/gopherd/gonum/integrate/quad"
    13  	"github.com/gopherd/gonum/stat/distuv"
    14  )
    15  
    16  func Example() {
    17  	fmt.Println("Evaluate the expected value of x^2 + 3 under a Weibull distribution")
    18  	f := func(x float64) float64 {
    19  		d := distuv.Weibull{Lambda: 1, K: 1.5}
    20  		return (x*x + 3) * d.Prob(x)
    21  	}
    22  	ev := quad.Fixed(f, 0, math.Inf(1), 10, nil, 0)
    23  	fmt.Printf("EV with 10 points = %0.6v\n", ev)
    24  
    25  	ev = quad.Fixed(f, 0, math.Inf(1), 30, nil, 0)
    26  	fmt.Printf("EV with 30 points = %0.6v\n", ev)
    27  
    28  	ev = quad.Fixed(f, 0, math.Inf(1), 100, nil, 0)
    29  	fmt.Printf("EV with 100 points = %0.6v\n", ev)
    30  
    31  	ev = quad.Fixed(f, 0, math.Inf(1), 10000, nil, 0)
    32  	fmt.Printf("EV with 10000 points = %0.6v\n\n", ev)
    33  
    34  	fmt.Println("Estimate using parallel evaluations of f.")
    35  	concurrent := runtime.GOMAXPROCS(0)
    36  	ev = quad.Fixed(f, 0, math.Inf(1), 100, nil, concurrent)
    37  	fmt.Printf("EV = %0.6v\n", ev)
    38  	// Output:
    39  	// Evaluate the expected value of x^2 + 3 under a Weibull distribution
    40  	// EV with 10 points = 4.20175
    41  	// EV with 30 points = 4.19066
    42  	// EV with 100 points = 4.19064
    43  	// EV with 10000 points = 4.19064
    44  	//
    45  	// Estimate using parallel evaluations of f.
    46  	// EV = 4.19064
    47  }