FLoating point arithmetic problem - how to calculate/plot properly?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have an ODE that is solved for the time with different static parameters. I have a default setting for that from where I start. From this default setting, I change one of this static parameters at a time with 5% abbrevation from this default setting; I start the iteration at -50%:5:+50% so I "walk" over the default parameter value during the iteration. I want to calculate
ef=mean(diff(odesolution) and for the percentage abbrevation, I calculate:
diffslope=(ef*100)/defaultslope. Then I get as a result something around 100. When I then do the calculation diffslope -100, I dont get exactly 0 for the iterationstep solution where ef= defaultslope but something around 1e-14. When I syms the diffslope before and then substract 100, i can't plot it properly. Idiffslope is here already rounded to the 10th decimal place.
deltaslope1=((diffslope*100)/defaultslope) ;
sensvec=[-50:5:50]; %The percentage change I already know
figure(4);
plot(sensvec,deltaslope1, '-o')
xlabel('Percentage change of parameter from default')
ylabel('Percentage change of C1 slope from default')
title('Sensitivity')
xline(0)
xlim([-50 50])
I would prefer a solution where I get a clear result for deltaslope 1.
댓글 수: 0
답변 (1개)
Jan
2021년 4월 27일
편집: Jan
2021년 4월 27일
This cannot work with floating point numbers. Remember that numbers stored in double format have a limited precision. Then claculations must include a rounding. You can only expect exact values, if the inputs and all intermediate values can be represented as binary values exactly. But this is a rare exception.
댓글 수: 2
Jan
2021년 4월 27일
Rounding is an option, but it does not fit all needs, e.g. if the value is greater than 1e17, rounding to decimal places is meansingless. Do not compare floating point values with 0, but apply a limit, e.g.: (x - y) < 10 * eps(max(abs(x, y)))
Yes, all values are concerned by the limited precision. Two common examples:
1e17 + 1 - 1e17 % == 0
1e17 - 1e17 + 1 % == 1
any(0:0.1:1) == 0.3 % false
any((0:0.1:1) - 0.3 < eps(0.3)) % true
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!