how to get back to the main Function after a for loop is finished

3 visualizaciones (últimos 30 días)
A function where I have a FOR loop runs indefinitely when that function is a sub function of the main function. How can I get back to the main function when the FOR loop is done? this problem happends when the input data is read through a text file otherwise the code runs fine. This is the code, the first part is the main function and the second function is the sub function. I have attached the text file as well. Thanks.
function [x,v,SHmaxx]=MySHmaxMatrixPolygon(x1)
%input data
load PolyDa.txt;
miu=0.6;
pp=PolyDa(1,8);
S2=PolyDa(1,10);
S3=PolyDa(1,11);
Sv=S2;
%Polygon boundaries
Fmiu=(sqrt(miu^2+1)+miu)^2;
A1=((Sv-pp)/Fmiu)+pp;
D1=Fmiu*(Sv-pp)+pp;
n=round(D1-A1)-1;
A2=((Sv-pp)/Fmiu)+pp;
B1=((Sv-pp)/Fmiu)+pp;
B2=Sv;
C1=Sv;
C2=Fmiu*(Sv-pp)+pp;
D2=Fmiu*(Sv-pp)+pp;
E1=Sv;
E2=Sv;
F1=A1;
F2=A2;
xx = [A1 B1 C1 D1 E1 F1 B1 E1 E1 C1];
yy = [A2 B2 C2 D2 E2 F2 B2 E2 E2 C2];
%Calling function polygon to calculate SHmax(x) at UCS1
z=zeros(n,1)+x1;
[x,fval]=fsolve(@polygon,z);
end
%% THE SUB FUNCTION THAT HAS THE ISSUE WITH THE FOR LOOP IS THIS ONE:
function P=polygon(xi)
% data
load PolyDa.txt;
trend=PolyDa(1,4);
plunge=PolyDa(1,5);
rake=PolyDa(1,6);
nu=PolyDa(1,13);
pp=PolyDa(1,8);
Pm=PolyDa(1,12);
S2=PolyDa(1,10);
wbo=PolyDa(1,7);
FA=PolyDa(1,15);
Biot=PolyDa(1,14);
azi=PolyDa(1,3);
incl=PolyDa(1,2);
To=PolyDa(1,9);
depth=PolyDa(1,1);
Sv=S2;
miu=0.6;
Fmiu=(sqrt(miu^2+1)+miu)^2;
A1=((Sv-pp)/Fmiu)+pp;
D1=Fmiu*(Sv-pp)+pp;
C1=Sv;
n=round(D1-A1)-1;
Sx=zeros(n,1);
S3=zeros(n,1);
Sgf=zeros(3,3);
Rbf=zeros(3,3);
Sbf=zeros(3,3);
Sf=zeros(3,3);
thetamax=zeros(n,1);
sTZf=zeros(n,1);
sZZf=zeros(n,1);
sTTf=zeros(n,1);
thetab=zeros(n,1);
st_min=zeros(n,1);
st_max=zeros(n,1);
Sigma1=zeros(n,1);
Sigma3=zeros(n,1);
P=zeros(n,1);
%calculating steps
s=(((D1+C1)/2)-(A1-1))/n;
UCS=zeros(n,1)+PolyDa(1,16);
Rgf=[cosd(trend)*cosd(plunge) cosd(plunge)*sind(trend) -sind(plunge); -cosd(rake)*sind(trend)+cosd(trend)*sind(plunge)*sind(rake) cosd(trend)*cosd(rake)+sind(plunge)*sind(rake)*sind(trend) cosd(plunge)*sind(rake); cosd(trend)*cosd(rake)*sind(plunge)+sind(trend)*sind(rake) cosd(rake)*sind(trend)*sind(plunge)-cosd(trend)*sind(rake) cosd(plunge)*cosd(rake)];
Rbf=[cosd(incl)*cosd(azi), sind(azi)*cosd(incl), -sind(incl); -sind(azi), cosd(azi), 0; sind(incl)*cosd(azi), sind(incl)*sind(azi), cosd(incl)];
DP=Pm-Biot*pp;
for i=2:n+1
Sx(1)=A1-1;
Sx(i)=Sx(i-1)+s;
S3(i-1)=Sx(i-1);
Sf=[xi(i-1) 0 0;0 S2 0;0 0 S3(i-1)];
%Evaluation of Rg
Sgf=Rgf'*Sf*Rgf;
%Evaluation of Sb
Sbf=Rbf*(Sgf*Rbf');
thetamax(i-1)= double(thetamaxx([azi,incl],[trend,plunge,rake],nu,pp,Pm,xi(i-1),S2,S3(i-1),wbo,FA,Biot));
thetab(i-1)=double((thetamax(i-1)-wbo/2)*3.14/180);
sZZf=Sbf(3,3)-2*nu*(Sbf(1,1)-Sbf(2,2))*cos(2*thetab)-4*nu*Sbf(1,2)*sin(2*thetab)-Biot*pp;
sTTf=Sbf(1,1)+Sbf(2,2)-2*(Sbf(1,1)-Sbf(2,2))*cos(2*thetab)-4*Sbf(1,2)*sin(2*thetab)-Pm-Biot*pp;
sTZf=2*(Sbf(2,3)*cos(thetab)-Sbf(1,3)*sin(thetab));
%Evaluate min tangential stress
st_min(i-1)=0.5*(sZZf(i-1)+sTTf(i-1)-sqrt((sZZf(i-1)-sTTf(i-1)).^2+4*sTZf(i-1).^2));
%Evaluate max tangential stress
st_max(i-1)=0.5*(sZZf(i-1)+sTTf(i-1)+sqrt((sZZf(i-1)-sTTf(i-1)).^2+4*sTZf(i-1).^2));
%Principal stresses
Sigma1(i-1)=max([st_max(i-1),st_min(i-1),DP]);
Sigma3(i-1)=min([st_max(i-1),st_min(i-1),DP]);
P(i-1)= Sigma1(i-1)-(UCS(i-1)+Sigma3(i-1)*(tand(45+FA/2))^2);
end
  2 comentarios
Adam
Adam el 3 de Mayo de 2019
If n is something valid then the for loop should terminate and return to the calling function anyway. You can easily check what n is by debugging.
leydy Garcia
leydy Garcia el 3 de Mayo de 2019
Thanks Adam for your comment. Could you please tell me what you mean by valid? what condition does n have to have to be valid? when I run the code, n is always a rounded number.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 3 de Mayo de 2019
Loading data inside by load() consumes a lot of time. Doing this repeatedly is a bad idea and slows down the processing massively. Better use a persistent variable and load the data once.
The next problem is loading the contents if the file directly to the workspace. This is critical if the MAT file contains a variable which is used inside the function also. The dynamic creation of varibales in the workspace impedes the JIT-acceleration, which slows down the processing again.
It is unlikely, that the loop works differently depending from where the function is called. So I assume it is only slow. You can check this easily using the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html . Set a breakoint and step through the code. As soon as the for loop is ready, the subfunction returns to the caller automatically. You do not have to trigger this explicitly.
  6 comentarios
Jan
Jan el 5 de Mayo de 2019
Editada: Jan el 5 de Mayo de 2019
Catch the output of load():
data = load('PolyDa.txt');
Now instead of having the data directly in the workspace, they are available as fields of the struct data.
It is hard to find an error, because you do not mention, what the error is. It is impossible that "the for loops keeps overrunning". Matlab does not behave magically. The loop does exactly what it is instructed to do. If all values from 2 to n+1 are processed, the loop is left and Matlab returns to the caller. As far as I understand your explanations, you claim that the loop is not left regularily. Because this is completely impossible in Matla, you should use the debugger to see, what happens instead.
"A function where I have a FOR loop runs indefinitely when that function is a sub function of the main function." No, this cannot be true. So please examine again, what you consider as problem. Why do you assume, that this loop runs infinitely? Chekc this again, because this assumption cannot be true.
Unfortunately the code is very hard to read in the forum's interface. Limit the line length to improve the readability. I cannot run it, because thetamaxx and thetamax are undefined. What is the input x1? There are many warning message in the editor. What about fixing them? A code with so many warning is not clean.
leydy Garcia
leydy Garcia el 5 de Mayo de 2019
THanks Jan. Actually I found no error. I wrote all again whitout leaving long spaces between sentences and very organized. I did not change the code, just wrote all very organized within the FOR LOOP and worked.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by