how to get back to the main Function after a for loop is finished
Show older comments
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 Comments
Adam
on 3 May 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
on 3 May 2019
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!