incompatible result from multi if elseif statements inside while-loop,

1 visualización (últimos 30 días)
M Adli Hawariyan
M Adli Hawariyan el 29 de Jun. de 2022
Editada: Johan el 29 de Jun. de 2022
from first state of structure we obtain first value of tol_1 and tol_2. After that we do looping until tol_1 and tol_2 reach >=0.9 && <1 for both of them. the calculation is giving the impact of each other, so if calc from tol_1 is given, the result value will impact tol_2 and so on. But the result of it never reach >=0.9 && <1 , my result is tol_1 = -0.0261; tol_2 = 0.6830
full program is shown at attachment including matlab file, and excel data input
the part of program shown in the website is part of the program to give general idea of the problem:
1st part is Initial forces
2nd part is first and second loop system including:
- force matrix for increasing
- force matrix for decreasing
- calculating process to generate first state (only part of program shown in forum, full at the attachment)
3rd part is the looping part with optimal result we called tolerance (tol_1 and tol_2)
%% Initial Forces
matxf = zeros(max(big_dof),1); %empty matrices for force
force = rmmissing(data(:,14));
tdof_f = length(force) %total force dof
for j=1:tdof_f;
fdof = data(j,13); %fdof= force dofs being read
f = data(j,14); %f= forces
matxf(fdof,1) = matxf(fdof,1) + f;
end
act_f = matxf(a_dof,1); %defined force at active dofs
%% First Force Loop System
% Loop Increase Forces Placement
matxfl_inc1 = zeros(max(big_dof),1);%empty matx for added looping force
td_f1inc = length(rmmissing((data2(:,2))))
for u=1:td_f1inc
fdofl_1inc = data2(u,2); %fdof_l= force loop dofs being read
floop_1inc = data2(u,3); %force_loop= added forces for loop
matxfl_inc1(fdofl_1inc,1) = matxfl_inc1(fdofl_1inc,1) + floop_1inc;
end
afl_inc1=matxfl_inc1(a_dof,1);%defined added loop force ...
%at active dofs
% Loop Decrease Force Placements
matxfl_dec1 = zeros(max(big_dof),1);%empty matx for added looping force
td_f1dec = length(rmmissing((data2(:,4))))
for u=1:td_f1dec
fdofl_1dec = data2(u,4); %fdof_l= force loop dofs being read
floop_1dec = data2(u,5); %force_loop= added forces for loop
matxfl_dec1(fdofl_1dec,1) = matxfl_dec1(fdofl_1dec,1) + floop_1dec;
end
afl_dec1=matxfl_dec1(a_dof,1);%defined added loop force...
%at active dofs
%% Second Force Loop System
% Loop Increase Forces Placement
matxfl_inc2 = zeros(max(big_dof),1);%empty matx for added looping force
td_f2inc = length(rmmissing((data2(:,12))))
for u=1:td_f2inc
fdofl_2inc = data2(u,12); %fdof_l= force loop dofs being read
floop_2inc = data2(u,13); %force_loop= added forces for loop
matxfl_inc2(fdofl_2inc,1) = matxfl_inc2(fdofl_2inc,1) + floop_2inc;
end
afl_inc2=matxfl_inc2(a_dof,1);%defined added loop force ...
%at active dofs
% Loop Decrease Force Placements
matxfl_dec2 = zeros(max(big_dof),1);%empty matx for added looping force
td_f2dec = length(rmmissing((data2(:,14))))
for u=1:td_f2dec
fdofl_2dec = data2(u,14); %fdof_l= force loop dofs being read
floop_2dec = data2(u,15); %force_loop= added forces for loop
matxfl_dec2(fdofl_2dec,1) = matxfl_dec2(fdofl_2dec,1) + floop_2dec;
end
afl_dec2=matxfl_dec2(a_dof,1);%defined added loop force...
%at active dofs
%% First State Deflection
%Initial Performance
kgf = k(a_dof,a_dof);
kkk = kgf^-1;
defl_ans = kkk*act_f; %deflection toward acting dofs
%%% Mapping generated Deflection
% #1 Deflection & Acting Force data each dof
def_int = zeros(big_dof,1); %empty matrix for after added force loop
for_int = zeros(big_dof,1);
for mm=1:toa_dof;
dof_act=ta_dof(mm,1);
def_val=defl_ans(mm,1);
for_val=act_f(mm,1);
def_int(dof_act,1)=def_int(dof_act,1)+def_val;
for_int(dof_act,1)=for_int(dof_act,1)+for_val;
end
% #2 Deflection for each axis
defl = def_int(:,1);
defl_x = defl(1:3:big_dof);
defl_y = defl(2:3:big_dof);
defl_t = defl(3:3:big_dof);
% #3 Post-Deflect position
newp_x = pos_x+defl_x;
newp_y = pos_y+defl_y;
newp_t = (pos_t + defl_t); %1 RAD = 57.296 DEG
%deflection position
a3defl_i= zeros(big_nod,3);
a3defl_i(:,1) = defl_x;
a3defl_i(:,2) = defl_y;
a3defl_i(:,3) = defl_t
calc = 1; %index for looping process
loop(calc) = 0; % count for pre-looping process
dpint_1 = a3pos_i((ceil(data2(:,11)/3)),data2(:,10));
deint_1 = a3defl_i((ceil(data2(:,7)/3)),data2(:,6));
dpint_2 = a3pos_i((ceil(data2(:,21)/3)),data2(:,20));
deint_2 = a3defl_i((ceil(data2(:,17)/3)),data2(:,16));
idp_1 = data2(:,9);
idp_2 = data2(:,19);
tol_1(calc)=abs((deint_1)/(idp_1 - dpint_1))
tol_2(calc)=abs((deint_2)/(idp_2 - dpint_2))
%% PART 4: Looping part for generating force with tolerance >= 90%
% Addiing looping force
while tol_1(calc) >= 0 & tol_2(calc) >= 0
calc = calc + 1
loop(calc)=loop(calc-1)+1
if tol_1 < 0.90
act_f = act_f + afl_inc1
elseif tol_1 >= 0.9 & tol_1 < 1
act_f = act_f
else tol_1 > 1
act_f = act_f + afl_dec1
end
if tol_2 < 0.90
act_f = act_f + afl_inc2
elseif tol_2 >= 0.9 & tol_2 < 1
act_f = act_f
else tol_2 > 1
act_f = act_f + afl_dec2
end
if tol_1 >= 0.9 & tol_1 < 1 & tol_2 >= 0.9 & tol_2 < 1
act_f = act_f
break
end
defl_loop_ans = kkk*act_f; %deflection toward acting dofs
%%% Mapping generated Deflection
% #1 Deflection & Acting Force data each dof
defl_loop = zeros(big_dof,1); %empty matrix for after added force loop
for_loop = zeros(big_dof,1);
for mm=1:toa_dof;
dof_act=ta_dof(mm,1);
def_val=defl_loop_ans(mm,1);
for_val=act_f(mm,1);
defl_loop(dof_act,1)=defl_loop(dof_act,1)+def_val;
for_loop(dof_act,1)=for_loop(dof_act,1)+for_val;
end
% #2 Deflection for each axis
defl = defl_loop(:,1);
defl_x = defl(1:3:big_dof);
defl_y = defl(2:3:big_dof);
defl_t = defl(3:3:big_dof);
% #3 Post-Deflect position
newp_x = pos_x+defl_x;
newp_y = pos_y+defl_y;
newp_t = (pos_t + defl_t); %1 RAD = 57.296 DEG
%deflection position
adefl_lo= zeros(big_nod,3);
adefl_lo(:,1) = defl_x;
adefl_lo(:,2) = defl_y;
adefl_lo(:,3) = defl_t
delo_1 = adefl_lo((ceil(data2(:,7)/3)),data2(:,6));
delo_2 = adefl_lo((ceil(data2(:,17)/3)),data2(:,16));
tol_1(calc)=((delo_1)/(idp_1 - dpint_1))
tol_2(calc)=((delo_2)/(idp_2 - dpint_2))
last_tol1 = tol_1(calc)
last_tol2 = tol_2(calc)
if last_tol1 >= 0.9 && last_tol2 >= 0.9 && last_tol1 < 1 && last_tol2 < 1
disp('force calc is done!!')
continue
else
end
end
  1 comentario
Johan
Johan el 29 de Jun. de 2022
Editada: Johan el 29 de Jun. de 2022
Few notes on your script:
  • Assigning variable in a for loop as you do results in the last value of your array in your variable:
data = rand(10,1)
data = 10×1
0.9973 0.8726 0.9880 0.7613 0.8624 0.6040 0.1296 0.5866 0.7629 0.5752
ndata = size(data,1);
for j=1:ndata
%here your are not indexing B to the loop so only the last value is
%kept, all the previous value are overwritten.
B = data(j,1);
end
B
B = 0.5752
If you want to assign an array or part of an array to a new variable you can use this syntax:
C = data(1:ndata,1)
C = 10×1
0.9973 0.8726 0.9880 0.7613 0.8624 0.6040 0.1296 0.5866 0.7629 0.5752
  • Using break and continue in a while loop can make sense but only in specific case, the logic statement you give at the beginning of the while loop if effectively a continue statement:
a = 1;
while true %infinite loop
a = a +1;
if a > 10 %escape condition to prevent infinite loop
break
end
end
a
a = 11
a = 1;
while a <= 10
a = a +1;
end
a
a = 11
You can put your breaking condition in the while statement and keep a if ... break as an escape statement to prevent infinite loop, which is effectively what my first example is.
% this is a continue statement so you want to compute as long as the
% condition is not 1
condition = 0; %initialize condition so that we don't skip the loop at the first iteration
while not(condition)
%your code
condition = last_tol1 >= 0.9 && last_tol2 >= 0.9 && last_tol1 < 1 && last_tol2 < 1;
end
Unrecognized function or variable 'last_tol1'.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by