I have a big string of code. There is a variable at the top that affects many different calculations, which I want to increment in a 1 in a loop for a desired value.
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
This string of code is very long, but I have broken it into 2 sections. The top section shows the variable (dda), which is the first component in a chain of many calculations, and which eventually affects the value for (nba) way down at the bottom. With the current value of dda (28), nba=0.848. I want the whole string of code to run in a loop with dda increasing incrementally by +0.01 until nba equals 0.810. I thought a simple while loop would work to say, while nba > 0.810, dda=dda+1, or something like that, but it doesn't. Wouldn't think this would be that hard! Hopefully it isn't. Thank you
%% Top of code
leng_side_bot=cosd(lower_side_ang)*radius;             %13.6680 inches
x_bot=upper_top_radius-(leng_side_bot-leng_side_up);   %11.4297 inches  
%%%%%%%%
side_circumf=2*pi*radius;         %97.3894  side circuference
dda=28;
ddb=28;
%%%%%%%%
high_side_ang_P=upper_side_ang-((40)/side_circumf)*360;    %32.872  ang
high_side_ang=upper_side_ang-((40+dda)/side_circumf)*360;   %28.797  ang
low_side_ang=lower_side_ang-((10+28)/side_circumf)*360;    %22.608  ang
low_side_ang_P=lower_side_ang-((10)/side_circumf)*360;     %26.683  ang
comb_side_ang_segs=high_side_ang+low_side_ang;             %51.406  ang total
%%%%%%%%%%%continued code..........
%%%%%%%%%%%%
%%%%%%%%%%%%.........
%% Bottom of code
pu1=[top_circ_lo2_seg_x    top_circ_lo2_seg_y    -Z_low_P2]; 
pu39=[top_circ_lo2_seg_x   -top_circ_lo2_seg_y    -Z_low_P2];
%%%%%%%%%%%%%%%%%%%%%
nu=norm(p51-p40);
nv=norm(p51-p539);
nva=nu/nv;
na=norm(p40-p31);
nb=norm(p339-p31);
nba=na/nb;
0 commentaires
Réponse acceptée
  Voss
      
      
 le 4 Fév 2025
        
      Modifié(e) : Voss
      
      
 le 4 Fév 2025
  
      Your code could have some structure like this:
% presuming these don't depend on the value of dda, they can be 
% calculated once, before the loop:
high_side_ang_P = upper_side_ang-((40)/side_circumf)*360;    %32.872  ang
low_side_ang = lower_side_ang-((10+28)/side_circumf)*360;    %22.608  ang
low_side_ang_P = lower_side_ang-((10)/side_circumf)*360;     %26.683  ang
% dda increment:
dda_incr = 0.01;
% initial dda value:
dda = 28-dda_incr;
% initial nba value (use something > 0.81 so the loop is entered):
nba = Inf;
while nba > 0.81
    % increment dda:
    dda = dda+dda_incr;
    % these depend on dda, so they need to be re-calculated inside the loop:
    high_side_ang = upper_side_ang-((40+dda)/side_circumf)*360;   %28.797  ang
    comb_side_ang_segs = high_side_ang+low_side_ang;             %51.406  ang total
    % p40, p31, etc., calculations ...
    na = norm(p40-p31);
    nb = norm(p339-p31);
    % nba value for current dda value:
    nba = na/nb;
end
2 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

