From: Johnny on
Hi,

Context: I'm trying to find the resting membrane potential (voltage) of a cell where:
gNa*(V-VNa) == - gK*(V-VK); is true, gNa, VNa, gK, and VK are parameters.

I simplified the problem to:
% gM, gL, VM, VL are defined.
V = linspace(-100,-30,7000); % Voltage
for search = 1:length(V)
Value1 = gM*(V(search)-VM);
Value2 = -gL*(V(search)-VL;
if Value1==Value2 %%%%%%%%%%%%% PROBLEM HERE!
Vposition = search;
end
Vrest = V(Vposition) % Returns the value of the resting potential voltage
end

The problem is... Value1 is never exactly equal to Value2 and the if statement is never satisfied. Is there something I can do to say: When Value1 is ABOUT EQUAL to Value2 in the if statement?

Thanks,

JOH
From: Cygnine on
"Johnny " <omega_harpoon(a)yahoo.com> wrote in message
> The problem is... Value1 is never exactly equal to Value2 and the if statement is never satisfied. Is there something I can do to say: When Value1 is ABOUT EQUAL to Value2 in the if statement?

If your real intention is "about equal", you'll have to define what "about" means:

absolute_tolerance = 1e-6;

if abs(Value1-Value2)<absolute_tolerance;
% do stuff
end

However, are you sure you want this in your code? Sometimes it's fine and sometimes it leads to outcomes you didn't intend.
From: Walter Roberson on
Johnny wrote:

> The problem is... Value1 is never exactly equal to Value2 and the if
> statement is never satisfied. Is there something I can do to say: When
> Value1 is ABOUT EQUAL to Value2 in the if statement?

abs(Value1 - Value2) < YourTolerance

YourTolerance can be an absolute value, or it can be expressed
proportional to the values involved, e.g.

abs(Value1 - Value2) < max(abs([Value1, Value2])) * ToleranceFraction

You may wish to frame your ToleranceFraction in terms of the floating
point uncertainty constant eps. The appropriate multiplier for eps is
going to depend upon the amount and variety of processing you went
through to get the numbers: e.g., if your initial input X had been
X * (1 + eps) instead, then how much of a change would that have made to
your output?