質問編集履歴

3

tuiki

2019/01/07 12:38

投稿

chiku_soh
chiku_soh

スコア12

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,117 @@
153
153
  です.
154
154
 
155
155
  よろしくお願いいたします.
156
+
157
+
158
+
159
+ 追記!
160
+
161
+ ```c++
162
+
163
+ #include "Eigen/Dense"
164
+
165
+ #include "Eigen/Core"
166
+
167
+ #include <iostream>
168
+
169
+ #include <fstream>
170
+
171
+ #include <cmath>
172
+
173
+ using namespace Eigen;
174
+
175
+ using namespace std;
176
+
177
+
178
+
179
+
180
+
181
+ void RungeKutta(MatrixXd &X, double tt, double dt, MatrixXd A, MatrixXd B, MatrixXd U) {
182
+
183
+ MatrixXd k1 = A*X*dt + -6.25*sin(2.0*tt*M_PI)*B*U*dt;
184
+
185
+ MatrixXd k2 = A*(X + 0.5*k1)*dt + -6.25*sin(2.0*tt*M_PI)*B*U*dt;
186
+
187
+ MatrixXd k3 = A*(X + 0.5*k2)*dt + -6.25*sin(2.0*tt*M_PI)*B*U*dt;
188
+
189
+ MatrixXd k4 = A*(X + k3)*dt + -6.25*sin(2.0*tt*M_PI)*B*U*dt;
190
+
191
+ MatrixXd k = (k1 + 2.0*k2 + 2.0*k3 + k4)*dt / 6.0;
192
+
193
+ X = X + k;
194
+
195
+ }
196
+
197
+
198
+
199
+ int main() {
200
+
201
+ double m = 1.0;
202
+
203
+
204
+
205
+ MatrixXd A(2, 2);
206
+
207
+ A(0, 0) = 0;
208
+
209
+ A(0, 1) = 1;
210
+
211
+ A(1, 0) = 0;
212
+
213
+ A(1, 1) = 0;
214
+
215
+ MatrixXd B(2, 2);
216
+
217
+ B(0, 0) = 0;
218
+
219
+ B(0, 1) = 0;
220
+
221
+ B(1, 0) = 0;
222
+
223
+ B(1, 1) = 1 / m;
224
+
225
+
226
+
227
+ double dt = 0.001;
228
+
229
+ double tt = 0.0;
230
+
231
+ MatrixXd X(1, 2);
232
+
233
+ X(0, 0) = 1;
234
+
235
+ X(0, 1) = 0;
236
+
237
+ MatrixXd U(1, 2);
238
+
239
+ U(0, 0) = 0;
240
+
241
+ U(0, 1) = 1;
242
+
243
+
244
+
245
+ double freq = 1.0;
246
+
247
+ ofstream ofs("outdata.csv");
248
+
249
+ ofs << "time," << "x," << "v" << endl;
250
+
251
+
252
+
253
+ for (int i = 0; i <= 1000; i++) {
254
+
255
+ RungeKutta(X, tt, dt, A, B, U);
256
+
257
+ ofs << tt << "," << X(0, 0) << "," << X(0, 1) << endl;
258
+
259
+ tt += dt;
260
+
261
+ }
262
+
263
+
264
+
265
+ return 0;
266
+
267
+ }
268
+
269
+ ```

2

エラーの追記

2019/01/07 12:38

投稿

chiku_soh
chiku_soh

スコア12

test CHANGED
File without changes
test CHANGED
@@ -146,4 +146,10 @@
146
146
 
147
147
 
148
148
 
149
+ エラー内容は,
150
+
151
+ Assertion failed: row >= 0 && row < rows() && col >= 0 && col < cols(), file Eigen/src/Core/DenseCoeffsBase.h, line 365
152
+
153
+ です.
154
+
149
155
  よろしくお願いいたします.

1

タイトルと,少しのソースコードの修正

2019/01/07 10:03

投稿

chiku_soh
chiku_soh

スコア12

test CHANGED
@@ -1 +1 @@
1
- c++で状態空間方程式を解きたのでが。
1
+ コンパイルは成功しますが,実行するとAssertion failedとわれま
test CHANGED
@@ -49,8 +49,6 @@
49
49
  using namespace Eigen;
50
50
 
51
51
  using namespace std;
52
-
53
-
54
52
 
55
53
 
56
54
 
@@ -110,15 +108,11 @@
110
108
 
111
109
  X(1, 0) = 0;
112
110
 
113
- MatrixXd Y(1, 1);
111
+ MatrixXd U(2, 1);
114
-
115
- Y(0, 0) = 0;
116
-
117
- MatrixXd U(1, 2);
118
112
 
119
113
  U(0, 0) = 0;
120
114
 
121
- U(0, 1) = -6.25*sin(2.0*tt*M_PI);
115
+ U(1, 0) = -6.25*sin(2.0*tt*M_PI);
122
116
 
123
117
 
124
118
 
@@ -134,9 +128,7 @@
134
128
 
135
129
  RungeKutta(X, tt, dt, A, B, U);
136
130
 
137
- Y = X*U;
138
-
139
- ofs << tt << "," << Y(0, 0) << endl;
131
+ ofs << tt << "," << X(0, 0) << endl;
140
132
 
141
133
  tt += dt;
142
134