MATLAB: Hi While running the code it seem the while statement does not run while equal to zero if i look at the output file sphere.nc i want to drive the x and z values to zero but it stops at x.001not x0

while

% 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 g;
format compact;
fontSize = 20;
apsize= 0.182;
%apsize = apsize;+0.00000000001;
Xspacing =.001;
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 >= 0
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 <= Xspacing
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;
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 by ImageAnalyst', 'NumberTitle', 'Off');
x1 = x1 -Xspacing;
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

If you want to get x00, change the while condition to
while apsize >= -Xspacing