Subtracting two values that are very small
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm subtracting two variables (R1 and R2) that are around a magnitude of e-3. I work out a difference between them (Diff = R2 - R1) but my Diff variable outputs as 0 for the whole matrix. I am trying to plot Diff on a plot. How can I make Diff not output as all 0's? Thank you.
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391]
R2 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391]
Diff = R2 - R1
댓글 수: 4
William Rose
2022년 2월 11일
@Muhammad Choudhury, if they were different then the vecotr
R2mR1=R2-R1;
wil have the correct difference.
I would not use
diff=R2-R1;
because there is a built-in function called diff() that computes the successive differences of a vector or array.
채택된 답변
Voss
2022년 2월 11일
R1 and R2 are equal, so their difference is in fact 0 everywhere.
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
R2 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
Diff = R2 - R1
isequal(R2,R1)
댓글 수: 3
Voss
2022년 2월 11일
If they were different then the output from diff() would be non-zero somewhere. You can plot the difference the same way regardless:
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
% increased R2(1) by 1e-17:
R2 = [0.00212278260869566 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
Diff = R2 - R1
plot(Diff);
Image Analyst
2022년 2월 12일
@Muhammad Choudhury I presume you had two vectors that were definitely different and you plotted Diff and got all zeros, or think you did. It should look something like Benjamin's plot above. Did it not?
If not then you either had all zeros, and a very long vector, and the only-non-zero Diff element was the first one or last one so it essentially overlapped with the y axis so you didn't notice it.
OR you just looked at the command window and saw only zeros and though they were all zeros because the command window by default only prints the first 4 decimal places.
OR they actually were all zeros despite what you thought.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Bartlett에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!