从 MATLAB 到 C++ 的多维数组
Multidimensional arrays from MATLAB to C++
所以我正在尝试做我在 C++ MATLAB 中做的一个项目,但一路上我遇到了困难。
这是我想转换为C++的 MATLAB 中的部分代码。它确实适用于 MATLAB,但不适用于C++
RelRough = [0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001];
ReT = [4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000];
for i = 1:length(ReT)
for j = 1:length(RelRough)
FCT_guess = 1;
tolerance = 1;
while tolerance > 1e-14
FCT_cal = 1/(-2*log10((RelRough(j)/3.7) + (2.51/(ReT(i)*sqrt(FCT_guess)))))^2;
tolerance = abs(FCT_cal-FCT_guess);
FCT_guess = FCT_cal;
FCT(i,j) = FCT_cal*1000;
end
end
end
这是我的C++版本,对于变量 g ,我不断收到诸如"表达式必须具有整数或无作用域枚举类型"之类的错误
double RelRough[] = { 0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001 };
const int lengthRelRough = sizeof(RelRough) / sizeof(RelRough[0]);
double ReT[] = { 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000 };
const int lengthReT = sizeof(ReT) / sizeof(ReT[0]);
double fct[lengthReT][lengthRelRough] = { 0 };
double fct_guess = 1;
double tolerance = 1;
double fct_cal = 0;
for (int ii = 0; ii < lengthReT; ++ii) {
for (int jj = 0; jj < lengthRelRough; ++jj) {
while (tolerance > 1e-14) {
double h = (RelRough[jj] / 3.7), w = (2.51 / (ReT[ii] * sqrt(fct_guess)));
double g = (-2*log10(h+w))^2;
fct_cal = 1/g;
tolerance = abs(fct_cal - fct_guess);
fct_guess = fct_cal;
fct[ii][jj] = fct_cal;
std::cout << fct[ii][jj] << "t";
}
}
}
return 0;
}
有没有人帮助看看出了什么问题。提前感谢!
更改以下内容:
double g = (-2*log10(h+w))^2;
到:
double g = pow(-2*log10(h+w),2.0);
正如@Eljay在他的评论中指出的,运算符^
执行C++
的XOR
,而不是幂。欲了解更多信息:
- 幂(pow 函数(
- 布尔运算(包括异或(
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '