Last active
February 27, 2016 20:52
-
-
Save ziriax/5c5a506697d3c1c85b21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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