Subtracting two values that are very small

조회 수: 1 (최근 30일)
Muhammad Choudhury
Muhammad Choudhury 2022년 2월 11일
댓글: Image Analyst 2022년 2월 12일
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]
R1 = 1×21
0.0021 0.0021 0.0021 0.0021 0.0021 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0023
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]
R2 = 1×21
0.0021 0.0021 0.0021 0.0021 0.0021 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0023
Diff = R2 - R1
Diff = 1×21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  댓글 수: 4
Muhammad Choudhury
Muhammad Choudhury 2022년 2월 11일
편집: Muhammad Choudhury 2022년 2월 11일
that is very true i didn't even realise, sorry about that, if my question still held, how would go about working it out?
William Rose
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
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
Diff = 1×21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
isequal(R2,R1)
ans = logical
1
  댓글 수: 3
Voss
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
Diff = 1×21
1.0e+-17 * 0.9975 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
plot(Diff);
Image Analyst
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 CenterFile Exchange에서 Bartlett에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by