Subtracting two values that are very small
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Muhammad Choudhury
el 11 de Feb. de 2022
Comentada: Image Analyst
el 12 de Feb. de 2022
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 comentarios
William Rose
el 11 de Feb. de 2022
@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.
Respuesta aceptada
Voss
el 11 de Feb. de 2022
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 comentarios
Voss
el 11 de Feb. de 2022
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
el 12 de Feb. de 2022
@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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Bartlett en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!