Having problems with the output of this formula:
typedef struct
{
float t, Vx, Vy, Px, Py;
} datapoint;
datapoint *data;
data = malloc(steps * sizeof(datapoint));
data[0].Vx = (20*cos(30)); //30 is changed to radians
data[0].Vy = (20*sin(30));
data[0].Px = 0;
data[0].Py = 0;
steps=100;
i=1;
do
{
printf("Time: %.2f\t",i*q5);
// X
data[i].Vx = data[i-1].Vx ;//- CalculateDrag(data[i-1].Vx, q4);
data[i].Px = ((data[i-1].Px) + ((data[i].Vx) * i));
printf("X = %.2f\t",data[i].Px);
// Y
data[i].Vy= data[i-1].Vy - (9.81)*i; //- CalculateDrag(data[i-1].Vy,q4);
data[i].Py= data[i-1].Py + (data[i].Vy * i);
printf("Y = %.2f\t", data[i].Py);
printf("\n");
i++;
} while(((data[i].Py) >0) && (i<=steps));
The output should look like this:
Time 0.10s: X = 1.73m Y = 1.00m
Time 0.20s: X = 3.46m Y = 1.90m
Time 0.30s: X = 5.20m Y = 2.71m
....
....
Time 2.00s: X = 34.64m Y = 1.36m
Time 2.10s: X = 36.37m Y = 0.40m
Time 2.20s: X = 38.11m Y = -0.66m
Landed at X = 38.11 at time 2.20s
But instead it prints out other values. Tried what i can but think there is maybe faulty code with the formula.
It looks like you’re trying to incrementally update values but are using absolute time values:
since you’re using the previous step value to calculate the new value (LHS of
+) but adding on the current speed multiplied by the total time so far (RHS of+).You either need to use absolute values:
or incremental values:
This works OK with Px since there’s no acceleration component. The Py component has an acceleration component so doing the incremental version will introduce an error, since speed is not constant throughout the time step interval. You are approximating a curve with a series of linear sections.
I guess you’re trying to implement the following:
and trying to find the point where s.y is zero (hitting the ground). Rather than using an array and incrementally calculate s, use the above equation like this:
You would also benefit from using a vector library (not
std::vector, but a math vector), then you can write the equation just like in the code above and not calculate the x and y parts individually. It will also be easier to migrate to a 3D system.