/* Soluzione numerica del problema del punto materiale lanciato con un certo angolo */ #include #include #include int main(int argc, char *argv[]) { int step=0; float v0, theta, vx, vy, vxm, vym, ax, ay, dt; float vx0, vy0; // ci servono per la soluzione analitica float t=0, x = 0., y = 0.; const float grad2rad = acos(-1) / 180.; const float g = 9.8; // m/s^2 /* legge i parametri del lancio (v0 e theta e dt) da riga di comando ./lancio_2D 10 45 0.01 */ if (argc < 4) { puts("Devi dare anche velocità iniziale (m/s), angolo (gradi) e dt (s)"); return 0; } v0 = atof(argv[1]); theta = atof(argv[2]); dt = atof(argv[3]); printf("Parametri del lancio: v0 = %.2f m/s, theta = %.0f gradi\n", v0, theta); printf("Discretizzazione: dt = %f s\n", dt); vx = vx0 = v0 * cos(theta * grad2rad); // notare la doppia assegnazione vy = vy0 = v0 * sin(theta * grad2rad); printf("\nVelocità iniziale: (%.2f, %.2f) m/s\n", vx, vy); ax = 0.; // anche se è nulla (può servire per altri casi) ay = -g; printf(" t=%.3f s; x=%.2f m, y=%.2f m; vx=%.2f m/s, vy=%.2f m/s\n", t, x, y, vx, vy); printf(" x=%.2f m, y=%.2f m; vx=%.2f m/s, vy=%.2f m/s\n", vx0 * t, vy0*t + ay*t*t / 2, vx0, vy0 + ay*t); while (y >= 0) { vxm = vx + ax * dt/2; // velocita' media: (v + (v+a*dt))/2 vym = vy + ay * dt/2; vx += ax * dt; // velocita' dopo intervallo dt vy += ay * dt; x += vxm * dt; /* posizione dopo intervallo dt in cui il corpo si e' mosso con v media vm */ y += vym * dt; t += dt; // tempo dopo intervallo dt printf(" t=%.3f s; x=%.2f m, y=%.2f m; vx=%.2f m/s, vy=%.2f m/s\n", t, x, y, vx, vy); // aggiungiamo anche la soluzione analitica printf(" x=%.2f m, y=%.2f m; vx=%.2f m/s, vy=%.2f m/s\n", vx0 * t, vy0*t + ay*t*t / 2, vx0, vy0 + ay*t); } return 0; }