Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #= Preamble:
- Import required modules, set up plotting, define various constants to be used later
- ==============================================================================#
- using Plots
- gr()
- const r = 1
- const n = 5
- const δ = 2*r/1000 #1/1000th of the diameter used as fixed length
- #= Generates a random point within a disk of radius r
- ==============================================================================#
- function PointRandomDiskCartesian(r=1)
- while true
- Potential_Points = [rand()*(r*2)-r rand()*(r*2)-r]
- if norm(Potential_Points) <=r
- return Potential_Points
- end
- end
- end
- #= Generates matrix of n points within a disk radius r
- ==============================================================================#
- function PointsRand(n, r=1)
- PointMatrix = zeros(n, 2)
- for i in 1:n
- PointMatrix[i, :] = PointRandomDiskCartesian(r)
- end
- return PointMatrix
- end
- #= Randomly increases/decreases one dimension of chosen random point by value δ
- ==============================================================================#
- function RandomPerturbation(Points, r, δ)
- while true
- rand_point_index = rand(1:size(Points)[1])
- rand_x_or_y = rand(1:size(Points)[2])
- rand_add_or_subt = rand([+1 -1])
- Potential_Points = Points[rand_point_index, :]
- Potential_Points[rand_x_or_y] += rand_add_or_subt*δ
- if norm(Potential_Points) <=r
- Points[rand_point_index, :] = Potential_Points
- return Points
- end
- end
- end
- OriginalPoints = PointsRand(5) #Calls PointsRand getting 5 random Cartesian coordinates
- println(OriginalPoints) #Prints them now, so it can be checked later
- PerturbedPoints = RandomPerturbation(OriginalPoints, r, δ) #Calls the perturbation function which takes in OritinalPoints and returns a perturbed version
- println(PerturbedPoints) #Prints the perturbed points
- println(OriginalPoints) #Prints the original points again
- println(OriginalPoints.-PerturbedPoints) #Now perturbed and original points are equal... why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement