Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////
- //State Initial Variables
- //Question 2.1.1: your code here:
- float Theta = radians(30.0); // Ángulo inicial
- float VelocidadAngular = 4; // Velocidad Angular (Rad/S)
- ////////////////////////////////////////////////////////////////////////////
- //Question 2.1.2: your code here:
- int ArraySize = 3; // Tamaño del array.
- int sTheta = 0; // Posición del ángulo.
- int sSpeed = 1; // Posición de la Velocidad Angular.
- int sTime = 2; // Posición del Tiempo.
- // EC
- float[] EulerCromer_State = new float[ArraySize]; //El Array que se usa para el Euler-Crome.
- // RK2
- float[] RK2_State = new float[ArraySize]; // El Array que se usa para el Runge-Kutta 2.
- // RK4
- float[] RK4_State = new float[ArraySize]; // El Array que se usa para el Runge-Kutta 4.
- ////////////////////////////////////////////////////////////////////////////
- //Window set up variables
- int WindowWidthHeight = 300;
- float WorldSize = 5.0;
- float PixelsPerMeter = (WindowWidthHeight ) / WorldSize;
- float OriginPixelsX = 0.5 * WindowWidthHeight;
- float OriginPixelsY = 0.5 * WindowWidthHeight;
- ////////////////////////////////////////////////////////////////////////////
- //Global constants
- float g = 9.8;// gravity = 9.8 m/s^2
- float radius = 1.5;
- float deltaT = 1.0/60.0; // Frame rate of 60 fps
- ///////////////////////////////////////////////////////////////////////////
- void setup()
- {
- /////////////////////////////////////////////////////////////////////////
- //Create initial state.
- //Question 2.1.3: Your code here
- // EC
- EulerCromer_State[sTheta] = Theta;
- EulerCromer_State[sSpeed] = VelocidadAngular;
- EulerCromer_State[sTime] = 0; // Tiempo inicial.
- // RK2
- RK2_State[sTheta] = Theta;
- RK2_State[sSpeed] = VelocidadAngular;
- RK2_State[sTime] = 0; // Tiempo inicial.
- // RK4
- RK4_State[sTheta] = Theta;
- RK4_State[sSpeed] = VelocidadAngular;
- RK4_State[sTime] = 0; // Tiempo inicial.
- /////////////////////////////////////////////////////////////////////////
- // Set up normalized colors.
- colorMode( RGB, 1.0 );
- // Set up the stroke color and width.
- stroke( 0.0 );
- // Create the window size, set up the transformation variables.
- size( 300, 700 );
- PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
- OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
- OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
- // Translate to the origin.
- translate( OriginPixelsX, OriginPixelsY );
- // Set frame rate
- frameRate( 1.0/deltaT );
- }
- void timeStepEC( float delta_t )
- {
- /////////////////////////////////////////////////////////////////////////
- //EULER-CROMER METHOD. Question 2.1.4
- float TangentialAccelerationCR = -g * EulerCromer_State[sTheta]; // Cálculo de la acceleración tangencial.
- float AngularAccelerationCR = TangentialAccelerationCR / radius; // Cálculo de la acceleración angular.
- EulerCromer_State[sSpeed] += AngularAccelerationCR * delta_t; // Velocidad Angular = Velocidad Angular + Acceleración Angular * delta_t
- EulerCromer_State[sTheta] += delta_t * EulerCromer_State[sSpeed]; // Theta = Theta + delta_t * Velocidad Angular
- EulerCromer_State[sTime] += delta_t; // Tiempo Total = Tiempo Total + delta_t
- /////////////////////////////////////////////////////////////////////////
- }
- void timeStepRK2( float delta_t )
- {
- /////////////////////////////////////////////////////////////////////////
- // RK2 METHOD. Question 2.1.4
- float Speed1 = RK2_State[sSpeed];
- float Acceleration1 = -(g / radius) * sin(RK2_State[sTheta]); // Acceleración Angular.
- float Speed2 = RK2_State[sSpeed] + (delta_t * Acceleration1);
- float TempTheta = RK2_State[sTheta] + (delta_t * Speed1);
- float Acceleration2 = - (g / radius) * TempTheta;
- RK2_State[sTheta] += (delta_t / 2.0) * (Speed1 + Speed2); // Actualizando la posición.
- RK2_State[sSpeed] += (delta_t / 2.0) * (Acceleration1 + Acceleration2); // Actualizando la velocidad.
- RK2_State[sTime] += delta_t; // Actualizando el tiempo.
- /////////////////////////////////////////////////////////////////////////
- }
- void timeStepRK4( float delta_t )
- {
- /////////////////////////////////////////////////////////////////////////
- // RK4 METHOD. Question 2.1.4
- float Speed1 = RK4_State[sSpeed];
- float Acceleration1 = - ( g / radius ) * RK4_State[sTheta];
- float Speed2 = RK4_State[sSpeed] + (( delta_t / 2.0 ) * Acceleration1 );
- float TempTheta1 = RK4_State[sTheta] + (( delta_t / 2.0 ) * Speed1 );
- float Acceleration2 = - ( g / radius ) * TempTheta1;
- float Speed3 = RK4_State[sSpeed] + (( delta_t / 2.0 ) * Acceleration2 );
- float TempTheta2 = RK4_State[sTheta] + (( delta_t / 2.0 ) * Speed2 );
- float Acceleration3 = - ( g / radius ) * TempTheta2;
- float Speed4 = RK4_State[sSpeed] + ( delta_t * Acceleration3 );
- float TempTheta3 = RK4_State[sTheta] + ( delta_t * Speed3 );
- float Acceleration4 = - ( g / radius ) * TempTheta3;
- RK4_State[sTheta] += (delta_t / 6.0) * (Speed1 + (2.0 * Speed2) + (2.0 * Speed3) + Speed4); // Actualizando la posición.
- RK4_State[sSpeed] += (delta_t / 6.0) * (Acceleration1 + (2.0 * Acceleration2) + (2.0 * Acceleration3) + Acceleration4); // Actualizando la velocidad.
- RK4_State[sTime] += delta_t; // Actualizando el tiempo.
- /////////////////////////////////////////////////////////////////////////
- }
- // The DrawState function assumes that the coordinate space is that of the
- // simulation - namely, meters, with the circle center placed at the origin.
- // There is currently a bug in processing.js which requires us to do the
- // pixels-per-meter scaling ourselves.
- void DrawState()
- {
- // Compute ball position
- /////////////////////////////////////////////////////////////////////////
- //Question 2.1.5 Your code here:
- // Euler-Cromer
- float BallX_EC = PixelsPerMeter * sin( EulerCromer_State[sTheta] ) * radius;
- float BallY_EC = PixelsPerMeter * cos( EulerCromer_State[sTheta] ) * radius;
- // RK2
- float BallX_RK2 = PixelsPerMeter * sin( RK2_State[sTheta] ) * radius;
- float BallY_RK2 = PixelsPerMeter * cos( RK2_State[sTheta] ) * radius;
- // RK4
- float BallX_RK4 = PixelsPerMeter * sin( RK4_State[sTheta] ) * radius;
- float BallY_RK4 = PixelsPerMeter * cos( RK4_State[sTheta] ) * radius;
- /////////////////////////////////////////////////////////////////////////
- // Draw ball, circles and text
- float ballDiameter = PixelsPerMeter * 0.3;
- // Draw ball EC
- translate( OriginPixelsX, OriginPixelsY );
- fill( 1.0, 0.0, 0.0 ); // Red
- ellipse( BallX_EC, BallY_EC, ballDiameter, ballDiameter );
- // Draw circle
- fill( 0 , 0 );
- ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
- textSize( 18 );
- fill(0);
- text( "Euler-Cromer", -56, 5 );
- // Draw ball RK2
- translate( 0, 210 );
- fill( 0.0, 1.0, 0.0 ); // Green
- ellipse( BallX_RK2, BallY_RK2, ballDiameter, ballDiameter );
- // Draw circle
- fill( 0 , 0 );
- ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
- fill(0);
- text( "RK 2", -20, 5 );
- // Draw ball RK4
- translate( 0, 210 );
- fill( 0.0, 0.0, 1.0 ); // Blue
- ellipse( BallX_RK4, BallY_RK4, ballDiameter, ballDiameter );
- // Draw circle
- fill( 0 , 0 );
- ellipse( 0, 0, 2 * radius * PixelsPerMeter, 2 * radius * PixelsPerMeter );
- fill(0);
- text( "RK 4", -20, 5 );
- }
- // The draw function creates a transformation matrix between pixel space
- // and simulation space, in meters, and then calls the DrawState function.
- // Unfortunately, there is currently a bug in processing.js with concatenated
- // transformation matrices, so we have to do the coordinate scaling ourselves
- // in the draw function.
- void draw()
- {
- //Time Step
- timeStepEC(deltaT);
- timeStepRK2(deltaT);
- timeStepRK4(deltaT);
- //Clear the display to a constant color
- background( 0.75 );
- // Draw the simulation
- DrawState();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement