-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem3.py
More file actions
145 lines (117 loc) · 4.34 KB
/
problem3.py
File metadata and controls
145 lines (117 loc) · 4.34 KB
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
from typing import *
import numpy.testing as npt
from constants import *
from dynamics import *
from plot import *
def test_part_1():
ts = np.arange(0, 1400 * dt, step=dt)
x_nom = np.array([nominal_trajectory.state_at(t) for t in ts])
y_nom = np.array(
[measurements(x, station_trajectories, t) for t, x in zip(ts, x_nom)]
)
x_hat = np.zeros_like(x_nom)
x_hat[0, :] = [0, 0.075, 0, -0.021] # initial perturbation
for i in range(x_hat.shape[0] - 1):
F, G = lt.F_G(ts[i], dt)
x_hat[i + 1, :] = F @ x_hat[i, :]
y_hat = np.zeros_like(y_nom)
for i in range(x_hat.shape[0]):
y_hat[i, :] = lt.H(ts[i]) @ x_hat[i, :]
x_pert_nonlinear = EllipticalTrajectory(x_nom[0, :] + x_hat[0, :]).propagate(ts)
y_pert_nonlinear = np.array(
[measurements(x, station_trajectories, t) for t, x in zip(ts, x_pert_nonlinear)]
)
x_pert_linear = x_nom + x_hat
y_pert_linear = y_nom + y_hat
nonlinear_control_points = {7250: [-3557, -6.481, 5651, -4.169]}
linear_pert_control_points = {7410: [-82.61, 0.1607, -72.49, -0.1582]}
linear_control_points = {9620: [1371, 7.685, -6462, 1.542]}
for control_points, to_check, atol in [
(nonlinear_control_points, x_pert_nonlinear, [1, 0.001, 1, 0.001]),
(linear_pert_control_points, x_hat, [0.01, 0.001, 0.01, 0.001]),
(linear_control_points, x_pert_linear, [1, 0.001, 1, 0.001]),
]:
for t, truth in control_points.items():
i = np.where(ts == t)[0][0]
for j in range(4):
npt.assert_allclose(to_check[i, j], truth[j], atol=atol[j], err_msg=j)
def part_1_ex_3():
ts = np.arange(0, 14000, step=dt)
x_nom = np.array([nominal_trajectory.state_at(t) for t in ts])
y_nom = np.array(
[measurements(x, station_trajectories, t) for t, x in zip(ts, x_nom)]
)
x_hat = np.zeros_like(x_nom)
x_hat[0, :] = [0, 0.075, 0, -0.021] # initial perturbation
for i in range(x_hat.shape[0] - 1):
F, G = lt.F_G(ts[i], dt)
x_hat[i + 1, :] = F @ x_hat[i, :]
y_hat = np.zeros_like(y_nom)
for i in range(x_hat.shape[0]):
y_hat[i, :] = lt.H(ts[i]) @ x_hat[i, :]
x_pert_nonlinear = EllipticalTrajectory(x_nom[0, :] + x_hat[0, :]).propagate(ts)
y_pert_nonlinear = np.array(
[measurements(x, station_trajectories, t) for t, x in zip(ts, x_pert_nonlinear)]
)
x_pert_linear = x_nom + x_hat
y_pert_linear = y_nom + y_hat
xs = [x_nom, x_pert_nonlinear, x_pert_linear]
xs_labels = ["Nominal", "Perturbed (Non-Lin)", "Perturbed (Lin)"]
ys_1 = [y_nom[:, :3], y_pert_nonlinear[:, :3], y_pert_linear[:, :3]]
# ax.legend(lines, xs_labels )
plot_states(
[np.array([st.state_at(t) for t in ts]) for st in station_trajectories]
+ [x_nom],
ts,
legend_labels=[f"gs{i}" for i in range(len(station_trajectories))] + ["Sat."],
)
plot_states(
[x_hat],
ts,
ylabels=[f"$x_{i}$" for i in range(1, 5)],
xlabel="Time [s]",
legend_labels=[r"$\bar{x}$"],
)
plot_states(
xs,
ts,
ylabels=[f"$x_{i}$" for i in range(1, 5)],
xlabel="Time [s]",
legend_labels=xs_labels,
kwargs=[{"linestyle": "-"}, {"linestyle": "--"}, {"linestyle": ":"}],
)
plot_states(
ys_1,
ts,
ylabels=[f"$y^{{s=1}}_{i}$" for i in range(1, 4)],
xlabel="Time [s]",
legend_labels=xs_labels,
kwargs=[{"linestyle": "-"}, {"linestyle": "--"}, {"linestyle": ":"}],
)
axs, _ = plot_states(
[(x_pert_nonlinear - x_pert_linear)],
ts,
ylabels=[f"$x_{i}$" for i in range(1, 5)],
xlabel="Time [s]",
)
axs[0].figure.suptitle(r"$(x_{nonlin} - x_{lin})$")
axs, _ = plot_states(
[(y_pert_nonlinear[:, :3] - y_pert_linear[:, :3])],
ts,
ylabels=[f"$y^{{s=1}}_{i}$" for i in range(1, 4)],
xlabel="Time [s]",
)
axs[0].figure.suptitle(r"$(x_{nonlin} - x_{lin})$")
masked_y_pert_nonlinear = mask_non_visible(
ts, x_pert_nonlinear, y_pert_nonlinear, station_trajectories
)
plot_measurements(
masked_y_pert_nonlinear,
ts,
ylabels=[f"$y^{{s=1}}_{i}$" for i in range(1, 4)],
xlabel="Time [s]",
)
plt.show()
if __name__ == "__main__":
# test_part_1()
part_1_ex_3()