--- /dev/null
+*.o
+newton
+*.plot
--- /dev/null
+# computational physics Makefile, rules for all example programs
+
+INCLUDEDIR = /usr/include
+CFLAGS = -O3 -Wall
+LIBS = -L/usr/lib -lm
+
+API = g_plot.o
+OBJS = newton
+
+all: $(OBJS)
+
+newton: $(API)
+ $(CC) $(CFLAGS) -o $@ $(API) $(LIBS) newton.c
+
+clean:
+ rm $(API) $(OBJS)
+
+remake: clean all
--- /dev/null
+/*
+ * g_plot.c - api for creating files viewable by gnuplot
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "g_plot.h"
+
+int gp_init(char *f_name) {
+ int fd;
+ if((fd=open(f_name,O_WRONLY|O_CREAT))<0)
+ printf("failed to open file: %s\n",f_name);
+ return fd;
+}
+
+int gp_add_data(int fd,void *data,int x_c,int y_c,int type) {
+ int x,y;
+ /* we need pointers */
+ for(y=0;y<y_c;y++) {
+ for(x=0;x<x_c;x++) {
+ if(type==TYPE_INT)
+ dprintf(fd,"%d ",*(((int *)data)+y*x_c+x));
+ if(type==TYPE_DOUBLE)
+ dprintf(fd,"%f ",*(((double *)data)+y*x_c+x));
+ }
+ dprintf(fd,"\n");
+ }
+ return 1;
+}
+
+int gp_close(int fd) {
+ return(close(fd));
+}
--- /dev/null
+/*
+ * g_plot.h - defines, prototypes
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#define MAX_FILE_OPENS 32
+#define MAX_CHAR_FNAME 64
+
+#define TYPE_INT 0
+#define TYPE_DOUBLE 1
+
+int gp_init(char *f_name);
+int gp_add_data(int fd,void *data,int x_c,int y_c,int type);
+int gp_close(int fd);
+
--- /dev/null
+/*
+ * 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;
+}