Advertisement
Guest User

Lab 3

a guest
Mar 13th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.14 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6.     )
  7.  
  8. func main(){
  9.     var (
  10.         xVals = []float64{0.5, 0.6, 0.7, 0.8}
  11.         yVals = []float64{-1, 0, 1, 5}
  12.     )
  13.    
  14.     fmt.Printf("%-10s | %-10s | %-10s | %-10s\n", "xi", "Ln(xi)", "Rn(xi)", "dRn(xi)")
  15.    
  16.     for index, xVal := range xVals {
  17.         fmt.Printf("%10.1f | %10.3f | %10e | %10e\n", xVal, ln(xVal, xVals, yVals), rn(x, xVals), deltaRn(x, xVals, yVals))
  18. }
  19.  
  20. func lagrange(n int, x float64, values []float64) float64 {
  21.     var product = 1.0
  22.    
  23.     for index, val := range values {
  24.         if index != n {
  25.             product *= (x - val) / (values[n] - val)
  26.         }
  27.     }
  28.    
  29.     return product
  30. }
  31.  
  32. func ln(x float64, xVals []float64, yVals []float64) float64 {
  33.     var sum = 0.0
  34.    
  35.     for i, yi := range yVals {
  36.         sum += yi * lagrange(i, x, xVals)
  37.     }
  38.    
  39.     return sum
  40. }
  41.  
  42. func w(x float64, values []float64) float64 {
  43.     var product = 1.0
  44.    
  45.     for _, val := range values {
  46.         product *= x - val
  47.     }
  48.    
  49.     return product
  50. }
  51.  
  52. func rn(x float64, values []float64) float64 {
  53.     return math.Abs(w(x, values)) / 24
  54. }
  55.  
  56. func deltaRn(x float64, xVals []float64, yVals []float64) float64 {
  57.     return math.Abs(rn(x, xVals)) / math.Abs(ln(x, xVals, yVals))
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement