annotate libs/commons-math-2.1/docs/userguide/TrajectoryDeterminationProblem.java @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1 /*
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
2 * Licensed to the Apache Software Foundation (ASF) under one or more
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
3 * contributor license agreements. See the NOTICE file distributed with
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
4 * this work for additional information regarding copyright ownership.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
5 * The ASF licenses this file to You under the Apache License, Version 2.0
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
6 * (the "License"); you may not use this file except in compliance with
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
7 * the License. You may obtain a copy of the License at
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
8 *
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
9 * http://www.apache.org/licenses/LICENSE-2.0
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
10 *
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
11 * Unless required by applicable law or agreed to in writing, software
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
12 * distributed under the License is distributed on an "AS IS" BASIS,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
14 * See the License for the specific language governing permissions and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
15 * limitations under the License.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
16 */
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
17
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
18 import org.apache.commons.math.optimization.general.EstimationException;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
19 import org.apache.commons.math.optimization.general.EstimatedParameter;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
20 import org.apache.commons.math.optimization.general.EstimationProblem;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
21 import org.apache.commons.math.optimization.general.LevenbergMarquardtEstimator;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
22 import org.apache.commons.math.optimization.general.SimpleEstimationProblem;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
23 import org.apache.commons.math.optimization.general.WeightedMeasurement;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
24
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
25 public class TrajectoryDeterminationProblem extends SimpleEstimationProblem {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
26
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
27 public static void main(String[] args) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
28 try {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
29 TrajectoryDeterminationProblem problem =
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
30 new TrajectoryDeterminationProblem(0.0, 100.0, 800.0, 1.0, 0.0);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
31
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
32 double[][] distances = {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
33 { 0.0, 806.5849 }, { 20.0, 796.8148 }, { 40.0, 791.0833 }, { 60.0, 789.6712 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
34 { 80.0, 793.1334 }, { 100.0, 797.7248 }, { 120.0, 803.2785 }, { 140.0, 813.4939 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
35 { 160.0, 826.9295 }, { 180.0, 844.0640 }, { 200.0, 863.3829 }, { 220.0, 883.3143 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
36 { 240.0, 908.6867 }, { 260.0, 934.8561 }, { 280.0, 964.0730 }, { 300.0, 992.1033 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
37 { 320.0, 1023.998 }, { 340.0, 1057.439 }, { 360.0, 1091.912 }, { 380.0, 1125.968 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
38 { 400.0, 1162.789 }, { 420.0, 1201.517 }, { 440.0, 1239.176 }, { 460.0, 1279.347 } };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
39 for (int i = 0; i < distances.length; ++i) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
40 problem.addDistanceMeasurement(1.0, distances[i][0], distances[i][1]);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
41 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
42
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
43 double[][] angles = {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
44 { 10.0, 1.415423 }, { 30.0, 1.352643 }, { 50.0, 1.289290 }, { 70.0, 1.225249 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
45 { 90.0, 1.161203 }, {110.0, 1.098538 }, {130.0, 1.036263 }, {150.0, 0.976052 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
46 {170.0, 0.917921 }, {190.0, 0.861830 }, {210.0, 0.808237 }, {230.0, 0.757043 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
47 {250.0, 0.708650 }, {270.0, 0.662949 }, {290.0, 0.619903 }, {310.0, 0.579160 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
48 {330.0, 0.541033 }, {350.0, 0.505590 }, {370.0, 0.471746 }, {390.0, 0.440155 },
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
49 {410.0, 0.410522 }, {430.0, 0.382701 }, {450.0, 0.356957 }, {470.0, 0.332400 } };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
50 for (int i = 0; i < angles.length; ++i) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
51 problem.addAngularMeasurement(3.0e7, angles[i][0], angles[i][1]);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
52 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
53
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
54 LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
55 estimator.estimate(problem);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
56 System.out.println("initial position: " + problem.getX0() + " " + problem.getY0());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
57 System.out.println("velocity: " + problem.getVx0() + " " + problem.getVy0());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
58
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
59 } catch (EstimationException ee) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
60 System.err.println(ee.getMessage());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
61 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
62 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
63
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
64 public TrajectoryDeterminationProblem(double t0,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
65 double x0Guess, double y0Guess,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
66 double vx0Guess, double vy0Guess) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
67 this.t0 = t0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
68 x0 = new EstimatedParameter( "x0", x0Guess);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
69 y0 = new EstimatedParameter( "y0", y0Guess);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
70 vx0 = new EstimatedParameter("vx0", vx0Guess);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
71 vy0 = new EstimatedParameter("vy0", vy0Guess);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
72
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
73 // inform the base class about the parameters
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
74 addParameter(x0);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
75 addParameter(y0);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
76 addParameter(vx0);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
77 addParameter(vy0);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
78
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
79 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
80
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
81 public double getX0() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
82 return x0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
83 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
84
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
85 public double getY0() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
86 return y0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
87 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
88
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
89 public double getVx0() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
90 return vx0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
91 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
92
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
93 public double getVy0() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
94 return vy0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
95 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
96
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
97 public void addAngularMeasurement(double wi, double ti, double ai) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
98 // let the base class handle the measurement
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
99 addMeasurement(new AngularMeasurement(wi, ti, ai));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
100 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
101
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
102 public void addDistanceMeasurement(double wi, double ti, double di) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
103 // let the base class handle the measurement
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
104 addMeasurement(new DistanceMeasurement(wi, ti, di));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
105 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
106
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
107 public double x(double t) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
108 return x0.getEstimate() + (t - t0) * vx0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
109 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
110
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
111 public double y(double t) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
112 return y0.getEstimate() + (t - t0) * vy0.getEstimate();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
113 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
114
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
115 private class AngularMeasurement extends WeightedMeasurement {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
116
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
117 public AngularMeasurement(double weight, double t, double angle) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
118 super(weight, angle);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
119 this.t = t;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
120 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
121
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
122 public double getTheoreticalValue() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
123 return Math.atan2(y(t), x(t));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
124 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
125
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
126 public double getPartial(EstimatedParameter parameter) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
127 double xt = x(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
128 double yt = y(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
129 double r = Math.sqrt(xt * xt + yt * yt);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
130 double u = yt / (r + xt);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
131 double c = 2 * u / (1 + u * u);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
132 if (parameter == x0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
133 return -c;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
134 } else if (parameter == vx0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
135 return -c * t;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
136 } else if (parameter == y0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
137 return c * xt / yt;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
138 } else {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
139 return c * t * xt / yt;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
140 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
141 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
142
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
143 private final double t;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
144 private static final long serialVersionUID = -5990040582592763282L;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
145
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
146 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
147
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
148 private class DistanceMeasurement extends WeightedMeasurement {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
149
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
150 public DistanceMeasurement(double weight, double t, double angle) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
151 super(weight, angle);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
152 this.t = t;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
153 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
154
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
155 public double getTheoreticalValue() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
156 double xt = x(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
157 double yt = y(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
158 return Math.sqrt(xt * xt + yt * yt);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
159 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
160
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
161 public double getPartial(EstimatedParameter parameter) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
162 double xt = x(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
163 double yt = y(t);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
164 double r = Math.sqrt(xt * xt + yt * yt);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
165 if (parameter == x0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
166 return xt / r;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
167 } else if (parameter == vx0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
168 return xt * t / r;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
169 } else if (parameter == y0) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
170 return yt / r;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
171 } else {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
172 return yt * t / r;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
173 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
174 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
175
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
176 private final double t;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
177 private static final long serialVersionUID = 3257286197740459503L;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
178
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
179 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
180
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
181 private double t0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
182 private EstimatedParameter x0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
183 private EstimatedParameter y0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
184 private EstimatedParameter vx0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
185 private EstimatedParameter vy0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
186
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
187 }