Skip to content

Instantly share code, notes, and snippets.

@mwidjaja1
Last active September 29, 2015 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwidjaja1/10602446 to your computer and use it in GitHub Desktop.
Save mwidjaja1/10602446 to your computer and use it in GitHub Desktop.
I plotted a 10 meter long Foucault Pendulum (one that swings in both the x & y directions) with respect to the earth's rotation in Quebec Canada by using C and its standard libraries.
/*
Matthew Widjaja
Project 2: Pendulum Program
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
/* --- STEP #01: DOWNLOADS INPUT FILE --------------------------------
** This will open up 'input2.txt' which will contain the initial
** values. It'll have one value per line, listed in the order of
** PendulumLength (m), X(0), Y(0), U(0), V(0), & Initial Latitude.
** ---------------------------------------------------------------- */
// Variables needed to open & save input2.txt
float paramInput[6];
float value;
char comment[25];
char inBuffer[100];
int i = 1;
// Open Text File for Reading
FILE *in = fopen("input2.txt", "r");
// Checks to see if Input File is available
if (in == NULL) {
printf("Error! Could not open file\n");
exit(-1);
}
// Saves & Displays the File, Line-by-Line
for (i=1; i<7; i++) {
fgets(inBuffer, 100, in);
sscanf(inBuffer, "%s %f", comment, &value);
paramInput[i-1] = value;
printf("%s: %f\n", comment, paramInput[i-1]);
}
// Closes Input File & Opens Output File
fclose(in);
/* --- STEP #02: INITIAL PARAMETERS -------------------------------
** These are the initial variables that the model requires,
** and should not change between various pendulums.
** ------------------------------------------------------------- */
// Variables for each new X & Y Value
float testX, testY;
// Stepsize & Number of Trials Counter
int trialI = 0;
float stepsizeH = 0.5;
// Variables on Earth's Gravity & Velocity
float veloW = 0.0000729; // Velocity of Earth (1/sec)
float gravG = 9.83; // Acceleration of Gravity (m/sec^2)
float constK = gravG/paramInput[0];
// Converts Latitude Information
paramInput[5] = paramInput[5] / (180*3.14159);
// Saves Initial x, y, u, v, & latitude Parameters to a Variable
float x = paramInput[1];
float y = paramInput[2];
float u = paramInput[3];
float v = paramInput[4];
float latM = paramInput[5];
// Opens Output File & Creates Buffer
char outBuffer[100];
FILE *out = fopen("output2.txt", "w");
fprintf(out, "X-Value \t Y-Value \n");
/* --- STEP #03: PENDULUM MODEL -----------------------------------
** This section runs the diffEqus to run the Pendulum Model,
** and tests if the pendulum shifted by at least 45 degrees.
** This will also save the outputs to a .txt data file.
** ------------------------------------------------------------- */
while (trialI < 100)
{
// Solves the four equations needed to do the pendulum
u = u + (stepsizeH * (2 * veloW * v * sin(latM) - constK * x));
v = v + (stepsizeH * (-2 * veloW * u * sin(latM) - constK * y));
testX = x + (stepsizeH * u);
testY = y + (stepsizeH * v);
trialI++;
// Reports Results
printf("On Trial %d\n",trialI);
printf("U = %f, V = %f, X = %f, Y = %f\n\n",u,v,testX,testY);
fputs(outBuffer, out);
fprintf(out, "%f \t %f \n", testX, testY);
// Reports Distance of New Line
if (testY/testX == 1) {
printf("At Trial %i, the pendulum reached 45 degrees\n", trialI);
exit(-1);
}
// If model still continues, we re-assign x & y and continue
x = testX;
y = testY;
}
// Closes Output File
fclose(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment