-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectile_sim.cpp
44 lines (38 loc) · 1.25 KB
/
projectile_sim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
const double g=9.81;
const double pi=(22.0/7.0);
int main(){
cout<<"Projectile Motion Simulator:"<<endl;
//Declaration of variables and input.
double v0=0,theta=0;
cout<<"Enter the inital velocity(m/s): ";
cin>>v0;
cout<<"Enter the angle of projection (degrees): ";
cin>>theta;
// Convert angle to radians
theta = theta*(pi/180.0);
//Calculate Total time of flight
double T= (2*v0*sin(theta))/g;
//Calculate Range
double R=(pow(v0,2)*sin(2*theta))/g;
//Calculate Max Height
double H=(pow((v0*sin(theta)),2))/(2*g);
ofstream outfile("projectile_data.txt");
outfile<<"Time\tX\tY\n";
for(double t=0;t<=T;t+=0.01){
double x=v0*t*cos(theta);
double y=(v0*sin(theta)*t)-(0.5*g*t*t);
outfile<<t<<"\t"<<x<<"\t"<<y<<"\n";
}
outfile.close();
cout<<"Total Time of Flight: "<<T<<" sec"<<endl;
cout<<"Maximum Height: "<<H<<" m"<<endl;
cout<<"Total Distance Travelled: "<<R<<" m"<<endl;
//Run Matlab script
//Adjust accordingly as the path location on your local computer.
system("/Applications/matlab/MATLAB_R2023b.app/bin/matlab -batch plot_projectile");
}