Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4. ' Declare all the variables that we're going to use
  5. Dim Mass_Sun, Mass_Earth, Radius_Earth, Radius_Sun As Double
  6. Dim X, Y, R, Vel_X, Vel_Y As Double
  7. ' Dim Accel, Accel_X, Accel_Y As Double
  8. Dim Step_T As Double
  9. Dim z(4), t As Double
  10.  
  11. ' Now define the starting values of these variables
  12. Mass_Sun = 100 : Radius_Sun = 40 ' sets the masses and radii
  13. Mass_Earth = 2 : Radius_Earth = 10 ' of the Sun and Earth
  14. X = 80 : Y = 0 ' sets the Earth's coords
  15. Vel_X = txt_Vel_X.Text : Vel_Y = txt_Vel_Y.Text ' read in the velocity components from the text boxes
  16. Step_T = 1 ' This is the increment in time between each loop
  17.  
  18. ' Now draw the Sun
  19. Call Plot(0, 0, Radius_Sun, Brushes.Orange)
  20.  
  21. While True ' start of loop
  22.  
  23. ' calculate the accel and new position of the Earth from Newton's Laws
  24.  
  25. R = Math.Sqrt(X ^ 2 + Y ^ 2)
  26.  
  27. 'Accel = -Mass_Sun / R ^ 2
  28. 'Accel_X = Accel * X / R : Accel_Y = Accel * Y / R
  29.  
  30. ' Vel_X = Vel_X + Accel_X * Step_T : Vel_Y = Vel_Y + Accel_Y * Step_T
  31.  
  32. ' X = X + Vel_X * Step_T : Y = Y + Vel_Y * Step_T
  33.  
  34. z(1) = X : z(2) = Y : z(3) = Vel_X : z(4) = Vel_Y
  35. Call RUNGE(t, z, 4, Step_T, TextBox1.Text)
  36. X = z(1) : Y = z(2) : Vel_X = z(3) : Vel_Y = z(4)
  37.  
  38. ' Plots the new Earth's position
  39. Call Plot(X, Y, Radius_Earth, Brushes.Aquamarine)
  40.  
  41. Call Delay(1) ' waits for a moment
  42.  
  43. ' Deletes the Earth from screen
  44. Call Plot(X, Y, Radius_Earth, Brushes.Black)
  45.  
  46. End While ' end of loop
  47.  
  48. End Sub
  49. Private Sub Plot(ByVal x As Double, ByVal y As Double, ByVal rad As Double, ByVal col As System.Drawing.Brush)
  50. ' This subroutine just plots a circle at the point (x,y)
  51.  
  52. Dim xplot, yplot, irad As Integer
  53. Dim xmin, xmax, ymin, ymax As Double
  54.  
  55. xmin = -100 : xmax = +100
  56. ymin = -100 : ymax = +100
  57.  
  58. xplot = (x - xmin) / (xmax - xmin) * PictureBox1.Width
  59. yplot = (-y - ymin) / (ymax - ymin) * PictureBox1.Height
  60. ' Note that VB has (0,0) at top left, so y-axis is inverted...
  61. irad = Int(rad)
  62.  
  63. PictureBox1.CreateGraphics.FillEllipse _
  64. (col, xplot, yplot, irad, irad)
  65. ' (Brushes.Green, xplot, yplot, 10, 10)
  66.  
  67. End Sub
  68. Private Sub Delay(ByVal t As Integer)
  69. Dim i As Integer
  70. For i = 1 To t * 10000000
  71.  
  72. Next i
  73. End Sub
  74.  
  75. Sub RUNGE(ByVal t, ByVal z, ByVal N, ByVal h, ByVal J)
  76.  
  77. Dim z_Mid(N), dzdt(N), k1(N), k2(N), k3(N), k4(N) As Double
  78.  
  79. Dim I As Integer
  80.  
  81. Call ders(t, z, N, dzdt)
  82. For I = 1 To N
  83. k1(I) = h * dzdt(I)
  84. z_Mid(I) = z(I) + 0.5 * k1(I)
  85. Next I
  86.  
  87. Call ders(t + 0.5 * h, z_Mid, N, dzdt)
  88. For I = 1 To N
  89. k2(I) = h * dzdt(I)
  90. z_Mid(I) = z(I) + 0.5 * k2(I)
  91. Next I
  92.  
  93. Call ders(t + 0.5 * h, z_Mid, N, dzdt)
  94. For I = 1 To N
  95. k3(I) = h * dzdt(I)
  96. z_Mid(I) = z(I) + k3(I)
  97. Next I
  98.  
  99. Call ders(t + h, z_Mid, N, dzdt)
  100. For I = 1 To N
  101. k4(I) = h * dzdt(I)
  102. Next I
  103.  
  104. If J = 1 Then
  105. For I = 1 To N
  106. z(I) = z(I) + k1(I)
  107. Next I
  108. ElseIf J = 2 Then
  109. For I = 1 To N
  110. z(I) = z(I) + k2(I)
  111. Next I
  112. ElseIf J = 4 Then
  113. For I = 1 To N
  114. z(I) = z(I) + k1(I) / 6 + k2(I) / 3 + k3(I) / 3 + k4(I) / 6
  115. Next I
  116. Else
  117. MsgBox("Order of Runge-Kutta requested is invalid")
  118. End
  119. End If
  120.  
  121. t = t + h
  122.  
  123. End Sub ' End of Runge Kutta Subroutine
  124.  
  125. Sub ders(ByVal t, ByVal z, ByVal N, ByVal dzdt)
  126. Dim G, Mass_Sun As Double
  127. ' put all the necessary declarations here...
  128.  
  129. Mass_Sun = 100
  130. G = 1
  131.  
  132. dzdt(1) = z(3) ' put the differential equation for z(1) here
  133. dzdt(2) = z(4) ' ditto for z(2)
  134. dzdt(3) = -G * ((Mass_Sun * z(1)) / (z(1) ^ 2 + z(2) ^ 2) ^ (3 / 2)) ' ditto for z(3)
  135. dzdt(4) = -G * ((Mass_Sun * z(2)) / (z(1) ^ 2 + z(2) ^ 2) ^ (3 / 2)) ' ditto for z(4)
  136. End Sub
  137.  
  138. End Class
Add Comment
Please, Sign In to add comment