Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions Euler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Euler.h"

int euler_menu (float *k, float *m, float *position1, float *position2, float *dt, float *t){

int opcion;
do{
printf("Calculo de salto bungee con método de Euler.\n\n");
printf ("Bienvenido. Favor de ingresar los datos.\n");
printf("1. Ingresar la constante del Bongee (k): \n");
printf("2. Ingresar la masa de la persona (m): \n");
printf("3. Ingresar la posicion inicial (Xo): \n");
printf("4. Ingresar la diferencia de tiempo (dt): \n");
printf("5. Ingresar el tiempo a medir (t): \n");
printf("6. Calcular y salir. (Es necesario ingresar primero todos los datos)\n");
printf("7. Salir.\n");
printf("Opcion: ");
scanf ("%d", &opcion);

switch (opcion){
case 1:
printf("k = ");
scanf("%f",k);
break;
case 2:
printf("m = ");
scanf("%f",m);
break;
case 3:
printf("Xo = ");
scanf("%f",position1);
*position2 = *position1;
break;
case 4:
printf("dt = ");
scanf("%f",dt);
printf("Regresar? [S/*] ");
break;
case 5:
printf("t = ");
scanf("%f",t);
break;
case 7:
printf("Saliendo...\n");
exit(1);
break;
default:
if(opcion != 6)
printf("Opcion invalida.\n");
break;
}//switch

}while (opcion != 6);
return 0;
}

void euler_print (float position, float time)
{
printf("position: %f\t",position);
printf("time: %f\n",time);
}

float euler_form (float k, float m, const double g, float position1, float position2, float dt){
float position;
float p1 = (-1*position1)*(1+((k*(dt*dt))/m));
float p2 = 2 * position2;
float p3 = (dt*dt)*g;
position=p1+p2+p3;
return position;
}

int euler_list (float position, conditions **first, float time){
conditions *node, *aux;
node= (conditions*) malloc (sizeof (conditions));
if (node==NULL){
printf ("No se asigno el espacio en memoria.\n");
exit (0);
}
node->position=position;
node->time = time;
node->sig=NULL;

if (*first==NULL){
*first=node;
}
else{
aux = *first;
while (aux->sig != NULL)
aux = aux->sig;
aux->sig = node;
}
return 0;
}

int euler_free (conditions *first){
conditions *aux;
aux=first;
while (aux != NULL){
first=first->sig;
free (aux);
aux=first;
}
return 0;
}
38 changes: 38 additions & 0 deletions Euler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Euler.h
//
//
// Created by Team 3.
//

#ifndef Euler_h
#define Euler_h

#include <stdio.h>
#include <stdlib.h>

typedef struct euler {
float position;
float time;
struct euler *sig;
}conditions;

#ifdef Euler_IMPORT
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int euler_menu (float *k, float *m, float *position1, float *position2, float *dt, float *t);

EXTERN void euler_print (float position, float time);

EXTERN float euler_form (float k, float m, const double g, float position1, float position2, float dt);

EXTERN int euler_list (float position, conditions **first, float time);

EXTERN int euler_free (conditions *first);

#undef Euler_IMPORT
#undef EXTERN
#endif /* Euler_h */
37 changes: 37 additions & 0 deletions Files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>

#include "Euler.h"

FILE * file_new(char *name, char *mode){
FILE *fp;
fp = fopen (name, mode);
return fp;
}

void file_write(FILE * fp, conditions *first){
conditions *node;
node=first;
while (node != NULL){
fprintf(fp, "%f,",node->position);
fprintf(fp, "%f\n",node->time);
node = node->sig;
}
fclose (fp);
}

void gnuplot () {
int i;
char *labels[] = {"set title \"Movimiento\"",
"set ylabel \"Posicion(x)\"",
"set xlabel \"Tiempo(t)\"",
"set grid",
"set datafile separator','",
"set terminal png",
"set output 'Posicion VS Tiempo.png'",
"plot \"datos.csv\" with lines"};
FILE *VentanaGnuPlot = popen ("gnuplot -persist", "w");
for (i=0; i<8; i++)
fprintf(VentanaGnuPlot, "%s \n", labels[i]);
pclose (VentanaGnuPlot);
}
70 changes: 70 additions & 0 deletions Files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Files.h
//
//
// Created by Team 3.
//

#ifndef Files_h
#define Files_h

#include <stdio.h>
#include <stdlib.h>

#include "Euler.h"

#ifdef Files_IMPORT
#define EXTERN
#else
#define EXTERN extern
#endif


/* Files.h -- Function prototypes */

/**
* Instanciates a new file pointer identified via its name and mode.
* @param
* name (char *):
* Name of the file
* mode (char *):
* Mode of file to be opened {r, rb, a, ab, w, wb}
*
* @return FILE * opened_file.
*/
EXTERN FILE * file_new(char *name, char *mode);



/**
* Writes a dynamic list into a file.
* @param
* file (FILE *):
* Storage file
* first (conditions):
* The first node of the dynamic list
* aux (conditions):
* The auxiliary of the dynamic list
* node (conditions):
* The actual node of the dynamic list
*
* @return void.
*/
EXTERN void file_write(FILE * file, conditions *first);

/**
* Opens a gnuplot instance to graphicate the data from a file.
* @param
* name (char *):
* Name of the file
*
* @return void.
*/
EXTERN void gnuplot ();

#undef files_IMPORT
#undef EXTERN



#endif /* Files_h */
Binary file added Implementation
Binary file not shown.
80 changes: 80 additions & 0 deletions Implementation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Implementation.c
//
//
// Created by Team 3.
//
/**
* Our sample program.
* @copyright 2020 by TDA
* @license as you wish
* @author Team 3.
* @version 2020-09-30
* @file
*/


/* Include standard headers: */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/* Include modules header we directly invoke here: */
#include "Euler.h"
#include "Files.h"

/*Constants*/
const double g = 9.81;

/*Tentativo*/
//start=clock ();
//<stop = clock ();
//time = ((double)(stop-start))/CLOCKS_PER_SEC;
//printf("time: %f\n",time*1e9);
/*Fin de codigo tentativo*/

int main(int argc, char **argv)
{
/* Initialize modules: */
float k,m,position1,position2,position3,dt,time,t;

conditions *first = NULL;
//clock_t start,stop;
FILE *fp;

euler_menu (&k,&m,&position1,&position2,&dt,&t);

for (float i = 0; i <= t;) {

position3 = euler_form (k,m,g,position1,position2,dt);

if (i < (2*dt)) {
euler_print (position1, i);
euler_list (position1,&first,i);

euler_print (position2, i+dt);
euler_list (position2,&first,i+dt);

euler_print (position3, i+(2*dt));
euler_list (position3,&first,i+(2*dt));
i=i+(2*dt);
}
else{
euler_print (position3,i);
euler_list (position3,&first,i);
}
position1=position2;
position2=position3;
i=i+dt;
}

fp=file_new ("datos.csv","wt");
file_write (fp,first);
gnuplot ();

euler_free (first);


return 0;
}
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Implementation: Implementation.o Euler.o Files.o
gcc -o Implementation Implementation.o Euler.o Files.o

Implementation.o: Implementation.c
gcc -c Implementation.c

Euler.o: Euler.c
gcc -c Euler.c

Files.o: Files.c
gcc -c Files.c

clean:
rm *.o