下面是我的MATLAB代码,用于生成wavepeed(c)的虚部,当雷诺数为249时使用函数oscalcpcf,我想以2为步长运行0.1到2之间的波数(alpha)。
c1holder = [];
c2holder = [];
reyholder = [];
alphaholder = [];
wantedrey = [];
wantedalpha = [];
for Rey = 249
for alpha = 0.1:0.1:2
c1=oscalcpcf(Rey,alpha,100);
c2=oscalcpcf(Rey,alpha,200);
c1holder = [c1holder c1];
c2holder = [c2holder c2];
reyholder = [reyholder Rey];
alphaholder = [alphaholder alpha];
end
end
vectors = [c1holder' c2holder' reyholder' alphaholder'];
上面的代码在我看来并不困难,但我的笔记本电脑却因一些雷诺数和alpha而变得暴躁。只需命名其中一个,Reynolds number = 249
和alpha = 0.3
。
当我运行上述代码时,我得到c1 = 6.06002472332094E-08
和c2 = 0.0000010870344982811
。
现在问题就在于此。如果我使用2
步骤从0.1
到-0.1
,我会得到c1 = -0.337584041016646
和c2 = 0.0000364854401656638
。
如果我要使用oscalcpcf
手动核对,即oscalcpcf(249,0.3,100)
和oscalcpcf(249,0.3,200)
,我会c1 = -0.337583911335139
和c2 = -0.337577395716528
。
我真的不知道这里发生了什么,有人可以帮忙!
修改
alpha: 2.000000000000000000000000000000
alpha: 1.899999999999999900000000000000
alpha: 1.800000000000000000000000000000
alpha: 1.700000000000000000000000000000
alpha: 1.600000000000000100000000000000
alpha: 1.500000000000000000000000000000
alpha: 1.399999999999999900000000000000
alpha: 1.299999999999999800000000000000
alpha: 1.200000000000000000000000000000
alpha: 1.100000000000000100000000000000
alpha: 1.000000000000000000000000000000
alpha: 0.899999999999999910000000000000
alpha: 0.799999999999999820000000000000
alpha: 0.699999999999999960000000000000
alpha: 0.599999999999999870000000000000
alpha: 0.500000000000000000000000000000
alpha: 0.399999999999999910000000000000
alpha: 0.299999999999999820000000000000
alpha: 0.199999999999999960000000000000
alpha: 0.099999999999999867000000000000
以及0.1
至2
alpha: 0.100000000000000010000000000000
alpha: 0.200000000000000010000000000000
alpha: 0.300000000000000040000000000000
alpha: 0.400000000000000020000000000000
alpha: 0.500000000000000000000000000000
alpha: 0.599999999999999980000000000000
alpha: 0.700000000000000070000000000000
alpha: 0.800000000000000040000000000000
alpha: 0.900000000000000020000000000000
alpha: 1.000000000000000000000000000000
alpha: 1.100000000000000100000000000000
alpha: 1.200000000000000200000000000000
alpha: 1.300000000000000300000000000000
alpha: 1.400000000000000100000000000000
alpha: 1.500000000000000200000000000000
alpha: 1.600000000000000100000000000000
alpha: 1.700000000000000200000000000000
alpha: 1.800000000000000300000000000000
alpha: 1.900000000000000100000000000000
alpha: 2.000000000000000000000000000000
OMG,当我告诉它时,我的计算机为什么不给出0.1
的精确步骤。函数oscalcpcf
对alpha
中的小变化非常敏感,当我检查我的脚本使用的这些值时,如果我由oscalcpcf
手动执行,则匹配。您能为我的计算机建议一种方法来提供0.1
的精确步骤吗?谢谢。
答案 0 :(得分:2)
我相信你有一个浮点错误,因为你的冒号生成0.1:0.1:2
向量。
alpha
的值不准确,因为计算机无法准确表示固定存储大小(例如双精度)的所有数字。对colon operator尤其如此,这使得不准确性通过向量元素传播。
现在,我不确定这是否会改善您的结果,但基于how the COLON operator works,我建议您尝试按以下方式运行循环(类似于this answer of mine):< / p>
for alpha = ((1:20) / 10)
此外,如果oscalcpcf()
是一个允许被篡改的函数,我建议你研究它并提高其对输入中微小变化的鲁棒性/灵敏度。 10 -14 %不准确不应对您的结果产生重大影响。
答案 1 :(得分:1)
试试这个:
alpha = ((1:20) *1e-1). you should get :
0.100000000000000005551115123126
0.200000000000000011102230246252
0.300000000000000044408920985006
0.400000000000000022204460492503
0.500000000000000000000000000000
0.600000000000000088817841970013
0.700000000000000066613381477509
0.800000000000000044408920985006
0.900000000000000022204460492503
1.000000000000000000000000000000
1.100000000000000088817841970013
1.200000000000000177635683940025
1.300000000000000044408920985006
1.400000000000000133226762955019
1.500000000000000000000000000000
1.600000000000000088817841970013
1.700000000000000177635683940025
1.800000000000000044408920985006
1.900000000000000133226762955019
2.000000000000000000000000000000
结果将精确到双精度标准。
此外,matlab symbolic toolbox中的sym
和vpa
函数可能会有所帮助。