Skip to content

Instantly share code, notes, and snippets.

@ziriax
Last active February 27, 2016 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziriax/5c5a506697d3c1c85b21 to your computer and use it in GitHub Desktop.
Save ziriax/5c5a506697d3c1c85b21 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
const double dt = 0.01; // seconds
const double k = 1;
const double m = 1;
const double x0 = 100;
var timer = Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(dt));
// Hooke's law:
// f = -k * x
// a = f/m
// v = integral(a)
// x = x0 + integral(v)
IObservable<double> sigX = null;
sigX = Observable.Defer(
() =>
{
var sigF = sigX.Select(x => -x * k);
var sigA = sigF.Select(f => f / m);
var sigV = sigA.Scan(0.0, (v, a) => v + a * dt);
return sigV.Scan(x0, (x, v) => x + v * dt);
})
.Publish(x0)
.RefCount()
.Sample(timer);
sigX.Subscribe(x => Console.Write("\r{0,10:F2}",x));
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment