summaryrefslogtreecommitdiff
path: root/books/workshops/2003/gamboa-cowles-van-baalen/support/kalman-demo.lisp
blob: 4c7b869e4ee4f54ee6fe3608d59966978ed03884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
; The ACL2 Matrix Algebra Book. Summary of definitions and algebra in matrix.lisp.
; Copyright (C) 2002 Ruben Gamboa and John R. Cowles, University of Wyoming

; This book is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.

; This book is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this book; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

; Written by:
; Ruben Gamboa and John Cowles
; Department of Computer Science
; University of Wyoming
; Laramie, WY 82071-3682 U.S.A.

; Summer and Fall 2002.
;  Last modified 1 November 2002.

#|
 To certify in ACL2 Version 2.6

  (ld ;; Newline to fool dependency scanner
    "defpkg.lsp")
  (certify-book "kalman-demo" 1)
|#

(in-package "KALMAN")

(include-book "kalman-proof")

(defun kalman-loop-body (xhatmin-prev pminus-prev k)
  (let* ((gain (m-* pminus-prev
		    (m-* (m-trans (h k))
			 (m-inv (m-+ (m-* (h k)
					  (m-* pminus-prev
					       (m-trans (h k))))
				     (r k))))))
	 (xhat (m-+ xhatmin-prev
		    (m-* gain
			 (m-- (z k)
			      (m-* (h k) xhatmin-prev)))))
	 (pplus (m-* (m-- (m-id (n))
			  (m-* gain (h k)))
		     pminus-prev))
	 (xhatmin (m-* (phi k) xhat))
	 (pminus (m-+ (m-* (phi k)
			   (m-* pplus
				(m-trans (phi k))))
		      (q k))))
    (mv gain xhat pplus xhatmin pminus)))

(encapsulate
 ()

 (local
  (defthm lemma-1
    (implies (and (integerp k)
		  (<= 0 k)
		  (equal xhatmin-prev (xhatmin k))
		  (equal pminus-prev (pminus k)))
	     (mv-let (gain xhat pplus xhatmin pminus)
		     (kalman-loop-body xhatmin-prev pminus-prev k)
		     (declare (ignore xhat pplus xhatmin pminus))
		     (equal gain (gain k))))
    :hints (("Goal"
	     :in-theory (disable acl2::*-+-right acl2::*-+-left
				 acl2::right-distributivity-of-m-*-over-m-+
				 acl2::left-distributivity-of-m-*-over-m-+)))))

 (local
  (defthm lemma-2
    (implies (and (integerp k)
		  (<= 0 k)
		  (equal xhatmin-prev (xhatmin k))
		  (equal pminus-prev (pminus k)))
	     (mv-let (gain xhat pplus xhatmin pminus)
		     (kalman-loop-body xhatmin-prev pminus-prev k)
		     (declare (ignore gain pplus xhatmin pminus))
		     (equal xhat (xhat k))))
    :hints (("Goal"
	     :in-theory (disable acl2::*-+-right acl2::*-+-left
				 acl2::right-distributivity-of-m-*-over-m-+
				 acl2::left-distributivity-of-m-*-over-m-+)))))

 (local
  (defthm lemma-3
    (implies (and (integerp k)
		  (<= 0 k)
		  (equal xhatmin-prev (xhatmin k))
		  (equal pminus-prev (pminus k)))
	     (mv-let (gain xhat pplus xhatmin pminus)
		     (kalman-loop-body xhatmin-prev pminus-prev k)
		     (declare (ignore gain xhat xhatmin pminus))
		     (equal pplus (pplus k))))
    :hints (("Goal"
	     :in-theory (disable pplus-as-mean
				 acl2::*-+-right acl2::*-+-left
				 acl2::right-distributivity-of-m-*-over-m-+
				 acl2::left-distributivity-of-m-*-over-m-+)))))

 (local
  (defthm lemma-4
    (implies (and (integerp k)
		  (<= 0 k)
		  (equal xhatmin-prev (xhatmin k))
		  (equal pminus-prev (pminus k)))
	     (mv-let (gain xhat pplus xhatmin pminus)
		     (kalman-loop-body xhatmin-prev pminus-prev k)
		     (declare (ignore gain xhat pplus pminus))
		     (equal xhatmin (xhatmin (1+ k)))))
    :hints (("Goal"
	     :in-theory (disable acl2::*-+-right acl2::*-+-left
				 acl2::right-distributivity-of-m-*-over-m-+
				 acl2::left-distributivity-of-m-*-over-m-+)))))

 (local
  (defthm lemma-5
    (implies (and (integerp k)
		  (<= 0 k)
		  (equal xhatmin-prev (xhatmin k))
		  (equal pminus-prev (pminus k)))
	     (mv-let (gain xhat pplus xhatmin pminus)
		     (kalman-loop-body xhatmin-prev pminus-prev k)
		     (declare (ignore gain xhat pplus xhatmin))
		     (equal pminus (pminus (1+ k)))))
    :hints (("Goal"
	     :in-theory (disable pplus-as-mean
				 pminus-as-mean
				 pminus-as-mean-almost
				 acl2::*-+-right acl2::*-+-left
				 acl2::right-distributivity-of-m-*-over-m-+
				 acl2::left-distributivity-of-m-*-over-m-+)))))

 (defthm kalman-loop-invariant
   (implies (and (integerp k)
		 (<= 0 k)
		 (equal xhatmin-prev (xhatmin k))
		 (equal pminus-prev (pminus k)))
	    (mv-let (gain xhat pplus xhatmin pminus)
		    (kalman-loop-body xhatmin-prev pminus-prev k)
		    (and (equal gain (gain k))
			 (equal xhat (xhat k))
			 (equal pplus (pplus k))
			 (equal xhatmin (xhatmin (1+ k)))
			 (equal pminus (pminus (1+ k))))))
   :hints (("Goal"
	    :use ((:instance lemma-1)
		  (:instance lemma-2)
		  (:instance lemma-3)
		  (:instance lemma-4)
		  (:instance lemma-5)))))
 )