DiffSharp


Kinematics

Let us use the DiffSharp library for describing the simple kinematics of a point particle moving in one dimension.

Take the function \(x(t) = t^3 - 6 t^2 + 10t\), giving the position \(x\) of a particle at time \(t\).

1: 
2: 
// Position x(t)
let x t = t * t * t - 6. * t * t + 10. * t

Plot \(x(t)\) between \(t=0\) and \(t=4\).

1: 
2: 
3: 
4: 
open FSharp.Charting

// Plot x(t) between t = 0 and t = 4
Chart.Line([for t in 0.0..0.01..4.0 -> (t, x t)]).WithXAxis(Title="t").WithYAxis(Title="x")
Chart

We can calculate the position \(x(t)\), the velocity \(v(t)=\frac{\partial x(t)}{\partial t}\), and the acceleration \(a(t)=\frac{\partial ^ 2 x(t)}{\partial t ^ 2}\) of the particle at the same time, using the diff2'' operation that returns the original value, the first derivative, and the second derivative of a given function.

1: 
2: 
3: 
4: 
open DiffSharp.AD.Float64

// diff2'' returns the tuple (original value, first derivative, second derivative)
let xva = D >> diff2'' (fun t -> t * t * t - 6 * t * t + 10 * t)

The following gives us a combined plot of \(x(t)\), \(v(t)\), and \(a(t)\).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
// Functions for extracting the position, velocity, acceleration values from a 3-tuple
let pos (x, _, _) = x |> float
let vel (_, v, _) = v |> float
let acc (_, _, a) = a |> float

// Draw x(t), v(t), and a(t) between t = 0 and t = 4
Chart.Combine([Chart.Line([for t in 0.0..0.01..4.0 -> (t, pos (xva t))], Name="x(t)")
               Chart.Line([for t in 0.0..0.01..4.0 -> (t, vel (xva t))], Name="v(t)")
               Chart.Line([for t in 0.0..0.01..4.0 -> (t, acc (xva t))], Name="a(t)")])
               .WithLegend().WithXAxis(Title = "t")
Chart
val x : t:float -> float

Full name: Examples-kinematics.x
val t : float
namespace FSharp
namespace FSharp.Charting
type Chart =
  static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member BoxPlotFromData : data:seq<#key * #seq<'a2>> * ?Name:string * ?Title:string * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart (requires 'a2 :> value)
  static member BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart
  static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Bubble : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  ...

Full name: FSharp.Charting.Chart
static member Chart.Line : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
static member Chart.Line : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:System.Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
namespace DiffSharp
namespace DiffSharp.AD
module Float64

from DiffSharp.AD
val xva : (float -> D * D * D)

Full name: Examples-kinematics.xva
union case D.D: float -> D
val diff2'' : f:(D -> 'a) -> x:D -> 'a * 'a * 'a (requires member get_T and member get_P)

Full name: DiffSharp.AD.Float64.DiffOps.diff2''
val t : D
val pos : x:D * 'a * 'b -> float

Full name: Examples-kinematics.pos
val x : D
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<_>
val vel : 'a * v:D * 'b -> float

Full name: Examples-kinematics.vel
val v : D
val acc : 'a * 'b * a:D -> float

Full name: Examples-kinematics.acc
val a : D
static member Chart.Combine : charts:seq<ChartTypes.GenericChart> -> ChartTypes.GenericChart