Same calcuation, but hand-result is different from code result
1 次查看(过去 30 天)
显示 更早的评论
I have this code:
aoamid = (2*0.6*sqrt(1-((0)/4)^2)/(pi^2)+0.6/(8*pi))
CLnew = (aoamid+0.01)/(2*sqrt(1-((0)/4)^2)/(pi^2)+1/(8*pi))
m = (CLnew-0.6)/(0.01)
aoafinal = aoamid - 0.6/m
However, the result from hand-calculation is different from the answer generated by the code: (left is hand-calculated result)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1302670/image.png)
0 个评论
回答(2 个)
Star Strider
2023-2-22
This simply appears to be a precision issue. Using round on the intermediate results provides the same result as the calculator —
format longG
aoamid = (2*0.6*sqrt(1-((0)/4)^2)/(pi^2)+0.6/(8*pi))
CLnew = (aoamid+0.01)/(2*sqrt(1-((0)/4)^2)/(pi^2)+1/(8*pi))
m = (CLnew-0.6)/(0.01)
aoafinal = aoamid - 0.6/m
Check = round(aoamid,4) - 0.6/round(m,4)
.
0 个评论
Image Analyst
2023-2-22
If you use more digits than 4 (like the script uses) you'll see that the values are very close -- both essentially zero being around 1e-16. When you're dealing with numbers in the 0.1 to 100 range or whatever, anything that's a difference that's less than about 1e-8 or so is essentially zero and due to truncation error.
format long g
aoamid = (2*0.6*sqrt(1-((0)/4)^2)/(pi^2)+0.6/(8*pi))
CLnew = (aoamid+0.01)/(2*sqrt(1-((0)/4)^2)/(pi^2)+1/(8*pi))
m = (CLnew-0.6)/(0.01)
aoafinal = aoamid - 0.6/m
aoafinal_using4digits = 0.1455 - 0.6 / m
% now do with a lot more decimal places:
aoafinal_fullprecision = 0.14545866183459 - 0.6 / 4.1248832653383
See, the difference is around 1e-16 when using script variables or full precision of about 14 decimal places, but when you use only 4 decimal places, the difference (error) is more, like 1e-5.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!