added integral-* to Makefile
[physik/computational_physics.git] / homogen.c
1 /*
2  * homogen.c - bewegung im homogenen feld f=g(0,-1)
3  *
4  * usage: ./homogen <x_0> <y_0> <vx_0> <vy_o> <steps> <g> <alpha>
5  *
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 #include <stdlib.h>
12 #include "g_plot.h"
13
14 int main(int argc,char **argv) {
15  double x,x_p,y,y_p,vx,vx_p,vy,vy_p;
16  double g,alpha,f_x,f_y,tau;
17  int i,steps;
18  int fd; /* data file */
19  double buf[5];
20  char filename[64];
21
22  if(argc!=8) {
23   printf("usage: %s <x_0> <y_0> <vx_0> <vy_0> <steps> <g> <alpha>\n",argv[0]);
24   return -1;
25  }
26
27  /* init + starting conditions */
28  x_p=atof(argv[1]); y_p=atof(argv[2]);
29  vx_p=atof(argv[3]); vy_p=atof(argv[4]);
30  steps=atoi(argv[5]); g=atof(argv[6]); alpha=atof(argv[7]);
31  tau=2*M_PI/steps;
32  sprintf(filename,"homogen_%f_%f_%f_%f_%d_%f_%f.plot",x_p,y_p,vx_p,vy_p,steps,g,alpha);
33  fd=gp_init(filename);
34
35  buf[0]=0;
36  buf[1]=x_p; buf[2]=y_p;
37  buf[3]=vx_p; buf[4]=vy_p;
38  gp_add_data(fd,buf,5,1,TYPE_DOUBLE);
39
40  /* loop */
41  for(i=0;;i++) {
42   f_x=-alpha*vx_p; f_y=-g-alpha*vy_p;
43   x=x_p+vx_p*tau; y=y_p+vy_p*tau;
44   vx=vx_p+f_x*tau; vy=vy_p+f_y*tau;
45   /* save data */
46   buf[0]=i*tau;
47   buf[1]=x; buf[2]=y;
48   buf[3]=vx; buf[4]=vy;
49   gp_add_data(fd,buf,5,1,TYPE_DOUBLE);
50   /* switch */
51   x_p=x; y_p=y;
52   vx_p=vx; vy_p=vy;
53   /* stop at y~0 ! */
54   if((y<=0)&&(i!=0)) break;
55  }
56
57  printf("reached ground at step %d\n",i);
58
59  /* close file */
60  gp_close(fd);
61
62  return 1;
63 }