示例#1
0
func test1() {
	rand.Seed(int64(0))

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Plotutil example"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	err = plotutil.AddLinePoints(p,
		"First", randomPoints(15),
		"Second", randomPoints(15),
		"Third", randomPoints(15))
	if err != nil {
		panic(err)
	}

	// Save the plot to a PNG file.
	if err := p.Save(4, 4, "points.png"); err != nil {
		panic(err)
	}
}
示例#2
0
func main() {
	// Get some random data.
	n, m := 5, 10
	pts := make([]plotter.XYer, n)
	for i := range pts {
		xys := make(plotter.XYs, m)
		pts[i] = xys
		center := float64(i)
		for j := range xys {
			xys[j].X = center + (rand.Float64() - 0.5)
			xys[j].Y = center + (rand.Float64() - 0.5)
		}
	}

	plt, err := plot.New()
	if err != nil {
		panic(err)
	}

	mean95 := plotutil.NewErrorPoints(plotutil.MeanAndConf95, pts...)
	medMinMax := plotutil.NewErrorPoints(plotutil.MedianAndMinMax, pts...)
	plotutil.AddLinePoints(plt,
		"mean and 95% confidence", mean95,
		"median and minimum and maximum", medMinMax)
	plotutil.AddErrorBars(plt, mean95, medMinMax)
	plotutil.AddScatters(plt, pts[0], pts[1], pts[2], pts[3], pts[4])

	plt.Save(4, 4, "errpoints.png")
}
示例#3
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	A2 = make([]float64, 2)
	A3 = make([]float64, 3)

	A1 = 1
	A2[0] = 1.0 / (3.0 * LaguerreD(2, z2[0]) * Laguerre(3, z2[0]))
	A2[1] = 1.0 / (3.0 * LaguerreD(2, z2[1]) * Laguerre(3, z2[1]))

	A3[0] = 1.0 / (4.0 * LaguerreD(3, z3[0]) * Laguerre(4, z3[0]))
	A3[1] = 1.0 / (4.0 * LaguerreD(3, z3[1]) * Laguerre(4, z3[1]))
	A3[2] = 1.0 / (4.0 * LaguerreD(3, z3[2]) * Laguerre(4, z3[2]))

	pt = make(plotter.XYs, nPlot)
	x = make([]float64, nPlot)

	dx := (xmax - xmin) / float64(nPlot-1)
	for i := range x {
		x[i] = dx*float64(i) + xmin
		pt[i].X = x[i]
		pt[i].Y = (math.Gamma(x[i]))
	}

	var p1, p2, p3 plotter.XYs

	p1 = make(plotter.XYs, nPlot)
	p2 = make(plotter.XYs, nPlot)
	p3 = make(plotter.XYs, nPlot)

	for i := range x {
		p1[i].X = x[i]
		p2[i].X = x[i]
		p3[i].X = x[i]
		p1[i].Y = (A1 * math.Pow(z1, x[i]-1))
		p2[i].Y = (A2[0]*math.Pow(z2[0], x[i]-1) + A2[1]*math.Pow(z2[1], x[i]-1))
		p3[i].Y = (A3[0]*math.Pow(z3[0], x[i]-1) + A3[1]*math.Pow(z3[1], x[i]-1) + A3[2]*math.Pow(z3[2], x[i]-1))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Gamma Function Approximations")
	p.Y.Label.Text = "Log(y)"
	p.X.Label.Text = "x"

	plotutil.AddLinePoints(p, "Log(Gamma)", pt, "m=0", p1, "m=1", p2, "m=2", p3)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "gammaLow.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#4
0
文件: main.go 项目: nrocy/wav
func main() {
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, "Usage: simpleRead <file.wav>\n")
		os.Exit(1)
	}

	// open file
	testInfo, err := os.Stat(os.Args[1])
	checkErr(err)

	testWav, err := os.Open(os.Args[1])
	checkErr(err)

	wavReader, err := wav.NewWavReader(testWav, testInfo.Size())
	checkErr(err)

	// File informations
	fmt.Println(wavReader)

	// limit sample count
	sampleCnt := wavReader.GetSampleCount()
	if sampleCnt > 10000 {
		sampleCnt = 10000
	}

	// setup plotter
	p, err := plot.New()
	checkErr(err)

	p.Title.Text = "Waveplot"
	p.X.Label.Text = "t"
	p.Y.Label.Text = "Ampl"

	pts := make(plotter.XYs, sampleCnt)

	// read samples and construct points for plot
	for i := range pts {
		n, err := wavReader.ReadSample()
		if err == io.EOF {
			break
		}
		checkErr(err)

		pts[i].X = float64(i)
		pts[i].Y = float64(n)
	}

	err = plotutil.AddLinePoints(p, "", pts)
	checkErr(err)

	// construct output filename
	inputFname := path.Base(os.Args[1])
	plotFname := strings.Split(inputFname, ".")[0] + ".png"

	if err := p.Save(10, 4, plotFname); err != nil {
		panic(err)
	}
}
示例#5
0
文件: example.go 项目: stanim/vgximg
func main() {
	rand.Seed(int64(0))

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Plotutil example"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	err = plotutil.AddLinePoints(p,
		"First", randomPoints(15))
	if err != nil {
		panic(err)
	}
	cnvs, err := vgximg.New(4*96, 4*96, "Example")
	if err != nil {
		panic(err)
	}
	p.Draw(plot.MakeDrawArea(cnvs))
	cnvs.Paint()
	time.Sleep(5 * time.Second)

	err = plotutil.AddLinePoints(p,
		"Second", randomPoints(15),
		"Third", randomPoints(15))
	if err != nil {
		panic(err)
	}
	p.Draw(plot.MakeDrawArea(cnvs))
	cnvs.Paint()
	time.Sleep(10 * time.Second)

	// Save the plot to a PNG file.
	//        if err := p.Save(4, 4, "points.png"); err != nil {
	//                panic(err)
	//        }
}
示例#6
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	ev = make([]float64, 0)

	for n := nmin; n <= nmax; n *= 2 {
		sum := 0.0
		dx := (xmax - xmin) / float64(n)
		dy := (ymax - ymin) / float64(n)
		y = make([]float64, n)
		x = make([]float64, n)
		for k := 0; k < n; k++ {
			x[k] = float64(k)*dx + 0.5*dx + xmin
			y[k] = (float64(k)*dy + 0.5*dy + ymin)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				sum += math.Exp(x[i]*y[j]) * dx * dy
			}
		}
		ev = append(ev, EXACT_VALUE-sum)
	}

	var pt plotter.XYs
	pt = make(plotter.XYs, len(ev))
	for i := range pt {
		pt[i].X = math.Log10(float64(nmin) * math.Pow(2, float64(i)))
		pt[i].Y = math.Log10(math.Abs(ev[i]))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Error of Midpoint Integral")
	p.X.Label.Text = "n"
	p.Y.Label.Text = "Log(|err|)"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "error_hw12.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#7
0
func GenerateGraph(teamName string, pythagorean, reals []float64) {
	p, _ := plot.New()
	p.Title.Text = teamName
	p.X.Label.Text = "Day"
	p.Y.Label.Text = "Parcentage"
	p.X.Min = 0.0
	p.X.Max = 38.0
	p.Y.Min = 0.0
	p.Y.Max = 100.0
	plotutil.AddLinePoints(p, "", GeneratePlot(pythagorean), GeneratePlot(reals))
	width := 4.0
	height := 4.0
	p.Save(width, height, fmt.Sprintf("%s.png", teamName))
}
示例#8
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	fx = math.Pi / math.Sqrt2 / 4.0 //Exact integral value
	ev = make([]float64, 0)

	var sum, avg float64
	for n := nmin; n <= nmax; n *= 2 {
		avg = 0.0
		for j := 0; j < navg; j++ {
			sum = 0.0
			for i := 0; i < n; i++ {
				sum += F(rand.Float64()*(xmax-xmin)+xmin, rand.Float64()*(ymax-ymin)+ymin)
			}
			sum /= float64(n)
			avg += sum
		}
		avg /= float64(navg)
		ev = append(ev, math.Abs(avg-fx))
	}

	var pt plotter.XYs
	pt = make(plotter.XYs, len(ev))
	for i := range pt {
		pt[i].X = math.Log(float64(nmin) * math.Pow(2, float64(i)))
		pt[i].Y = math.Log(math.Abs(ev[i]))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Error of Monte Carlo Integral")
	p.X.Label.Text = "Log(n)"
	p.Y.Label.Text = "Log(|err|)"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "error.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#9
0
func PlotXvsY(title, directory string, dss []*rdb.Dataset, yValuesKey, yValuesLabel, xValuesKey, xValuesLabel string) {

	var plottingArgs []interface{}

	var targetIds []string

	for i, ds := range dss {

		values, ids := ds.GetDatasetFloatValuesPair(yValuesKey, xValuesKey)

		if i == 0 {
			targetIds = ids
		} else {
			ids, values = datautils.MatchKeys(targetIds, ids, values)
		}

		pts := make(plotter.XYs, len(values))
		for j := range pts {
			pts[j].X = float64(j)
			pts[j].Y = values[j]
		}
		plottingArgs = append(plottingArgs, ds.GetName(), pts)
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = title
	p.X.Label.Text = xValuesLabel
	p.Y.Label.Text = yValuesLabel

	err = plotutil.AddLinePoints(p, plottingArgs...)
	if err != nil {
		panic(err)
	}

	savePlot(title, directory, p)
}
示例#10
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	var pt plotter.XYs
	pt = make(plotter.XYs, maxCf)
	dx := (b - a) / float64(maxCf-1)
	x = make([]float64, maxCf)
	for i := range x {
		x[i] = math.Exp(a + dx*float64(i))
		pt[i].X = a + dx*float64(i)
		pt[i].Y = x[i]
	}

	for i := range x {

	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("exp(x)")
	p.X.Label.Text = "x"
	p.Y.Label.Text = "y"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "exp.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#11
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	h = 1.0
	F = math.Log
	x0 = 3
	fx = -1.0 / (x0 * x0)
	//
	//F = math.Tan
	//x0 = math.Asin(0.8)
	//fx = 2*math.Pow(math.Cos(x0),-2.0)*math.Tan(x0)
	//F = f3
	//fx = 2.0
	//x0 = 0

	var δ0, δn, β, e, df float64
	var δv []float64
	δv = make([]float64, (k-n)/l)
	ev = make([]float64, 0)
	var count int
	for j := n; j < k; j += l {
		δ0 = (F(x0+h) - 2*F(x0) + F(x0-h)) / (h * h)
		β = math.Pow(α, float64(n))
		for i := 1; i < count; i++ {
			e = δ0 - δv[i]
			δv[i] = δ0
			δn = δ0 + e*β/(1-β)
			df = math.Abs(δ0 - fx)
			if df <= math.Abs(tol*δ0) {
				break
			}
			β = β * math.Pow(α, float64(l))
			δv[count] = δn
		}
		if df < math.Abs(tol*δn) {
			break
		}
		if df > tol {
			ev = append(ev, df)
		}
		h *= α
		count++
	}
	//fmt.Printf("Number of iterations:%d\n",count)
	fmt.Printf("Deviation from true value: %e\n", math.Abs(fx-δn))

	//fmt.Println(ev)

	var pt plotter.XYs
	pt = make(plotter.XYs, len(ev))
	for i := range pt {
		pt[i].X = float64(i)
		pt[i].Y = math.Log10(math.Abs(ev[i]))
		//fmt.Println(math.Log(math.Abs(ev[i])))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Error of 2nd Derivative Approximation, Log(x)")
	p.X.Label.Text = "n"
	p.Y.Label.Text = "Log(|err|)"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "error.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#12
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	//Initialize variables, define FFTW plans
	a = -1.0
	b = 1.0
	x = make([]float64, nx)
	f = fftw.Alloc1d(nx)
	fftx = fftw.Alloc1d(nx)
	var pt, ptf plotter.XYs
	pt = make(plotter.XYs, nx)
	ptf = make(plotter.XYs, nx)

	forward := fftw.PlanDft1d(f, fftx, fftw.Forward, fftw.Estimate)
	backward := fftw.PlanDft1d(fftx, f, fftw.Backward, fftw.Estimate)

	for i := range x {
		x[i] = (b-a)*float64(i)/float64(nx-1) + a
		f[i] = complex(sign(x[i]), 0.0)
	}
	// Transforms data, in place
	forward.Execute()

	for i := range pt {
		pt[i].X = float64(i)
		pt[i].Y = cmplx.Abs(fftx[i])
		ptf[i].X = x[i]
		ptf[i].Y = real(f[i])
	}

	//Plotting Stuff

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	pf, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Power Spectrum of sign(x)"
	p.X.Label.Text = "n"
	p.Y.Label.Text = "Power"
	plotutil.AddLinePoints(p, pt)
	if err := p.Save(6, 4, "prefilterp.png"); err != nil {
		panic(err)
	}

	pf.Title.Text = "sign(x), No filter"
	pf.X.Label.Text = "x"
	pf.Y.Label.Text = "sign(x)"
	plotutil.AddLinePoints(pf, ptf)
	if err := pf.Save(6, 4, "prefilter.png"); err != nil {
		panic(err)
	}

	//Apply filter by removing high frequency components
	for i := nx / 4; i < nx/2; i++ {
		fftx[nx-i] = 0.0
		fftx[i] = 0.0
	}

	// Returns data, in place, to time domain
	backward.Execute()
	for i := range pt {
		pt[i].X = float64(i)
		pt[i].Y = cmplx.Abs(fftx[i])
		ptf[i].X = x[i]
		ptf[i].Y = real(f[i]) / float64(nx)
	}
	p, err = plot.New()
	if err != nil {
		panic(err)
	}
	pf, err = plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Power Spectrum of sign(x), After Low-Pass Filter"
	p.X.Label.Text = "n"
	p.Y.Label.Text = "Power"
	plotutil.AddLinePoints(p, pt)
	if err := p.Save(6, 4, "postfilterp.png"); err != nil {
		panic(err)
	}
	pf.Title.Text = "sign(x), After Low-pass filter"
	pf.X.Label.Text = "x"
	pf.Y.Label.Text = "sign(x)"
	plotutil.AddLinePoints(pf, ptf)

	if err := pf.Save(6, 4, "postfilter.png"); err != nil {
		panic(err)
	}
	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#13
0
func main() {
	F = math.Log        //Set f to desired function
	var a float64 = 0.5 //lower limit of range
	var b float64 = 1.5 //upper limit of range

	var N int //Number of interpolation points
	var dx float64
	var x []float64 //points on function to interpolate
	var f []float64 //function evaluated at xi's

	N = 10
	dx = 1.0 / float64(N+1)
	x = make([]float64, N+2)
	f = make([]float64, N+2)
	n := len(x)
	x[0] = a
	f[0] = F(a)
	x[n-1] = b
	f[n-1] = F(b)

	//Generate evenly spaced points from a to b
	//and evaluate log(x)
	for q := 1; q < n-1; q++ {
		x[q] = a + dx*float64(q)
		f[q] = F(x[q])
	}

	//-------------------
	//Plotting

	var pte, ptf, pti plotter.XYs

	pte = make(plotter.XYs, nPlot)
	ptf = make(plotter.XYs, nPlot)
	pti = make(plotter.XYs, nPlot)
	dx = 1.0 / float64(nPlot-1)
	var tf float64 = 0.0
	var tp float64 = 1.0
	var tx float64 = 0.0
	for i := range pte {
		tx = a + dx*float64(i)
		pte[i].X = tx
		ptf[i].X = tx
		pti[i].X = tx
		tf = 0.0
		for j := range f {
			tp = 1.0
			for k := range x {
				if j != k {
					tp *= (tx - x[k]) / (x[j] - x[k])
				}
			}
			tp *= f[j]
			tf += tp
		}
		ptf[i].Y = F(tx)
		pti[i].Y = tf
		pte[i].Y = math.Log(math.Abs(tf-F(tx))) / 10
		fmt.Println(tx, math.Log(math.Abs(tf-F(tx)))/10)
	}
	pte[0].Y = pte[1].Y
	pte[nPlot-1].Y = pte[nPlot-2].Y

	p_err, err := plot.New()
	if err != nil {
		panic(err)
	}

	p_err.Title.Text = fmt.Sprintf("Polynomial Interpolation Error, N=%d", N)
	p_err.X.Label.Text = "x"
	p_err.Y.Label.Text = "log(x)-p(x)"

	plotutil.AddLinePoints(p_err, pte)

	// Save the plot to a PNG file.
	if err := p_err.Save(6, 4, "errorplot.png"); err != nil {
		panic(err)
	}

	p_i, err := plot.New()
	if err != nil {
		panic(err)
	}

	p_i.Title.Text = fmt.Sprintf("Polynomial Interpolation, N=%d", N)
	p_i.X.Label.Text = "x"
	p_i.Y.Label.Text = "y"

	plotutil.AddLinePoints(p_i, "p(x)", pti, "log(x)", ptf)

	// Save the plot to a PNG file.
	if err := p_i.Save(6, 4, "interpolation.png"); err != nil {
		panic(err)
	}
}
示例#14
0
文件: port.go 项目: akonneker/scicomp
func main() {
	fmt.Println("Running...")
	start := time.Now()

	//F = math.Log
	//x0 = 3
	//fx = -1.0/(x0*x0)
	//
	//F = math.Tan
	//x0 = math.Asin(0.8)
	//fx = 2*math.Pow(math.Cos(x0),-2.0)*math.Tan(x0)
	F = f3
	fx = 2.0
	x0 = 0

	xv := make([]float64, nmax) // h * alpha^j
	xv[0] = h
	yv := make([]float64, nmax) // most recent richardson_extrapolants
	yv[0] = (F(x0+h) - 2*F(x0) + F(x0-h)) / (h * h)
	ev := make([][]float64, nmax)
	ev[0] = make([]float64, 1)
	err := math.Log10(math.Max(1.e-17, math.Abs(yv[0]-fx)))
	ev[0][0] = err

	for n := 1; n < nmax; n++ {
		ev[nc] = make([]float64, n+1)
		xv[nc] = xv[nc-1] * α
		δ := (F(x0+xv[nc]) - 2*F(x0) + F(x0-xv[nc])) / (xv[nc] * xv[nc])
		β := α2
		for i := 0; i < nc; i++ {
			δn := δ + (δ-yv[i])*β/(1-β)
			yv[i] = δ
			err = math.Log10(math.Max(1.e-17, math.Abs(yv[i]-fx)))
			ev[nc][i] = err
			δ = δn
			β *= α2
		}
		yv[nc] = δ
		err = math.Log10(math.Max(1.e-17, math.Abs(yv[nc]-fx)))
		ev[nc][nc] = err
		nc++
	}

	var pt plotter.XYs
	pt = make(plotter.XYs, nmax)
	for i := range pt {
		pt[i].X = -math.Log10(xv[i])
		pt[i].Y = ev[i][i]
	}

	p, errp := plot.New()
	if errp != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Error of Derivative Approximation, Sin")
	p.X.Label.Text = "-Log(h)"
	p.Y.Label.Text = "Log(|err|)"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "rich_error_sin.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#15
0
文件: hw3.go 项目: akonneker/scicomp
func main() {
	fmt.Println("Running...")
	start := time.Now()
	var a float64 = -1.0 //lower limit of range
	var b float64 = 1.0  //upper limit of range
	var err plotter.XYs
	err = make(plotter.XYs, nstop-nstart)
	var maxerr float64
	var interp [4]plotter.XYs

	for i := range interp {
		interp[i] = make(plotter.XYs, np+1)
	}
	var k int = 0
	for n := nstart; n < nstop; n++ {
		maxerr = 0
		err[n-nstart].X = float64(n)
		var x []float64 = make([]float64, n+1)
		var dx float64 = (b - a) / float64(n)
		for i := range x {
			x[i] = a + dx*float64(i)
		}
		dx = (b - a) / float64(np-1)
		var j int = 1
		for i := 0; i < np+1; i++ {
			tx := a + dx*float64(i)
			if tx > x[j] && j < n {
				j++
			}
			z := (tx - x[j-1]) / (x[j] - x[j-1])
			ty := F(x[j-1])*(1-z)*(1-2*z) + 4*F((x[j-1]+x[j])/2.0)*z*(1-z) + F(x[j])*z*(2*z-1)
			maxerr = math.Max(maxerr, math.Abs(ty-F(tx)))

			if k < 4 {
				if n == plotValues[k] {
					interp[k][i].X = tx
					interp[k][i].Y = ty
				}
			}
		}
		if k < 4 {
			if n == plotValues[k] {
				k++
			}
		}
		err[n-nstart].Y = math.Log(maxerr)
	}
	//Plotting
	pi, e0 := plot.New()
	if e0 != nil {
		panic(e0)
	}
	pi.Title.Text = "Maximum Error of Spline Interpolation of Runge's Function"
	pi.X.Label.Text = "N"
	pi.Y.Label.Text = "Log(Error)"

	// Make a line plotter and set its style.
	l := plotter.NewLine(err)
	l.LineStyle.Width = vg.Points(1)
	l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
	l.LineStyle.Color = color.RGBA{B: 255, A: 255}

	pi.Add(l)

	// Save the plot to a PNG file.
	if err := pi.Save(6, 4, "runge_error.png"); err != nil {
		panic(err)
	}

	pi, e0 = plot.New()
	if e0 != nil {
		panic(e0)
	}

	pi.Title.Text = "Spline Interpolation of Runge's Function"
	pi.X.Label.Text = "N"
	pi.Y.Label.Text = "Log(Error)"
	plotutil.AddLinePoints(pi, interp[2], interp[1], interp[0])
	//Set plot bounds
	pi.X.Min = 0
	pi.X.Max = 0.5

	// Save the plot to a PNG file.
	if err := pi.Save(6, 4, "runge_interp.png"); err != nil {
		panic(err)
	}
	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
示例#16
0
func main() {

	flag.Parse()

	if path == "" {
		fmt.Println("A path to a trace is needed.")
		os.Exit(1)
	}

	f, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	tl := &statedb.TimeLine{}
	dec := json.NewDecoder(f)

	if err := dec.Decode(tl); err != nil {
		panic(err)
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = path
	p.X.Label.Text = "timeline/ms"
	p.Y.Label.Text = "event duration/ms"

	// if !syncs && !commit && !price {
	// 	fmt.Println("No data flags were set!")
	// 	return
	// }

	// if price {
	for i, v := range tl.PriceChanges {
		tl.PriceChanges[i] = v * factor
	}

	pc, err := NewPriceTraces(tl.PriceTimes, tl.PriceChanges)
	if err != nil {
		panic(err)
	}
	// fmt.Println("Adding Price Traces...")
	// err = plotutil.AddLinePoints(p,
	// "price changes", pc)
	// if err != nil {
	// 	panic(err)
	// }
	// }

	// if syncs {
	fmt.Println("Adding Sync events")
	sp, err := NewXYer(tl.SyncStarts, tl.SyncDurations)
	if err != nil {
		panic(err)
	}
	// err = plotutil.AddLinePoints(p, "sync", sp)
	// if err != nil {
	// 	panic(err)
	// }
	// }

	// if commit {
	fmt.Println("Adding Commit events")

	cp, err := NewXYer(tl.CommitStarts, tl.CommitDurations)
	if err != nil {
		panic(err)
	}

	// err = plotutil.AddLinePoints(p, "commits", cp)
	// if err != nil {
	// 	panic(err)
	// }
	// }

	err = plotutil.AddLinePoints(p,
		"sync", sp,
		"commits", cp,
		"price changes", pc)
	if err != nil {
		panic(err)
	}

	path = strings.Replace(path, " ", "", -1)

	if err := p.Save(8, 4, path+".pdf"); err != nil {
		panic(err)
	}
	wd, err := os.Getwd()
	if err != nil {
		os.Exit(1)
	}
	file := filepath.Join(wd, path+".pdf")
	fmt.Println(file)
	err = exec.Command("/usr/bin/open", file).Run()
	if err != nil {
		fmt.Println(err.Error())
	}
}
示例#17
0
文件: hw1.go 项目: akonneker/scicomp
func main() {
	F = math.Log        //Set f to desired function
	var a float64 = 0.5 //lower limit of range
	var b float64 = 1.5 //upper limit of range

	var N, nx int //Number of interpolation points
	var dx float64
	var x []float64 //points on function to interpolate
	var f []float64 //function evaluated at xi's
	var γ []float64 //interpolation coefficients
	var ptm plotter.XYs
	ptm = make(plotter.XYs, NMax-NMin)

	for N = NMin; N < NMax; N++ {

		nx = 100 * (N + 1)
		dx = 1.0 / float64(N+1)
		x = make([]float64, N+2)
		f = make([]float64, N+2)
		γ = make([]float64, N+2)
		n := len(γ)
		x[0] = a
		f[0] = F(a)
		x[n-1] = b
		f[n-1] = F(b)

		//Generate evenly spaced points from a to b
		//and evaluate log(x)
		for q := 1; q < n-1; q++ {
			x[q] = a + dx*float64(q)
			f[q] = F(x[q])
		}
		copy(γ, f)

		//Use algorithm 10.2.1 to calculate the
		//interpolation polynomial coefficients
		for j := 1; j < n; j++ {
			for i := n - 1; i >= j; i-- {
				γ[i] = (γ[i] - γ[i-1]) / (x[i] - x[i-j])
			}
		}

		pt := 0.0
		i_err := 0.0

		//Use algorithm 10.2.2 to evaluate the interpolation function
		for k := 0; k <= nx; k++ {
			xk := a + float64(k)/float64(100*(N+1))
			pt = γ[n-1]
			for l := n - 2; l >= 0; l-- {
				pt = γ[l] + pt*(xk-x[l])
			}
			i_err = math.Max(math.Abs(F(xk)-pt), i_err)
		}
		ptm[N-NMin].X = float64(N)
		ptm[N-NMin].Y = math.Log10(i_err)
		//-------------------
		if N == NPlot {
			//Plotting

			var yp float64
			var pte, ptf, pti plotter.XYs

			//ye = make([]float64, nPlot)
			pte = make(plotter.XYs, nPlot)
			ptf = make(plotter.XYs, nPlot)
			pti = make(plotter.XYs, nPlot)

			//fmt.Println(γ)

			for k := range pte {
				xk := (b-a)/float64(nPlot-1)*float64(k) + a
				pte[k].X = xk
				ptf[k].X = xk
				pti[k].X = xk
				ptf[k].Y = F(xk)
				yp = γ[n-1]
				for l := n - 2; l >= 0; l-- {
					yp = γ[l] + yp*(xk-x[l])
				}
				pti[k].Y = yp
				pte[k].Y = math.Abs(F(xk) - yp)
			}

			p_err, err := plot.New()
			if err != nil {
				panic(err)
			}

			p_err.Title.Text = fmt.Sprintf("Polynomial Interpolation Error, N=%d", N)
			p_err.X.Label.Text = "x"
			p_err.Y.Label.Text = "log(x)-p(x)"

			plotutil.AddLinePoints(p_err, pte)

			// Save the plot to a PNG file.
			if err := p_err.Save(6, 4, "errorplot.png"); err != nil {
				panic(err)
			}

			p_i, err := plot.New()
			if err != nil {
				panic(err)
			}

			p_i.Title.Text = fmt.Sprintf("Polynomial Interpolation, N=%d", N)
			p_i.X.Label.Text = "x"
			p_i.Y.Label.Text = "y"

			plotutil.AddLinePoints(p_i, "p(x)", pti, "log(x)", ptf)

			// Save the plot to a PNG file.
			if err := p_i.Save(6, 4, "interpolation.png"); err != nil {
				panic(err)
			}
		}
	}
	p_m, err := plot.New()
	if err != nil {
		panic(err)
	}

	p_m.Title.Text = fmt.Sprintf("Maximum Interpolation Error, N=%d to N=%d", NMin, NMax)
	p_m.X.Label.Text = "N"
	p_m.Y.Label.Text = "Log10(Error)"

	plotutil.AddLinePoints(p_m, ptm)

	// Save the plot to a PNG file.
	if err := p_m.Save(6, 4, "maxerror.png"); err != nil {
		panic(err)
	}
}
示例#18
0
func PlotXvsYGroupedByXs(title, directory string, groupeddss map[string][]*rdb.Dataset, yValuesKey, yValuesLabel, xValuesLabel string) {
	var plottingPointArgs []interface{}
	var plottingErrorArgs []interface{}

	names := map[string]bool{}
	xKeys := []float64{}
	for xKey, dssGroup := range groupeddss {
		xKeys = append(xKeys, datautils.ParseFloatOrFail(xKey))
		for _, d := range dssGroup {
			names[d.GetName()] = true
		}
	}

	sort.Float64s(xKeys)
	xKeyStrings := []string{}
	for _, floatVal := range xKeys {
		xKeyStrings = append(xKeyStrings, strconv.FormatFloat(floatVal, 'f', 1, 64))
	}

	for name, _ := range names {

		datasetPointsAcrossGroups := []plotter.XYer{}

		for _, groupKey := range xKeyStrings {

			dssGroup := groupeddss[groupKey]

			var ds *rdb.Dataset

			for _, d := range dssGroup {
				if d.GetName() == name {
					ds = d
					break
				}
			}

			if ds == nil {
				str := fmt.Sprintf("Couldn't find ds (%s) in dss group (%s)", name, groupKey)
				panic(str)
			}

			values := ds.GetDatasetFloatValues(yValuesKey)
			if len(values) == 0 {
				continue
			}
			xys := make(plotter.XYs, len(values))
			datasetPointsAcrossGroups = append(datasetPointsAcrossGroups, xys)

			for i, val := range values {
				xys[i].X = datautils.ParseFloatOrFail(groupKey)
				xys[i].Y = val
			}
		}

		mean95, err := plotutil.NewErrorPoints(plotutil.MeanAndConf95, datasetPointsAcrossGroups...)

		if err != nil {
			fmt.Println(mean95)
			panic(err)
		}
		plottingPointArgs = append(plottingPointArgs, name, mean95)
		plottingErrorArgs = append(plottingErrorArgs, mean95)
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = title
	p.X.Label.Text = xValuesLabel
	p.Y.Label.Text = yValuesLabel

	plotutil.AddLinePoints(p, plottingPointArgs...)
	plotutil.AddErrorBars(p, plottingErrorArgs...)

	savePlot(title, directory, p)
}