MATLAB: How to force a while loop to run. this code doe not like to run the last loop when x1 is equal to zero on the plot

while loop

% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long ;
%format compact;
fontSize = 20;
apsize = 0.182000;
Xspacing =.001000;
zero = 0;
x1 = 0;
z1 = 0;
k = -0.871; % conic constant
r = 0.066; % radius of curvature
a2= 0; a4= 120; a6=0 ; % coeff of higher order terms
c= 1/r;
fid = fopen('sphere.NC','w');
hold off
fprintf(fid, 'G01\r\nG71\r\nG90\r\nG94\r\nG14\r\n' ); % g code header







fprintf(fid, 'T0101\r\n' ); % g code header
fprintf(fid, 'M4S4000\r\n' ); % g code header
fprintf(fid, 'M26\r\n' ); % g code header
%index = 1;
while apsize >= zero
x1 = apsize ;
z1 = -1*(((c*(x1.^2))./(1+sqrt(1-((1+k)*c*c*x1.^2))))+(a2*x1.^2)+(a4*x1.^4)+(a6*x1.^6));
x2 = apsize-Xspacing;
z2 = -1*(((c*(x2.^2))./(1+sqrt(1-((1+k)*c*c*x2.^2))))+(a2*x2.^2)+(a4*x2.^4)+(a6*x2.^6));
if apsize <= 0
m = 0;
else
m = (z1-z2)/(x1-x2);
end
p = atand(m);
fprintf(fid, 'X%0.8f Z%0.8f D%0.8f\r\n', x1 , z1, p);
apsize = apsize-Xspacing;
zero = 0 ;
%index = index + 1;
plot(x1,-z1,'r.-','LineWidth', 2, 'MarkerSize', 20);
hold on
grid on;
title('z vs. x', 'fontSize', fontSize);
xlabel('x', 'fontSize', fontSize);
ylabel('z', 'fontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo ', 'NumberTitle', 'Off');
end
fprintf(fid, 'M29\r\n' ); % g code header
fprintf(fid, 'M5\r\n' ); % g code header
fprintf(fid, 'M30\r\n' ); % g code header
fprintf(fid, ' \r\n' ); % g code header
fclose(fid);
msgbox('Done with program');

Best Answer

Welcome to the world of floating point numbers where
0.1 + 0.1 + 0.1 == 0.3
is false (try it!). That is because binary floating point cannot store 0.1 exactly. The nearest number to 0.1 that can be stored is 0.1 + about 1e-17. Unfortunately as you sum or subtract these numbers the error accumulate, and by the time you've added three 0.1, it's no longer equal to 0.3 but the next number up (0.3 + about 5e-17).
This is the same problem you have. You'll see that the last x1 that is plotted is not exactly .001 but .001 - about 1.6e-16. Subtract .001 from that and you do not get 0 but -1.6e-16 which is clearly smaller than 0, hence your loop ends. When your loop finishes see:
>>x1
x1 =
0.001
>>x1 - 0.001
ans =
-1.5959e-16
The solution is to round apsize when you do the comparison
while round(apsize, 3) >= 0 %round to nearest 0.001