Question

In: Computer Science

Projectile motion with air resistance code in c language

Projectile motion with air resistance code in c language

Solutions

Expert Solution

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>


Related Solutions

How to Calculate the x ,y position of Horizontal Projectile motion with air resistance
How to Calculate the x ,y position of Horizontal Projectile motion with air resistance
In this assignment you will study the effects of air resistance in 2-D projectile motion. Note:...
In this assignment you will study the effects of air resistance in 2-D projectile motion. Note: Your values for numbers should always have units where appropriate and you need to show all steps in your calculations. You will: a) Evaluate position and velocity vectors for 2-D motion with and without air resistance. b) Derive the time of flight of a particle without air resistance. c) Derive the maximum range of a particle without air resistance. d) Compare the maximum ranges...
Use the model for projectile motion, assuming there is no air resistance and g=32 feet per...
Use the model for projectile motion, assuming there is no air resistance and g=32 feet per second per second. Determine the maximum height and range of a projectile fired at a height of 8 feet above the ground with an initial speed of 300 feet per second and at an angle of 45degrees above the horizontal. (Round your answers to three decimal places.) (a) maximum height (feet) (b) maximum range (feet)
For projectile motion on level ground assuming negligible air resistance (the initial angle being neither 0o...
For projectile motion on level ground assuming negligible air resistance (the initial angle being neither 0o nor 90o): Can the speed ever be the same as the initial speed at a time other than at t = 0 ? a. No b. Yes, at the end of the arch (end of trajectory) c. Yes, at its maximum height d. Yes, at half of its maximum height
In the absence of air resistance, a projectile that lands at the elevation from which it...
In the absence of air resistance, a projectile that lands at the elevation from which it was launched achieves maximum range when launched at a 45?angle. Suppose a projectile of mass m is launched with speed v0 into a headwind that exerts a constant, horizontal retarding force F? wind=?Fwindi^. By what percentage is the maximum range of a 0.900 kg ball reduced if Fwind =0.600 N ? Express your answer with the appropriate units. *Answer cannot be 7.25 %, 6.8%,...
In the absence of air resistance, a projectile that lands at the elevation from which it...
In the absence of air resistance, a projectile that lands at the elevation from which it was launched achieves maximum range when launched at a 45∘ angle. Suppose a projectile of mass m is launched with speed v0 into a headwind that exerts a constant, horizontal retarding force F⃗ wind =−Fwindi^. By what percentage is the maximum range of a 0.600 kg ball reduced if Fwind = 0.100 N ? Express your answer with the appropriate units.
a)derive the equation of motion of a bouncing ball dropped from rest (with air resistance )...
a)derive the equation of motion of a bouncing ball dropped from rest (with air resistance ) b) trajectory-- time it take to stop step by step please .
What is the horizontal component of acceleration of a projectile in projectile motion, AND what is...
What is the horizontal component of acceleration of a projectile in projectile motion, AND what is the horizontal component of velocity of a projectile in projectile motion? Please assume that there is no air resistance? Please explain your answer in a short paragraph and type in any calculations you would need to determine this. in a problem.
Fire the projectile launcher straight upwards (angle = 90o) at 18 m/s. (Air resistance box should...
Fire the projectile launcher straight upwards (angle = 90o) at 18 m/s. (Air resistance box should be unchecked, so that air resistance is OFF). Also, the height is at 0m. Using kinematics and showing your work below, determine: A) The time it should take the projectile to reach maximum height. (Hint: Use the kinematic equation v = v0 – gt) B) The maximum height reached by the projectile.
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT