Changing values until a condition comes true

17 views (last 30 days)
Hamid
Hamid on 6 Nov 2015
Commented: Hamid on 6 Nov 2015
I have a code that produce some value that is from some equation with some inputs.
inputs are inserted by me at the first of code.
end of the code I have a condition must be met otherwise I have to change my inputs until condition met(Es>Estep).
How can I do that?
please help me out. this is my code :
function groundgrid
format short g
close all
clc
I=input('Please specify the rms symmetrical line to ground fault current in kA ');
IG=input('Please specify the maximum grid current in kA ');
tc=input('Please specify the duration of fault current in s ');
ts=input('Please specify the shock duration in s ');
Tm=input('Please specify which type connectors You intend to use in centigrade (for exothermic weld press 1083/for bolted press 250) ');
Ta=input('Please specify the ambient temperature in centugrade ');
mat=input('Please specify which material You intend to use (for copper press 1/for aluminum press 0) ');
shape=input('Please specify which shape of ground grid You intend to use (for square press 0/for rectangular press 1/for L-shaped press 2/for other shapes press 3) ');
Lg=input('Please specify the length of ground system in m ');
Wg=input('Please specify the width of ground system in m ');
Lx=input('Please specify the maximum length of the grid in the x direction in m ');
Ly=input('Please specify the maximum length of the grid in the y direction in m ');
D=input('Please specify the maximum distance between main conductors in m ');
Dm=input('Please specify the maximum distance between any two points on the grid in m ');
p=input('Please specify the resistivity of the surface material in ?.m ');
pS=input('Please specify the soil resistivity in ?.m ');
h=input('Please specify the depth of buried conductors in m ');
hs=input('Please specify the height of surface layer in m ');
nR=input('Please specify the minimum number of rods placed in area ');
Lr=input('Please specify the length of each rod in m ');
b=input('Please specify the diameter of rod in m ');
a=input('Please specify the diameter of conductor in m ');
weight=input('Please specify what is the weight of substation man (for 50kg press 1/for 70kg press 0) ');
gror=input('Please specify which grids You intend to use (for grids with no ground rods or without corner rods press 1/for grids with ground rods or with corner rods press 0) ');
% cross-section area:
if mat>0.5
TCAP=1;ar=1;pr=1;k0=1;
else
TCAP=10;ar=10;pr=10;k0=10;
end
A=I/sqrt(((TCAP*10^-4)/(tc*ar*pr))*log((k0+Tm)/(k0+Ta)));
% ground system resistance:
b=b/2;
d=a;
a=a/2;
Na=(Lg/D)+1;
Nb=(Wg/D)+1;
Lc=(Lg*Nb)+(Wg*Na);
if 0<h<(0.1*(sqrt(Lg*Wg)))
k2=(0.15*(Lg/Wg))+5.5;
k1=(-0.04*(Lg/Wg))+1.41;
elseif (0.1*(sqrt(Lg*Wg)))<h<((1/6)*(sqrt(Lg*Wg)))
k2=(0.10*(Lg/Wg))+4.68;
k1=(-0.05*(Lg/Wg))+1.20;
else
k2=(-0.05*(Lg/Wg))+4.40;
k1=(-0.05*(Lg/Wg))+1.13;
end
asec=sqrt(2*a*h);
R1=(p/(pi*Lc))*[log((2*Lc)/asec)+((k1*Lc)/sqrt(Lg*Wg))-k2];
R2=(p/(2*pi*nR*Lr))*[log((4*Lr)/b)-1+((2*k1*Lr)/(sqrt(Lg*Wg)))* ((sqrt(nR)- 1))^2];
Rm= (p/(pi*Lc))*[log((2*Lc)/Lr)+((k1*Lc)/sqrt(Lg*Wg))-k2+1];
Rg=(R1*R2-Rm^2)/(R1+R2-2*Rm);
% tolerable step and touch voltage:
CS=1-((0.09*(1-(p/pS)))/((2*hs)+0.09));
if weight==1
Estep=(1000+6*CS*pS)*(0.116/(sqrt(ts)));
Etouch=(1000+1.5*CS*pS)*(0.116/(sqrt(ts)));
else
Estep=(1000+6*CS*pS)*(0.157/(sqrt(ts)));
Etouch=(1000+1.5*CS*pS)*(0.157/(sqrt(ts)));
end
% step and mesh voltage:
if shape==0
na=(2*Lc)/(2*(Lg+Wg));
nb=1;
nc=1;
nd=1;
elseif shape==1
na=(2*Lc)/(2*(Lg+Wg));
nb=sqrt((2*(Lg+Wg))/(4*(sqrt(Lg*Wg))));
nc=1;
nd=1;
elseif shape==2
na=(2*Lc)/(2*(Lg+Wg));
nb=sqrt((2*(Lg+Wg))/(4*(sqrt(Lg*Wg))));
nc=[(Lx*Ly)/(Lg*Wg)]^((0.7*(Lg*Wg))/(Lx*Ly));
nd=1;
else na=(2*Lc)/(2*(Lg+Wg));
nb=sqrt((2*(Lg+Wg))/(4*(sqrt(Lg*Wg))));
nc=[(Lx*Ly)/(Lg*Wg)]^((0.7*(Lg*Wg))/(Lx*Ly));
nd=Dm/(sqrt((Lx)^2+(Ly)^2));
end
n=na*nb*nc*nd;
if gror==0
Kii=1;
LM=(Lc+[1.55+1.22*(Lr/((Lx)^2+(Ly)^2))]*(nR*Lr));
else
Kii=1/((2*n)^(2/n));
LM=Lc+(nR*Lr);
end
Kh=sqrt(1+(h/1));
Km=(1/(2*pi))* ((log(((D^2)/16*h*d))+(((D+2*h)^2)/(8*D*d))-(h/(4*d)))+((Kii/Kh)*log(8/(pi*(2*n-1)))));
Ki=0.644+(0.148*n);
Em=(p*Km*Ki*IG)/LM;
Ls=(0.75*Lc)+(0.85*nR*Lr);
Ks=(1/pi)*[(1/2*h)+(1/(D+h))+(1/D)*(1-(0.5)^(n-2))];
Es=(p*Ks*Ki*IG)/Ls;
if Es>Estep
end
end

Accepted Answer

Thorsten
Thorsten on 6 Nov 2015
Edited: Thorsten on 6 Nov 2015
function groundgrid
Es = 1; Estep = 1; % sample values such that the while loop is entered
while Es <= Estep
% put your code here
I = input ...
IG = input
end % while

More Answers (2)

the cyclist
the cyclist on 6 Nov 2015
Sounds to me like you could use a while loop.

Guillaume
Guillaume on 6 Nov 2015
Edited: Guillaume on 6 Nov 2015
A while loop, as per Thorsten or the cyclist's answer is the normal way to repeat a task until a condition is met. If you're happy with that just accept their answer.
However, from a usability point of view, wrapping all these input lines into a while loop is a really bad idea (Having all of these input lines to start with is a bad idea). If you only want to change just one value between each repeat, you still have to reenter all of them. And if you make a mistake, there's no way to go back and edit a value. You just have to abort and restart from scratch.
I would rethink the user interface of your function. Spending a bit more time to make it more flexible would make using the function much more bearable.
Ideally, you'd make a GUI but creating GUIs in matlab is a pain. You could stay with a text interface but for example at the start of the while loop (inside the loop), you could display the current state of the input variables, then prompt the user of the name(s) of the variable to modify. Then ask the new values for these variables.

Community Treasure Hunt

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

Start Hunting!