Reputation: 23
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.
Upvotes: 0
Views: 307
Reputation: 71070
It looks like you're trying to incrementally update values but are using absolute time values:
data[i].Px = ((data[i-1].Px) + ((data[i].Vx) * i));
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:
data[i].Px = ((data[0].Px) + ((data[i].Vx) * i));
or incremental values:
data[i].Px = ((data[0].Px) + ((data[i].Vx) * t)); // where t is the time step
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:
s = ut + at2/2
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:
t = 0;
u = initial vector;
a = gravity;
do
{
t += time step;
s = u * t + a * t * t / 2;
} while (s.x >= 0);
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.
Upvotes: 1
Reputation: 35069
Your datapoint
variable is not initialized, it points somewhere in the data, which can be really dangerous. You should initialize it by allocating memory for your structs. I suggest you use the malloc
or calloc
function to do so. e.g:
datapoint *data = calloc(sizeof(datapoint), 20);
Now that you have allocated your array of structs, you should deallocate it in the end, after you are done reading/writing it:
free(data);
EDIT: I don't even think you are needing dynamic memory, it would be sufficient if you declare your array as follows:
datapoint data[20];
You do not have to free
it now. In fact it would be dangerous to do so.
Upvotes: 1
Reputation: 88711
From the code you've posted:
datapoint *data;
data[0].Vx = (20*cos(30));
You're using data uninitalised here. It could be pointing anywhere and this is undefined behaviour so anything could happen.
I think from reading your code you wanted to do something like:
data = malloc(sizeof(datapoint) * steps);
Upvotes: 2