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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
void Read_From_File(vector<double>&Time_Vector,vector<double>&Accel_X_Vector,vector<double>&Accel_Y_Vector)
{
double Time=0,Accel_X=0,Accel_Y=0,X_Position=0, Y_Position=0;
ifstream inFile("input3.txt");
if(inFile.fail())
{
cout << "error reading from infile" << endl;
}
while(inFile>>Time>>Accel_X>>Accel_Y>>X_Position>>Y_Position)
{
Time_Vector.push_back(Time/1000);
Accel_X_Vector.push_back(Accel_X);
Accel_Y_Vector.push_back(Accel_Y);
}
}
vector<double>Velocity_X_Vector_R_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
vector<double>X_Velocity_R_Rsum;
double Velocity_X_R_Rsum = 0;
double Sum =0;
X_Velocity_R_Rsum.push_back((Time_Vector[0]*Accel_X_Vector[0]));
for (int i=1; i<Time_Vector.size()-1; i++)
{
Velocity_X_R_Rsum=(Velocity_X_R_Rsum+Accel_X_Vector[i]*(Delta_T));
X_Velocity_R_Rsum.push_back(Velocity_X_R_Rsum);
}
Velocity_X_R_Rsum=(Velocity_X_R_Rsum+Accel_X_Vector[Time_Vector.size()-1]*(Time_Vector[Time_Vector.size()-1]-Time_Vector[Time_Vector.size()-2]));
X_Velocity_R_Rsum.push_back(Velocity_X_R_Rsum );
return X_Velocity_R_Rsum;
}
vector<double>Velocity_X_Vector_L_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
vector<double>X_Velocity_L_Rsum;
double Velocity_X_L_Rsum = 0;
X_Velocity_L_Rsum.push_back((Time_Vector[0]*Accel_X_Vector[0]));
for (int i=0; i<Time_Vector.size(); i++)
{
Velocity_X_L_Rsum=(Velocity_X_L_Rsum+(Accel_X_Vector[i]*(Delta_T)));
X_Velocity_L_Rsum.push_back(Velocity_X_L_Rsum);
}
return X_Velocity_L_Rsum;
}
vector<double>Trapzoidal_X(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
vector<double>X_Velocity_T_Rsum;
vector<double>X_Position_T_Rsum;
double T_Rsum_X_Velocity=0;
double T_Rsum_X_Position=0;
T_Rsum_X_Velocity = 0.5*((Velocity_X_Vector_L_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[0]+
Velocity_X_Vector_R_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[0]));
X_Velocity_T_Rsum.push_back(T_Rsum_X_Velocity);
for (int i =0; i<Accel_X_Vector.size(); i++)
{
T_Rsum_X_Velocity = 0.5*((Velocity_X_Vector_L_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[i]+
Velocity_X_Vector_R_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[i]));
X_Velocity_T_Rsum.push_back(T_Rsum_X_Velocity);
T_Rsum_X_Position=(T_Rsum_X_Position+((X_Velocity_T_Rsum[i]+X_Velocity_T_Rsum[i+1]))*0.5*Delta_T);
X_Position_T_Rsum.push_back(T_Rsum_X_Position);
}
return X_Position_T_Rsum;
}
vector<double>Velocity_Y_Vector_R_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
vector<double>Y_Velocity_R_Rsum;
double Velocity_Y_R_Rsum = 0;
Y_Velocity_R_Rsum.push_back(Time_Vector[0]*Accel_Y_Vector[0]);
for (int i=1; i<Time_Vector.size()-1; i++)
{
Velocity_Y_R_Rsum=(Velocity_Y_R_Rsum+(Accel_Y_Vector[i]*(Delta_T)));
Y_Velocity_R_Rsum.push_back(Velocity_Y_R_Rsum);
}
Velocity_Y_R_Rsum = Velocity_Y_R_Rsum + Accel_Y_Vector[Time_Vector.size()-1]*(Time_Vector[Time_Vector.size()-1]-Time_Vector[Time_Vector.size()-2]);
Y_Velocity_R_Rsum.push_back(Velocity_Y_R_Rsum);
return Y_Velocity_R_Rsum;
}
vector<double>Velocity_Y_Vector_L_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
vector<double>Y_Velocity_L_Rsum;
double Velocity_Y_L_Rsum = 0;
Y_Velocity_L_Rsum.push_back(Time_Vector[0]*Accel_Y_Vector[0]);
for (int i=0; i<Time_Vector.size(); i++)
{
Velocity_Y_L_Rsum= Velocity_Y_L_Rsum+Accel_Y_Vector[i]*(Delta_T);
Y_Velocity_L_Rsum.push_back(Velocity_Y_L_Rsum);
}
return Y_Velocity_L_Rsum;
}
vector<double>Trapzoidal_Y(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
vector<double>Y_Velocity_T_Rsum;
vector<double>Y_Position_T_Rsum;
double T_Rsum_Y_Velocity=0;
double T_Rsum_Y_Position=0;
T_Rsum_Y_Velocity = 0.5*((Velocity_Y_Vector_L_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[0]+
Velocity_Y_Vector_R_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[0]));
Y_Velocity_T_Rsum.push_back(T_Rsum_Y_Velocity);
for (int i =0; i<Accel_Y_Vector.size(); i++)
{
T_Rsum_Y_Velocity = 0.5*((Velocity_Y_Vector_L_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[i]+
Velocity_Y_Vector_R_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[i]));
Y_Velocity_T_Rsum.push_back(T_Rsum_Y_Velocity);
}
for (int i=0; i<Y_Velocity_T_Rsum.size()-1; i++)
{
T_Rsum_Y_Position=(T_Rsum_Y_Position+((Y_Velocity_T_Rsum[i]+Y_Velocity_T_Rsum[i+1]))*0.5*Delta_T);
Y_Position_T_Rsum.push_back(T_Rsum_Y_Position);
}
return Y_Position_T_Rsum;
}
int main()
{
vector<double>Time_Vector;
vector<double>Accel_X_Vector;
vector<double>Accel_Y_Vector;
Read_From_File(Time_Vector,Accel_X_Vector,Accel_Y_Vector);
const double Delta_T =(Time_Vector[Time_Vector.size()-1]-(Time_Vector[0])) / (Time_Vector.size()-1);
for ( int i=0; i<Time_Vector.size(); i++)
{
// cout<<Velocity_X_Vector_L_Rsum(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(15)
// <<Velocity_Y_Vector_L_Rsum(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<setw(15)
// <<Velocity_X_Vector_R_Rsum(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(15)
// <<Velocity_Y_Vector_R_Rsum(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<endl;
cout<<scientific;
cout<<Time_Vector[i]<<setw(20)<<Trapzoidal_X(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(20)<<
Trapzoidal_Y(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<endl;
}
}
|