CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun May 11 13:21:39 2014
4
5
@author: rlabbe
6
"""
7
8
from __future__ import print_function, division
9
10
from numpy.random import randn
11
import math
12
13
class DogSimulation(object):
14
15
def __init__(self, x0=0, velocity=1,
16
measurement_variance=0.0, process_variance=0.0):
17
""" x0 - initial position
18
velocity - (+=right, -=left)
19
measurement_variance - variance in measurement m^2
20
process_variance - variance in process (m/s)^2
21
"""
22
self.x = x0
23
self.velocity = velocity
24
self.measurement_noise = math.sqrt(measurement_variance)
25
self.process_noise = math.sqrt(process_variance)
26
27
28
def move(self, dt=1.0):
29
'''Compute new position of the dog assuming `dt` seconds have
30
passed since the last update.'''
31
# compute new position based on velocity. Add in some
32
# process noise
33
velocity = self.velocity + randn() * self.process_noise
34
self.x += velocity * dt
35
36
def sense_position(self):
37
# simulate measuring the position with noise
38
measurement = self.x + randn() * self.measurement_noise
39
return measurement
40
41
def move_and_sense(self, dt=1.0):
42
self.move(dt)
43
44
return self.sense_position()
45
46