0

I have these random Gaussian variables A and B to represent voltages. I also have resistances in series with the voltages, RA and RB, respectively. I want to take voltage measurements in between RA and RB, and I want to measure the current from A to B. I have implemented,

I = (A-B)/(RA+RB);
U = I*RA-A;

Let's say RA has a high value, RH, but I want to use process of elimination to find out. I have implemented,

A_calc = U+I*RL; %guess the low value first

Now I want to check if the calculation matches.

if A_calc==A
    disp('RA=RL');
else
    disp('RA=RH');
end

Now the issue is, A_calc is never equal to A. There are always deviations, no matter what I set the resistance value to.

What could be the error? Is it the resolution lost? I thought MATLAB was 16 bits of resolution?

  • 1
    No, MATLAB doesn’t use 16 bits, it uses 64-but floating point values. Read here to learn more: https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab – Cris Luengo Dec 14 '19 at 14:21

1 Answers1

2

I'd need the entire code to figure out where your problem is - but in general, it is very common to find numerical inaccuracies in such computations. You almost always define some kind of difference threshold, under it the variables are considered equal. Please use:

function eq = isalmostequal(a,b,tol)
if ~exist('tol','var'); 
   tol = 1e-9;
end

eq = (abs(a - b) < tol)
end

call it:

if isalmostequal(A,A_calc)
   disp('RA=RL');
else
   disp('RA=RH');
end
max
  • 3,915
  • 2
  • 9
  • 25
Mano
  • 797
  • 3
  • 17
  • you should use `nargin` to check if an input exists – max Dec 14 '19 at 13:32
  • @max - I think that is a matter of opinion. The way above allows you to edit the function, adding more inputs, without being bound to the number of arguments. However, it is slightly less efficient. – Mano Dec 14 '19 at 13:35