+/*
+ * newton'sche bewegungsgleichung
+ *
+ *
+ * usage: ./newton <steps> <alpha> <x_p> <v_p>
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
+#include "g_plot.h"
+
+int main(int argc,char **argv) {
+ double x,x_p,v,v_p;
+ double alpha,force,tau;
+ int i,j,steps;
+ int fd; /* data file */
+ double *buf;
+ char filename[32];
+
+ if(argc!=5) {
+ printf("usage: %s <steps> <alpha> <x_0> <v_0>\n",argv[0]);
+ return -1;
+ }
+
+ /* init + starting conditions */
+ steps=atoi(argv[1]);
+ alpha=atof(argv[2]);
+ x_p=atof(argv[3]);
+ v_p=atof(argv[4]);
+ tau=2*M_PI/steps;
+ sprintf(filename,"newton_%d_%f_%f_%f.plot",steps,alpha,x_p,v_p);
+ fd=gp_init(filename);
+
+ /* allocate memory for data buffer */
+ if((buf=(double *)malloc(3*steps*sizeof(double)))==NULL) {
+ puts("malloc failed!");
+ return -1;
+ }
+ buf[0]=0; buf[1]=x_p; buf[2]=v_p;
+
+ /* loop */
+ for(i=0;i<steps-1;i++) {
+ force=-x_p-alpha*v_p;
+ x=x_p+v_p*tau;
+ v=v_p+force*tau; /* masse = 1 */
+ /* save */
+ j=3*i;
+ buf[3+j]=i*tau; buf[4+j]=x; buf[5+j]=v;
+ /* switch */
+ x_p=x; v_p=v;
+ }
+
+ /* write to file */
+ gp_add_data(fd,buf,3,steps,TYPE_DOUBLE);
+ gp_close(fd);
+
+ return 1;
+}