# While loop iteration

I think there is a problem with my while loop in this code. My aim is to find new values of (S_0 and z_0) in my code if the condition in the while loop holds. I keep getting just one iteration count thou I expect more than one count. or is there any problem with where I place the z_0 outside the function? Any suggestions and advice to get this work done will be appreciated, please. My code is below

``````
using LinearAlgebra, BenchmarkTools

e=[] ;f=[] ; g=[] ; h=[] ; A1=[] ; T1=[]

n=7
W=[4 3 5 6 7 6 5] # Various weights
x=[33 15 2 5 39 17 11] #X-cordinates
y=[41 11 4 45 3 26 4] #y-cordinates

x_0=sum(W.*x)/sum(W) #Finding the point x0
y_0=sum(W.*y)/sum(W) # Finding the point y0
XY=[x_0,y_0] ## cordinate for the point x0,y0

#Implementing equation 4.1 and 4.3
function algorithm(S_0,z_0)

###THE VARIOUS POINTS THAT BRACKETS THE MINIMUM

(s1,z1)=(2,3) # First point that brackets the minimum
(s2,z2)= (4,5) # Second point that brackets the minimum
(s3,z3)= (6,9) # Third point that brackets the minimum

S_0 = 0.5* ((s2^2 -s3^2)*z1 + (s3^2 - s1^2)*z2 + (s1^2-s2^2)*z3 / (s2 -s3)*z1 + (s3 - s1)*z2 + (s1-s2)*z3) #Eqn 4.3

for i in 1:n
a= x_0 - x[i] #xi = x^k - x_j
b= y_0 - y[i] #yi = y^k - y_j
a1=a
b1=b
A=[a1,b1]

TopLamda_x=W[i].*a # To find the numerator of Lamda_x
TopLamda_y=W[i].*b # To find the numerator of Lamda_y
push!(e,TopLamda_x) ; push!(f,a) ; push!(g,TopLamda_y) ; push!(h,b) ;  append!((A1),(a,b))

###The Loop helps to substract s*Lamda form xi_j
NormLamda_x=norm(f) # Find the norm for Lamda x denominator
NormLamda_y=norm(h) # Find the norm for Lamda y denominator
Lamda_x=sum(e/NormLamda_x) # To calculate Lamda x
Lamda_y=sum(g/NormLamda_y) # To calculate Lamda y
B=[Lamda_x,Lamda_y] # An array to store both x and y cordinates of Lamda
C=S_0*B # use s to multiply Lamda

T=A1[i].-C
append!((T1),T) # Save values after each iteration into T1

##Finding T(x)
## CALCULATING THE DISTANCE

D_ip1=[] # An array to store the distance from the initial point to each cordinate

D_ip= ((x[i]-x_0)^2 + (y[i]-y_0)^2)^0.5 # Finding the distance from initial point to each cordinate
push!(D_ip1, D_ip) # Storing the distance after each iteration

sum_distance= sum(D_ip1) # Summing all distances for the various iterations

# FINDING THE Numerator for x-cordinate of partial derivate
Top_derivativeX1=[]  # An array to store the partial derivative of x_cordinate(numerator)
Top_derivativeX= W[i]*(x[i] - x_0 )
push!(Top_derivativeX1, Top_derivativeX) #store the partial derivative of x_cordinate(numerator) after each iteration

sum_partialX= sum(Top_derivativeX1) # Find the total sum for all iterations for x.

partialX= (sum_partialX / sum_distance) # This is the partial derivative of x representing x cordinate

#Numerator for Y-cordinate of of partial derivate

Top_derivativeY1=[] # An array to store the partial derivative of y_cordinate(numerator)

Top_derivativeY= W[i]*(y[i] - y_0 )
push!(Top_derivativeY1, Top_derivativeY) #store the partial derivative of y_cordinate(numerator) after each iteration

sum_partialY= sum(Top_derivativeY1) # Find the total sum for all iterations for y.

partialY= (sum_partialY / sum_distance) # This is the partial derivative of y representing y cordinate

DerivativeXY=[partialX,partialY] ##Store the partial derivative of X and Y
sDerivativeXY= S_0*DerivativeXY ## Multiply S by the partial derivative

XYnew= (XY .- sDerivativeXY) ## Find new cordinate for X ## T of x in eqn 2.2

end

end
algorithm(S_0,z_0)

#norm(T1) # To find the Norm of xi_j -s*Lamda

z_0=sum(W.*norm(T1)) # To find the total sum by multiplying W_j with T1 ###Eqn 4.1

## Stopping Criterion
function stop()
count=0;
while(abs(XYnew-XY) >= 0.01  && abs(XYnew-XY) >= 0.01)
XY=XYnew
(S_0, z_0)= algorithm(S_0, z_0)

count = count + 1;
end
return XYnew

end

stop()

``````

When you enter this function, what is `XYnew`?

It is very, very hard to debug such problems with the use global variables like that. And, additionally, that will totally kill the performance of your code. See the first tip in: Performance Tips · The Julia Language.

In the process of debugging your error, I would suggest you to remove progressively all global variables, you will probably find what is wrong in the meantime.

1 Like

XYnew is defined in the first function. It stores the values of S_0 and z_0. I just called it in the second function since it is already defined in the first function. The aim is to get new values for XYnew as the while loop holds. I will work on what you suggested, please. Thank you.

You should then be getting an error of `XYNew` not being defined when you call `stop()`, because `XYNew` is local to that first function, not a global variable.

If you are not getting that error, is probable that you had defined another `XYNew` as a global variable somewhere else, and your `stop()` function is doing nothing related to what is going on in the first function.

This is what happens if I just copy/paste the code there into a new Julia section:

``````julia> stop()
ERROR: UndefVarError: XYnew not defined
Stacktrace:
 stop()
@ Main ./REPL:4
 top-level scope
@ REPL:1
``````

(appart from other errors before that)

Okay. I get you.
I need to look into this again.

Thank you.

1 Like