MATLAB与C++的结果/输出不正确/输出使用特征矩阵
我将我的MATLAB代码与C ++进行比较,并获得不正确的输出,以简单简单。
我的MATLAB代码:
Ny = 10;
ygl = -cos(pi*(0:Ny)/Ny)'; %Gauss-Lobatto chebyshev points
%Chebyshev matrix:
VGL = cos(acos(ygl(:))*(0:Ny));
dVGL = diag(1./sqrt(1-ygl.^2))*sin(acos(ygl)*(0:Ny))*diag(0:Ny);
C ++代码:
static const int ny = 10;
std::vector<double> ygl(ny+1);
for (int i = 0; i< ny+1; i++){
ygl[i] = -1. * cos(((i) * EIGEN_PI )/ny); //This is correct
}
Eigen::Matrix< double, ny+1, 1> v ;
v.setZero();
Eigen::Matrix< double, ny+1, 1> v1 ;
v1.setZero();
Eigen::Matrix< double, ny+1, ny+1> m;
m.setZero();
Eigen::Matrix< double, ny+1, ny+1> m1;
m1.setZero();
Eigen::Matrix< double, ny+1, ny+1> dv;
dv.setZero();
for (int j = 0; j < ny+1; j++){
v[j] = 1./(sqrt(1-pow(ygl[j],2)));
v1[j] = 1. * (j);
}
m = v.array().sqrt().matrix().asDiagonal(); //this is correct
m1 = v1.array().matrix().asDiagonal(); //this is correct
for (int i = 0; i < ny+1; i++){
for (int j = 0; j < ny+1; j++){
dv(j + ny*i) = m(j) * sin(acos(ygl[j]) * (i)) * m1(j); //this is incorrect
cout << dv(j + ny*i) << "\n";
}
}
您可以看到,发现两个列向量V和V1的对角线矩阵是正确的,但是当将额外的术语乘以时,请完全放弃结果。我不确定它是否像此处的操作顺序一样简单。
为了为这个特定示例提供更多上下文,正确的输出应该看起来像:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
0 1.0000 -3.8042 7.8541 -12.3107 16.1803 -18.4661 18.3262 -15.2169 9.0000 0.0000
0 1.0000 -3.2361 4.8541 -4.0000 -0.0000 6.0000 -11.3262 12.9443 -9.0000 -0.0000
0 1.0000 -2.3511 1.1459 2.9062 -6.1803 4.3593 2.6738 -9.4046 9.0000 0.0000
0 1.0000 -1.2361 -1.8541 4.0000 0.0000 -6.0000 4.3262 4.9443 -9.0000 -0.0000
0 1.0000 -0.0000 -3.0000 0.0000 5.0000 -0.0000 -7.0000 0.0000 9.0000 -0.0000
0 1.0000 1.2361 -1.8541 -4.0000 -0.0000 6.0000 4.3262 -4.9443 -9.0000 -0.0000
0 1.0000 2.3511 1.1459 -2.9062 -6.1803 -4.3593 2.6738 9.4046 9.0000 -0.0000
0 1.0000 3.2361 4.8541 4.0000 -0.0000 -6.0000 -11.3262 -12.9443 -9.0000 0.0000
0 1.0000 3.8042 7.8541 12.3107 16.1803 18.4661 18.3262 15.2169 9.0000 -0.0000
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
%%%%%%%%%%%%%%; 在从评论中提出@cris Luengo的建议之后。我有以下内容,
Eigen::Matrix< double, 1, ny+1> v1;//rewrote v1[j] = 1. * (j); from for loop
v1.setZero();
std::iota(v1.begin(), v1.end(), 0);
for (int j = 0; j < ny+1; j++){
v[j] = 1./(sqrt(1-pow(ygl[j],2)));
//v1[j] = 1. * (j);
}
m = v.array().matrix().asDiagonal();
m1 = v1.array().matrix().asDiagonal();
for (int i = 0; i < ny+1; i++){
for (int j = 0; j < ny+1; j++){
ygl[j] = -1. * cos(((j) * EIGEN_PI )/ny);//this is correct
dv(j + ny*i) = sin(acos(ygl[j]) * (i));//this is correct
}
}
dv1 = (m) * (dv) * (m1);
std::cout << dv1 << "\n"; //this is incorrect
I am comparing my MATLAB code to c++ and getting incorrect output for something fairly simple.
My MATLAB code:
Ny = 10;
ygl = -cos(pi*(0:Ny)/Ny)'; %Gauss-Lobatto chebyshev points
%Chebyshev matrix:
VGL = cos(acos(ygl(:))*(0:Ny));
dVGL = diag(1./sqrt(1-ygl.^2))*sin(acos(ygl)*(0:Ny))*diag(0:Ny);
C++ code:
static const int ny = 10;
std::vector<double> ygl(ny+1);
for (int i = 0; i< ny+1; i++){
ygl[i] = -1. * cos(((i) * EIGEN_PI )/ny); //This is correct
}
Eigen::Matrix< double, ny+1, 1> v ;
v.setZero();
Eigen::Matrix< double, ny+1, 1> v1 ;
v1.setZero();
Eigen::Matrix< double, ny+1, ny+1> m;
m.setZero();
Eigen::Matrix< double, ny+1, ny+1> m1;
m1.setZero();
Eigen::Matrix< double, ny+1, ny+1> dv;
dv.setZero();
for (int j = 0; j < ny+1; j++){
v[j] = 1./(sqrt(1-pow(ygl[j],2)));
v1[j] = 1. * (j);
}
m = v.array().sqrt().matrix().asDiagonal(); //this is correct
m1 = v1.array().matrix().asDiagonal(); //this is correct
for (int i = 0; i < ny+1; i++){
for (int j = 0; j < ny+1; j++){
dv(j + ny*i) = m(j) * sin(acos(ygl[j]) * (i)) * m1(j); //this is incorrect
cout << dv(j + ny*i) << "\n";
}
}
As you can see finding the diagonal matrix of the two column vectors v and v1 are correct but then when multiplied with an additional term give completely off results. I am not sure if it's as simple as order of operation here or something else.
To give more context for this specific example, the correct output should look like:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
0 1.0000 -3.8042 7.8541 -12.3107 16.1803 -18.4661 18.3262 -15.2169 9.0000 0.0000
0 1.0000 -3.2361 4.8541 -4.0000 -0.0000 6.0000 -11.3262 12.9443 -9.0000 -0.0000
0 1.0000 -2.3511 1.1459 2.9062 -6.1803 4.3593 2.6738 -9.4046 9.0000 0.0000
0 1.0000 -1.2361 -1.8541 4.0000 0.0000 -6.0000 4.3262 4.9443 -9.0000 -0.0000
0 1.0000 -0.0000 -3.0000 0.0000 5.0000 -0.0000 -7.0000 0.0000 9.0000 -0.0000
0 1.0000 1.2361 -1.8541 -4.0000 -0.0000 6.0000 4.3262 -4.9443 -9.0000 -0.0000
0 1.0000 2.3511 1.1459 -2.9062 -6.1803 -4.3593 2.6738 9.4046 9.0000 -0.0000
0 1.0000 3.2361 4.8541 4.0000 -0.0000 -6.0000 -11.3262 -12.9443 -9.0000 0.0000
0 1.0000 3.8042 7.8541 12.3107 16.1803 18.4661 18.3262 15.2169 9.0000 -0.0000
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
%%%%%%%%%%%%
After taking the suggestion of @Cris Luengo from the comments. I have the following,
Eigen::Matrix< double, 1, ny+1> v1;//rewrote v1[j] = 1. * (j); from for loop
v1.setZero();
std::iota(v1.begin(), v1.end(), 0);
for (int j = 0; j < ny+1; j++){
v[j] = 1./(sqrt(1-pow(ygl[j],2)));
//v1[j] = 1. * (j);
}
m = v.array().matrix().asDiagonal();
m1 = v1.array().matrix().asDiagonal();
for (int i = 0; i < ny+1; i++){
for (int j = 0; j < ny+1; j++){
ygl[j] = -1. * cos(((j) * EIGEN_PI )/ny);//this is correct
dv(j + ny*i) = sin(acos(ygl[j]) * (i));//this is correct
}
}
dv1 = (m) * (dv) * (m1);
std::cout << dv1 << "\n"; //this is incorrect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论