2012-09-01 45 views
2

下面是我的MATLAB代碼來產生波形速度的虛部(c),使用函數oscalcpcf當雷諾數是249,我想運行波數(alpha)在0.1到2之間2.真的很簡單的MATLAB代碼與不合邏輯的結果

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']; 

上面的代碼在我看來並不困難,但我的筆記本電腦對於雷諾數和阿爾法的一些配對變得怪異。只要命名其中的一個,Reynolds number = 249alpha = 0.3

當我運行上面的代碼,我得到c1 = 6.06002472332094E-08c2 = 0.0000010870344982811

現在是這個問題。如果我從步驟-0.120.1,我得到c1 = -0.337584041016646c2 = 0.0000364854401656638

如果我是檢查使用oscalcpcf,即oscalcpcf(249,0.3,100)oscalcpcf(249,0.3,200)手動,我得到c1 = -0.337583911335139c2 = -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.12

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步驟時,我告訴它這樣做。功能oscalcpcfalpha中的小改動非常敏感,當我檢查我的腳本使用的這些值時,如果我通過oscalcpcf手動執行該操作,它將會匹配。你能否建議我的電腦給出精確的步驟0.1?謝謝。

+1

不知道'oscalcpcf'在做什麼,我們只能猜測。根據函數對輸入的敏感程度,你的問題可能是精確的。嘗試添加'disp(['alpha:'sprintf('%。30f',alpha)]);'到你的內部for循環來查看'alpha'的實際值是什麼。 – Deve

+0

實際上,一旦您瞭解計算機如何進行算術運算,完全符合邏輯的結果。 – 2012-09-01 13:55:15

回答

2

我相信你有一個浮點錯誤,因爲你的冒號生成的0.1:0.1:2載體。

對於alpha,您會得到不準確的值,因爲計算機無法準確表示給定固定存儲大小(如雙精度)的所有數字。對於colon operator尤其如此,這使得不準確性通過向量元素傳播。現在

,我不知道這是否提高你的結果還是不行,但基於how the COLON operator works,我建議你試試如下(類似於this answer of mine)來運行你的循環:

for alpha = ((1:20)/10) 

而且,如果oscalcpcf()是一個被允許被篡改的函數,我建議你仔細研究它,並改善它對輸入的小變化的魯棒性/敏感性。 A 10 -14%錯誤不應該對您的結果產生重大影響。

+0

嘿感謝您的建議,但不準確仍然存在......我想知道是否有任何方法可以創建步長爲0.1的精確向量...... – matlablearner

+0

不幸的是不是。我建議你在'oscalcpcf'上工作時不那麼敏感。 –

+3

NO。閱讀回覆。您不能在浮點算術中表示數字0.1。 – 2012-09-01 10:56:16

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 

結果是精確到雙精度標準。 另外,symvpa功能matlab symbolic toolbox可能會有所幫助,如果你有它。