% 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 offfprintf(fid, 'G01\r\nG71\r\nG90\r\nG94\r\nG14\r\n' ); % g code header
fprintf(fid, 'T0101\r\n' ); % g code headerfprintf(fid, 'M4S4000\r\n' ); % g code headerfprintf(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 ongrid 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');endfprintf(fid, 'M29\r\n' ); % g code headerfprintf(fid, 'M5\r\n' ); % g code headerfprintf(fid, 'M30\r\n' ); % g code headerfprintf(fid, ' \r\n' ); % g code headerfclose(fid);msgbox('Done with program');
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
Best Answer