Beispiel #1
0
// Build splines based on quadratics
// Does curve fitting witih quadratics for the form:
// a*x*x + b*x + c = y
// 1. First Segment - (x0, y0),(x1, y1)
//    a. Assume a == 0
//    b. Set up the first two points
//    c. Compute first derivative (slope) y'[1]
//    d. Plot (will be a line)
// 2. For points i = 2; i < n; i++
//    a. Use known derivative from previous step
//    b. a*x[i-1]*x[i-1] + b*x[i-1] + c = y[i-1]
//    c. a*x[i]*x[i] + b*x[i] + c = y[i]
//    d. 2*a*x[i-1] + b = y'[i-1]
//    e. Solve equations.
//    f. y'[i] ;= 2*a*x[i] + b
//    g. plot
func (d *Doodle) GraphQuadSpline(list List) {
	bug.Where()
	n := len(list)
	m := make(Matrix, 3)
	bug.Pd("n", n)
	m[0] = FillPoly(list[0], 3)
	m[1] = FillPoly(list[1], 3)
	m[2] = FirstSlope()
	fmt.Printf("m=%v\n", m)
	v := m.GaussianElimination()
	fmt.Printf("m=%v\n", m)
	fmt.Printf("v=%v\n", v)
	d.Plot(func(x float64) float64 {
		return Poly(v, x)
	}, list[0].X, list[1].X)
	for i := 2; i < n; i++ {
		m[0] = FillPoly(list[i-1], 3)
		m[1] = FillPoly(list[i], 3)
		m[2] = Derivative(list[i-1].X, v[0], v[1])
		fmt.Printf("m=%v\n", m)
		v = m.GaussianElimination()
		fmt.Printf("m=%v\n", m)
		fmt.Printf("v=%v\n", v)
		d.Plot(func(x float64) float64 {
			return Poly(v, x)
		}, list[i-1].X, list[i].X)
	}
}
Beispiel #2
0
// Build splines based on cubic equations - attempt A
// The four equations are:
// 1. cubic(x-1)
// 2. cubic(x)
// 3. cubic(x+1)
// 4. 1st derivative
func (d *Doodle) GraphCubicSplineA(list List) {
	bug.Where()
	n := len(list)
	m := make(Matrix, 4)
	bug.Pd("n", n)
	m[0] = FillPoly(list[0], 4)
	m[1] = FillPoly(list[1], 4)
	m[2] = FillPoly(list[2], 4)
	m[3] = FillPoly(list[3], 4)
	fmt.Printf("m=%v\n", m)
	v := m.GaussianElimination()
	fmt.Printf("m=%v\n", m)
	fmt.Printf("v=%v\n", v)
	d.Plot(func(x float64) float64 {
		return Poly(v, x)
	}, list[0].X, list[1].X)
	for i := 2; i < n-1; i++ {
		m[0] = FillPoly(list[i-1], 4)
		m[1] = FillPoly(list[i], 4)
		m[2] = FillPoly(list[i+1], 4)
		m[3] = FirstDerivativeA(list[i-1].X, v[0], v[1], v[2])
		//m[2] = FirstDerivative(list[i-1].X, v[0], v[1], v[2])
		//m[3] = SecondDerivative(list[i-1].X, v[0], v[1])
		fmt.Printf("m=%v\n", m)
		v = m.GaussianElimination()
		fmt.Printf("m=%v\n", m)
		fmt.Printf("v=%v\n", v)
		d.Plot(func(x float64) float64 {
			return Poly(v, x)
		}, list[i-1].X, list[i].X)
	}
}