7 people like it.

Lagrange formula

Lagrange's interpolation formula

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
let lagrange (x:float list) (y:float list) (v:float) =
    [for i in 0..x.Length - 1 ->
         [for j in [0..i-1]@[i+1..x.Length - 1] ->
            (v - x.[j]) / (x.[i] - x.[j])]
         |> List.fold (*) y.[i]]
    |> List.sum

//Usage

//x values
let x = [111.0;112.0;113.0;114.0;115.0]
//y values
let y = [13.0;24.0;35.0;46.0;57.0]
//interpolating value
let v = 117.0

lagrange x y v
val lagrange : x:float list -> y:float list -> v:float -> float

Full name: Script.lagrange
val x : float list
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val y : float list
val v : float
val i : int
property List.Length: int
val j : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sum
val x : float list

Full name: Script.x
val y : float list

Full name: Script.y
val v : float

Full name: Script.v
Raw view Test code New version

More information

Link:http://fssnip.net/7RV
Posted:7 years ago
Author:Ivan Padron
Tags: math , mathematic