Pendulum modeling in C

This is my code for trying a loop whileto simulate the swing of a pendulum, however the values ​​obtained do not help me:

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

main()
{
    float theta = 0;   // initial value for angle
    float omega = 0.2; // initial value for angular speed
    float time = 0;    // initial time
    float dt = 0.01;   // time step

    while (theta < 2 * PI && -2*PI) {
        time = time + dt;
        theta = theta + omega*dt;

        printf("theta=%i, omega=%i, time=%i, and dt=%i\n", theta, omega, time, dt);
    }
    system("PAUSE");
}

How can I change this to be more useful? The while condition that I should use is that it should stop when the pendulum makes one revelation forward or backward (-2PI or 2PI). I need to use the formula "omega = omega- (g / l) dtsin (theta)" and DO NOT assume that theta is approximately equal to sin (theta).

+4
source share
5 answers
  • while(theta <2*PI&&-2*PI)

    c, ,

    while((theta < 2 * PI) && (theta > -2 * PI))  
    
    PS .
+4

:

  • main()
    

    • int main()
    • int main(void)
    • int main(int argc, char *argv[])

    return 0;
    

    :

    #include <stdio.h>
    
    int main(void) {       
        /* Do stuff */        
        return 0;    
    }
    

    , .

  • LutzL, M_PI PI. #include <math.h>

    , :

    printf("\nValue of M_PI is %f\n\n", M_PI);
    

    .

  • rootkea:

    c, ,

    while((theta < 2 * PI) && (theta > -2 * PI))

  • ( , ), double float.

  • float "%i". , "%i" int undefined. "%f" float double signed decimal.

printf .

+4

, , . , :

        & ; = & theta; 0 cos (& omega; t)

& omega; = g/L, L - , g - .

thetaMax - , g - , L - , t - . , , , thetaMax .

Alrught, :

, :

KE = E
m*g*L*sin(thetaMax) = m*sqr(omega)*sqr(L)/2

:

m*g*L*thetaMax = m*sqr(omega)*sqr(L)/2
thetaMax = sqr(omega)*L/(2*g)

. , . :

#include <stdio.h>
#include <math.h>
#define PI 3.14159265
#define L 1 //length of the pendulum
#define g 9.8  //gravity const

int main()
{
    double theta = 0; // initial value for angle
    double omega = 0.2; // initial value for angular speed
    double time = 0; // initial time
    double dt = 0.01; // time step

    double thetaMax = omega*omega*L/(2*g);

    while (theta < thetaMax) {
        time = time + dt;
        theta = thetaMax * sin(omega*time);
        printf("theta=%f, omega=%f, time=%f, and thetaMax=%f\n", theta, omega, time, thetaMax);
     }
    return 0;
}

, theta while-loop .

+2

a (, )

θ '' + k · sin (θ) = 0

:

k = g/L

g L.

θ - , , , , , , -90 °. , theta .

() , /. ( ω=θ', (θ',ω')=(ω, -k·sin(θ))

for(...) {
    omega -= k*sin(theta) * dt;
    theta  += omega * dt;
    t += dt;
    printf(...);
}
+1

, , . , "" , , 1.

, , - . , , :

( ) , , , - , , ,

, , , , - : sin cos, , .. T.

, , , theta << 1 ( : sin(theta) = theta) theta, , :

enter image description here

( ), , :

enter image description here

2 ( ( ), ), .. , , , , :

enter image description here

,

theta , , , , pun, !

, , , . ​​ :

enter image description here

theta. , , :

enter image description here

b - , , .

,

, .

, , @Petr Stepanov, thetaMax, .

#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define E 2.71828      // Euler number
#define L 1            // length of the pendulum
#define g  9.80665     // gravity const

int main() {

    double theta = 0.0; // initial value for angle
    double omega = 0.2; // initial value for angular speed
    double time = 0.0;  // initial time
    double dt = 0.01;   // time step
    double b = 0.5;     // modified damping constant

    double thetaMax = omega * omega * L / (2 * g);

    while (theta < thetaMax) {
        time = time + dt;
        theta = thetaMax * ldexp(E, (-b) * time) * sin(omega * time);

        printf("Deflection angle=%f, Angular frequency=%f, Time=%f\n", theta, omega, time);
    }

    return 0;
}

1. An object with mass m, hanging on a fixed point with an inelastic “massless” thread with a length l, which allows it to swing freely in a gravitational field, determined by the constant of gravitational acceleration g.

2. It should be noted that: "all models are wrong, the practical question is how wrong they are so that they are not useful"

+1
source

Source: https://habr.com/ru/post/1624505/


All Articles