/* program skraatkast 2 * * Range of a particle in a uniform gravitational field as a function * of angle. * Made to investigate what happens, when y_0 is not at ground level. * * Input: * x_0, y_0, v_0, g, theta_min, theta_max, theta_step (in degrees) * * Given theta (loop from theta_min to theta_max in theta_step steps) * Calculate t values for which y(t)=0 by solving a 2nd degree equation. * Calculate the corresponding values of x. * * Ouput: * theta, t_1, t_2, x_1(t_1),x_2(t_2) .. * .. * * A plot of x_1(theta) and x_2(theta) will show the optimal angle * and the maximal range. * * * Author: Peter Snoer Jensen * Date: 2019-02-18 */ #include #include #include int main(int argc, char *argv[]) { float x_0, y_0, v_0, theta_min, theta_max, theta_step, g; float A, B, C, D; float theta, theta_rad, t_1, t_2, x_1, x_2; if (argc != 8) { fprintf(stderr, "usage: %s x_0 y_0 v_0 g theta_min theta_max theta_step\n", argv[0]); exit(-1); } sscanf(argv[1], "%f", &x_0); sscanf(argv[2], "%f", &y_0); sscanf(argv[3], "%f", &v_0); sscanf(argv[4], "%f", &g); sscanf(argv[5], "%f", &theta_min); sscanf(argv[6], "%f", &theta_max); sscanf(argv[7], "%f", &theta_step); for (theta = theta_min; theta < theta_max; theta += theta_step) { theta_rad = theta * M_PI / 180.; A = -0.5 * g; B = v_0 * sin(theta_rad); C = y_0; D = B * B - 4. * A * C; if (D >= 0) { t_1 = (-B + sqrt(D)) / (2. * A); t_2 = (-B - sqrt(D)) / (2. * A); x_1 = v_0 * cos(theta_rad) * t_1 + x_0; x_2 = v_0 * cos(theta_rad) * t_2 + x_0; fprintf(stdout, "%f %f %f %f %f\n", theta, t_1, t_2, x_1, x_2); } else { fprintf(stderr, "D is less than zero at angle %f\n", theta); fprintf(stderr, "Starting below ground level with too little speed are we?\n"); } } return 0; }