Same calcuation, but hand-result is different from code result
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
0 Kommentare
Antworten (2)
Star Strider
am 22 Feb. 2023
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 Kommentare
Image Analyst
am 22 Feb. 2023
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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!