% example of while usage.  Search/iterate for value of x where y = 5. 
%
% y = x^2 + 0.974382
%

clear;          %always clear workspace/memory before (re)running program.

x = 0;          %initialize x (first guess)
ysoln = 5.0;    % desired value of y. 
epsilon = 20;   % epsilon is error, initially make big pos number.
eps = 20; 
dx = 1.0;       % increment to change x at each iteration.

while eps > 0.0000001
    y = x^2 + 0.974382;
    eps_prev = epsilon; %save previous value of epsilon
    epsilon = 5-y;
    eps = abs(epsilon); 
    if sign(epsilon) ~= sign(eps_prev)
        dx = dx/(-10);
    end
    x = x + dx;
end
x
y
