Exemplo n.º 1
0
func TestRootFdf(t *testing.T) {

	var maxIter int = 100
	var x float64 = 5.0
	var rExpected float64 = math.Sqrt(5.0)

	qp := &QuadraticParams{
		A: 1.0,
		B: 0.0,
		C: -5.0,
	}

	f := &gogsl.GslFunctionFdf{
		Function:   Quadratic,
		Derivative: QuadraticDeriv,
		Fdf:        QuadraticFdf,
		Params:     qp,
	}
	gogsl.InitializeGslFunctionFdf(f)

	T := root.GSL_ROOT_FDFSOLVER_NEWTON
	s := root.FdfsolverAlloc(T)
	root.FdfsolverSet(s, f, x)

	fmt.Printf("using %s method\n", root.FdfsolverName(s))
	fmt.Printf("%-5s %10s %10s %10s\n", "iter", "root", "err", "err(est)")

	iter := 0
	for {
		iter += 1
		root.FdfsolverIterate(s)
		x0 := x
		x = root.FdfsolverRoot(s)
		status := gogsl.GslError(root.TestDelta(x, x0, 0, 1e-3))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Converged:\n")
		}
		fmt.Printf("%5d %10.7f %+10.7f %10.7f\n", iter, x, x-rExpected, x-x0)
		if status != gogsl.GSL_CONTINUE || iter >= maxIter {
			break
		}
	}
}
Exemplo n.º 2
0
Arquivo: root.go Projeto: dtromb/gogsl
func FdfsolverSet(s *GslRootFdfsolver, fdf *gogsl.GslFunctionFdf, root float64) int32 {
	gogsl.InitializeGslFunctionFdf(fdf)
	return int32(C.gsl_root_fdfsolver_set((*C.gsl_root_fdfsolver)(unsafe.Pointer(s.Ptr())), (*C.gsl_function_fdf)(unsafe.Pointer(fdf.CPtr())), C.double(root)))
}