Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math"
- )
- func main(){
- var (
- xVals = []float64{0.5, 0.6, 0.7, 0.8}
- yVals = []float64{-1, 0, 1, 5}
- )
- fmt.Printf("%-10s | %-10s | %-10s | %-10s\n", "xi", "Ln(xi)", "Rn(xi)", "dRn(xi)")
- for index, xVal := range xVals {
- fmt.Printf("%10.1f | %10.3f | %10e | %10e\n", xVal, ln(xVal, xVals, yVals), rn(x, xVals), deltaRn(x, xVals, yVals))
- }
- func lagrange(n int, x float64, values []float64) float64 {
- var product = 1.0
- for index, val := range values {
- if index != n {
- product *= (x - val) / (values[n] - val)
- }
- }
- return product
- }
- func ln(x float64, xVals []float64, yVals []float64) float64 {
- var sum = 0.0
- for i, yi := range yVals {
- sum += yi * lagrange(i, x, xVals)
- }
- return sum
- }
- func w(x float64, values []float64) float64 {
- var product = 1.0
- for _, val := range values {
- product *= x - val
- }
- return product
- }
- func rn(x float64, values []float64) float64 {
- return math.Abs(w(x, values)) / 24
- }
- func deltaRn(x float64, xVals []float64, yVals []float64) float64 {
- return math.Abs(rn(x, xVals)) / math.Abs(ln(x, xVals, yVals))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement