Advertisement
EXTREMEXPLOIT

Circular Motion Aproximation

May 14th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.09 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////
  2. //State Initial Variables
  3. //Question 2.1.1: your code here:
  4. float Theta = radians(30.0); // Ángulo inicial
  5. float VelocidadAngular = 4; // Velocidad Angular (Rad/S)
  6.  
  7. ////////////////////////////////////////////////////////////////////////////
  8. //Question 2.1.2: your code here:
  9. int ArraySize = 3; // Tamaño del array.
  10. int sTheta = 0; // Posición del ángulo.
  11. int sSpeed = 1; // Posición de la Velocidad Angular.
  12. int sTime = 2; // Posición del Tiempo.
  13.  
  14. // EC
  15. float[] EulerCromer_State = new float[ArraySize]; //El Array que se usa para el Euler-Crome.
  16.  
  17. // RK2
  18. float[] RK2_State = new float[ArraySize]; // El Array que se usa para el Runge-Kutta 2.
  19.  
  20. // RK4
  21. float[] RK4_State = new float[ArraySize]; // El Array que se usa para el Runge-Kutta 4.
  22.  
  23. ////////////////////////////////////////////////////////////////////////////
  24. //Window set up variables
  25. int WindowWidthHeight = 300;
  26. float WorldSize = 5.0;
  27. float PixelsPerMeter = (WindowWidthHeight ) / WorldSize;
  28. float OriginPixelsX = 0.5 * WindowWidthHeight;
  29. float OriginPixelsY = 0.5 * WindowWidthHeight;
  30.  
  31. ////////////////////////////////////////////////////////////////////////////
  32. //Global constants
  33. float g = 9.8;// gravity = 9.8 m/s^2
  34. float radius = 1.5;
  35. float deltaT = 1.0/60.0; // Frame rate of 60 fps
  36. ///////////////////////////////////////////////////////////////////////////
  37.  
  38. void setup()
  39. {
  40.     /////////////////////////////////////////////////////////////////////////
  41.     //Create initial state.
  42.     //Question 2.1.3: Your code here
  43.     // EC
  44.     EulerCromer_State[sTheta] = Theta;
  45.     EulerCromer_State[sSpeed] = VelocidadAngular;
  46.     EulerCromer_State[sTime] = 0; // Tiempo inicial.
  47.    
  48.     // RK2
  49.     RK2_State[sTheta] = Theta;
  50.     RK2_State[sSpeed] = VelocidadAngular;
  51.     RK2_State[sTime] = 0; // Tiempo inicial.
  52.    
  53.     // RK4
  54.     RK4_State[sTheta] = Theta;
  55.     RK4_State[sSpeed] = VelocidadAngular;
  56.     RK4_State[sTime] = 0; // Tiempo inicial.
  57.  
  58.     /////////////////////////////////////////////////////////////////////////
  59.    
  60.     // Set up normalized colors.
  61.     colorMode( RGB, 1.0 );
  62.    
  63.     // Set up the stroke color and width.
  64.     stroke( 0.0 );
  65.    
  66.     // Create the window size, set up the transformation variables.
  67.     size( 300, 700 );
  68.     PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
  69.     OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
  70.     OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
  71.  
  72.     // Translate to the origin.
  73.     translate( OriginPixelsX, OriginPixelsY );
  74.    
  75.     // Set frame rate
  76.     frameRate( 1.0/deltaT );
  77.  
  78. }  
  79.    
  80. void timeStepEC( float delta_t )
  81. {
  82.     /////////////////////////////////////////////////////////////////////////
  83.     //EULER-CROMER METHOD. Question 2.1.4
  84.     float TangentialAccelerationCR = -g * EulerCromer_State[sTheta]; // Cálculo de la acceleración tangencial.
  85.     float AngularAccelerationCR = TangentialAccelerationCR / radius; // Cálculo de la acceleración angular.
  86.    
  87.     EulerCromer_State[sSpeed] += AngularAccelerationCR * delta_t; // Velocidad Angular = Velocidad Angular + Acceleración Angular * delta_t
  88.     EulerCromer_State[sTheta] += delta_t * EulerCromer_State[sSpeed]; // Theta = Theta + delta_t * Velocidad Angular
  89.     EulerCromer_State[sTime] += delta_t; // Tiempo Total = Tiempo Total + delta_t
  90.     /////////////////////////////////////////////////////////////////////////
  91. }
  92.  
  93. void timeStepRK2( float delta_t )
  94. {
  95.   /////////////////////////////////////////////////////////////////////////
  96.   // RK2 METHOD. Question 2.1.4  
  97.   float Speed1 = RK2_State[sSpeed];
  98.   float Acceleration1 = -(g / radius) * sin(RK2_State[sTheta]); // Acceleración Angular.
  99.   float Speed2 = RK2_State[sSpeed] + (delta_t * Acceleration1);
  100.   float TempTheta = RK2_State[sTheta] + (delta_t * Speed1);
  101.   float Acceleration2 = - (g / radius) * TempTheta;
  102.  
  103.   RK2_State[sTheta] += (delta_t / 2.0) * (Speed1 + Speed2); // Actualizando la posición.
  104.   RK2_State[sSpeed] += (delta_t / 2.0) * (Acceleration1 + Acceleration2); // Actualizando la velocidad.
  105.   RK2_State[sTime] += delta_t; // Actualizando el tiempo.
  106.   /////////////////////////////////////////////////////////////////////////
  107. }
  108.  
  109. void timeStepRK4( float delta_t )
  110. {
  111.   /////////////////////////////////////////////////////////////////////////
  112.   // RK4 METHOD. Question 2.1.4
  113.   float Speed1 = RK4_State[sSpeed];
  114.   float Acceleration1 = - ( g / radius ) * RK4_State[sTheta];
  115.   float Speed2 = RK4_State[sSpeed] + (( delta_t / 2.0 ) * Acceleration1 );
  116.   float TempTheta1 = RK4_State[sTheta] + (( delta_t / 2.0 ) * Speed1 );
  117.   float Acceleration2 = - ( g / radius ) * TempTheta1;
  118.   float Speed3 = RK4_State[sSpeed] + (( delta_t / 2.0 ) * Acceleration2 );
  119.   float TempTheta2 = RK4_State[sTheta] + (( delta_t / 2.0 ) * Speed2 );
  120.   float Acceleration3 = - ( g / radius ) * TempTheta2;
  121.   float Speed4 = RK4_State[sSpeed] + ( delta_t * Acceleration3 );
  122.   float TempTheta3 = RK4_State[sTheta] + ( delta_t * Speed3 );
  123.   float Acceleration4 = - ( g / radius ) * TempTheta3;
  124.  
  125.   RK4_State[sTheta] += (delta_t / 6.0) * (Speed1 + (2.0 * Speed2) + (2.0 * Speed3) + Speed4); // Actualizando la posición.
  126.   RK4_State[sSpeed] += (delta_t / 6.0) * (Acceleration1 + (2.0 * Acceleration2) + (2.0 * Acceleration3) + Acceleration4); // Actualizando la velocidad.
  127.   RK4_State[sTime]  += delta_t; // Actualizando el tiempo.
  128.  
  129.   /////////////////////////////////////////////////////////////////////////
  130. }
  131.  
  132. // The DrawState function assumes that the coordinate space is that of the
  133. // simulation - namely, meters, with the circle center placed at the origin.
  134. // There is currently a bug in processing.js which requires us to do the
  135. // pixels-per-meter scaling ourselves.
  136. void DrawState()
  137. {
  138.     // Compute ball position
  139.     /////////////////////////////////////////////////////////////////////////
  140.     //Question 2.1.5 Your code here:
  141.     // Euler-Cromer
  142.     float BallX_EC =  PixelsPerMeter * sin( EulerCromer_State[sTheta] ) * radius;
  143.     float BallY_EC =  PixelsPerMeter * cos( EulerCromer_State[sTheta] ) * radius;
  144.     // RK2
  145.     float BallX_RK2 =  PixelsPerMeter * sin( RK2_State[sTheta] ) * radius;
  146.     float BallY_RK2 =  PixelsPerMeter * cos( RK2_State[sTheta] ) * radius;
  147.     // RK4
  148.     float BallX_RK4 =  PixelsPerMeter * sin( RK4_State[sTheta] ) * radius;
  149.     float BallY_RK4 =  PixelsPerMeter * cos( RK4_State[sTheta] ) * radius;
  150.    
  151.     /////////////////////////////////////////////////////////////////////////
  152.            
  153.     // Draw ball, circles and text
  154.     float ballDiameter = PixelsPerMeter * 0.3;
  155.     // Draw ball EC
  156.     translate( OriginPixelsX, OriginPixelsY );
  157.     fill( 1.0, 0.0, 0.0 ); // Red
  158.     ellipse( BallX_EC, BallY_EC, ballDiameter, ballDiameter );
  159.     // Draw circle
  160.     fill( 0 , 0 );
  161.     ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
  162.     textSize( 18 );
  163.     fill(0);
  164.     text( "Euler-Cromer", -56, 5 );
  165.    
  166.     // Draw ball RK2
  167.     translate( 0, 210 );
  168.     fill( 0.0, 1.0, 0.0 ); // Green
  169.     ellipse( BallX_RK2, BallY_RK2, ballDiameter, ballDiameter );
  170.     // Draw circle
  171.     fill( 0 , 0 );
  172.     ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
  173.     fill(0);
  174.     text( "RK 2", -20, 5 );
  175.  
  176.     // Draw ball RK4
  177.     translate( 0, 210 );
  178.     fill( 0.0, 0.0, 1.0 ); // Blue
  179.     ellipse( BallX_RK4, BallY_RK4, ballDiameter, ballDiameter );
  180.     // Draw circle
  181.     fill( 0 , 0 );
  182.     ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
  183.     fill(0);
  184.     text( "RK 4", -20, 5 );
  185. }
  186.  
  187.  
  188. // The draw function creates a transformation matrix between pixel space
  189. // and simulation space, in meters, and then calls the DrawState function.
  190. // Unfortunately, there is currently a bug in processing.js with concatenated
  191. // transformation matrices, so we have to do the coordinate scaling ourselves
  192. // in the draw function.
  193. void draw()
  194. {
  195.     //Time Step
  196.     timeStepEC(deltaT);
  197.     timeStepRK2(deltaT);
  198.     timeStepRK4(deltaT);
  199.  
  200.     //Clear the display to a constant color
  201.     background( 0.75 );
  202.    
  203.     // Draw the simulation
  204.     DrawState();    
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement