Skip to content

Instantly share code, notes, and snippets.

Created March 19, 2015 01:07
What would you like to do?
/* Supplemental Problem for physics II
* Use to solve bringing four charges together in a parallelogram
* AND calculating the energy that is required.
* Created by: Austin Walters
# include <stdio.h>
# include <math.h>
# define ke 8.98755e9
/* Charge of units being brought in going from top left clockwise*/
double q_1;
double q_2;
double q_3;
double q_4;
double r; /* Short distance */
double R; /* Long distance */
double x; /* Value of first group */
double y; /* Value of second group*/
double z; /* Value of third group */
double U; /* Potential Energy */
printf("This program only works to calculate the energy required to group");
printf("four charges in a rectangular position, where the only unkown is U");
printf(", or the energy required to position the charges.\n");
printf("Enter the charge of q, please take only the single value of q.");
printf("Here is an example: if there is something such as 2q and q being q =");
printf("3.2e-5c, you only need to have 3.2e-5, adding in the C will cause a malfunction.");
printf("Please enter the top left number then rotate clockwise.\n\n");
printf("q1 (top left) > ");
scanf("%lf", &q_1);
printf("q2 (top right) > ");
scanf("%lf", &q_2);
printf("q3 (bottom right) > ");
scanf("%lf", &q_3);
printf("q4 (bottom left) > ");
scanf("%lf", &q_4);
printf("Please enter the top/bottom distance in meters. > ");
scanf("%lf", &R);
printf("Please enter the sides distance in meters. > ");
scanf("%lf", &r);
/* Solving for bringing q4 as if all were still */
x = (((ke*(q_1)*(q_4)) / (r)) + ((ke*(q_2)*(q_4))/(sqrt((pow(r, 2)) + (pow(R, 2)))) + (((ke*(q_3)*(q_4)) / (R)))));
/* Solving for bringing in q1 as if q2 and q3 were alone. */
y = ( ( (ke*(q_1)*(q_3)) / ( sqrt( (pow(r, 2)) + (pow(R, 2)) ) ) ) + (( ke*(q_2)*(q_1) ) / (R) ) );
/* Solving as if q2 was still and q3 was brought in. */
z = (((ke*(q_2)*(q_3)) / (r)));
/* Adding together to obtain the total energy. */
U = (x + y + z);
printf("The total energy required to bring the charges to the configuration");
printf(" is > %.9f Joules\n\n", U);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment