In: Computer Science
CODE:
#include <math.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#define PI 3.14159
#define g 9.81
struct variables {
double l,m,v,vl,vm,alpha;
};
int choice();
double Accell(struct variables * vm,double A);
double Accelm(struct variables * vm,double A);
void RK2(struct variables * vm, double dt,double A);
void start(int totalTime,int dt,char *argv[],struct variables *
vm,double omega);
FILE * open_file ( char * path , const char * mode );
/***************************************************/
int main(int argc, char *argv[]) {
int i, numberOfSteps,n=0, option=0;
double totalTime, dt,omega=1, l1,l2,m1,m2,dl,A,l_mal;
struct variables lV;
FILE *fp;
system("clear");
if (argc != 7) {
fprintf(stderr,"\n usage: <l0> <m0> <v0>
<alpha> <totalTime> <dt> \n");
exit(1);
}
totalTime = atof(argv[5]);
dt = atof(argv[6]);
numberOfSteps = (int)(totalTime / dt + 0.5);
printf(" two-dimensional motion of a grave problem
encountered\n\n");
getchar();
/******************************************************/
printf("Choose the coefficient of friction:");
scanf("%lf",&A);
printf("#[Type 0] Analysis of the motion of the projectile by
choosing the angle of inclination\n");
printf("#[Type 1] Maximum range\n");
option=choice();
struct timeval begin, end;
long int time;
gettimeofday(&begin, 0);
if(A==0) fp=open_file("A=0.dat","w");
if(A==0.1)fp=open_file("A=0,1.dat","w");
if(A==1)fp=open_file("A=1.dat","w");
do{
if(option==1){
omega=omega + 0.25;
n++;
}else{
printf("choose the angle of inclination
(degrees)\n");
scanf("%lf",&omega);
}
start(totalTime,dt,argv,&lV,omega);
for (i = 0; i < numberOfSteps;
i++) {
l1=lV.l;
m1=lV.m;
RK2(&lV,dt,A);
l2=lV.l;
m2=lV.m;
dl=0.5*(l2-l1);
if(lV.m>=0) fprintf(fp,"%g %g %g %g %g %g\n",
(double)i*dt,lV.alpha, lV.l, lV.m,lV.vl,lV.vm);
if(m1*m2<0){
if(lV.l>l_mal){
l_mal=lV.l;
fprintf(fp,"%lf
%lf\n",(l_mal-dl),omega);
}
i=numberOfSteps;
}
}
}while(omega<90 && option==1);
fclose(fp);
system("sleep 2");
gettimeofday(&end, 0);
time = ((end.tv_sec - begin.tv_sec) * 1000000 + (end.tv_usec -
begin.tv_usec)) / 1000;
printf("time: %li microsec.\n", time);
exit(0);
}
/******************************************************/
FILE * open_file ( char * path , const char * mode ){
FILE * fa ;
if ( ( fa = fopen ( path , mode ) ) == NULL ){
fprintf(stderr,"\n Error opening %s\n" , path ) ;
exit ( EXIT_FAILURE ) ;
}
return fa ;
}
/*************************************************/
int choice(){
int option;
do{
printf("choose option\n");
scanf("%d",&option);
if(option<0 || option>1) printf("Error! Re-enter the
value\n");
}while(option<0 || option>1) ;
return option;
}
/************************************************/
double Accell(struct variables * vm,double A) {
return
-A*vm->vl*sqrt(vm->vl*vm->vl+vm->vm*vm->vm);
}
double Accelm(struct variables * vm,double A) {
return
-g-A*vm->vm*sqrt(vm->vl*vm->vl+vm->vm*vm->vm);
}
/***********************************************/
void start(int totalTime,int dt,char *argv[],struct variables *
vm,double omega){
vm->l = atof(argv[1]);
vm->m = atof(argv[2]);
vm->v= atof(argv[3]);
vm->alpha=0.5*PI*omega/90.;
vm->vl=vm->v*cos(vm->alpha);
vm->vm=vm->v*sin(vm->alpha);
}
void RK2(struct variables * vm, double dt,double A) {
struct variables tmp;
tmp.l = vm->l + 0.5 * vm->vl * dt;
tmp.m = vm->m + 0.5 * vm->vm * dt;
tmp.vl = vm->vl + 0.5 * Accell(vm,A) * dt;
tmp.vm = vm->vm + 0.5 * Accelm(vm,A) * dt;
vm->l += tmp.vl * dt;
vm->m += tmp.vm * dt;
vm->vl += Accell(&tmp,A) * dt;
vm->vm += Accelm(&tmp,A) * dt;
}
OUTPUT:
[H[2J[3Jusage: <l0> <m0> <v0> <alpha>
<totalTime> <dt>