Advertisement
RankWinner

Untitled

Mar 31st, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.11 KB | None | 0 0
  1. #= Preamble:
  2. Import required modules, set up plotting, define various constants to be used later
  3. ==============================================================================#
  4. using Plots
  5. gr()
  6.  
  7. const r = 1
  8. const n = 5
  9. const δ = 2*r/1000    #1/1000th of the diameter used as fixed length
  10.  
  11.  
  12. #= Generates a random point within a disk of radius r
  13. ==============================================================================#
  14. function PointRandomDiskCartesian(r=1)
  15.     while true
  16.         Potential_Points = [rand()*(r*2)-r rand()*(r*2)-r]
  17.  
  18.         if norm(Potential_Points) <=r
  19.             return Potential_Points
  20.         end
  21.     end
  22. end
  23.  
  24.  
  25. #= Generates matrix of n points within a disk radius r
  26. ==============================================================================#
  27. function PointsRand(n, r=1)
  28.     PointMatrix = zeros(n, 2)
  29.  
  30.     for i in 1:n
  31.         PointMatrix[i, :] = PointRandomDiskCartesian(r)
  32.     end
  33.     return PointMatrix
  34. end
  35.  
  36. #= Randomly increases/decreases one dimension of chosen random point by value δ
  37. ==============================================================================#
  38. function RandomPerturbation(Points, r, δ)
  39.     while true
  40.         rand_point_index = rand(1:size(Points)[1])
  41.         rand_x_or_y      = rand(1:size(Points)[2])
  42.         rand_add_or_subt = rand([+1 -1])
  43.  
  44.         Potential_Points = Points[rand_point_index, :]
  45.         Potential_Points[rand_x_or_y] += rand_add_or_subt*δ
  46.  
  47.         if norm(Potential_Points) <=r
  48.             Points[rand_point_index, :] = Potential_Points
  49.             return Points
  50.         end
  51.     end
  52. end
  53.  
  54. OriginalPoints  = PointsRand(5) #Calls PointsRand getting 5 random Cartesian coordinates
  55. println(OriginalPoints)         #Prints them now, so it can be checked later
  56.  
  57. PerturbedPoints = RandomPerturbation(OriginalPoints, r, δ) #Calls the perturbation function which takes in OritinalPoints and returns a perturbed version
  58. println(PerturbedPoints) #Prints the perturbed points
  59.  
  60. println(OriginalPoints) #Prints the original points again
  61.  
  62. println(OriginalPoints.-PerturbedPoints) #Now perturbed and original points are equal... why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement