Please help me about my mistakes?

I want to progress step by step. Would you please guide me about my code below? I want to get acc=-10*t values between t=0 and t=1 with step size 0.001

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
  #include <iostream>
#include <cmath>
using namespace std;

#define dt 0.001      /* time steps*/
#define time 1   /* end time */

//--------------------------------------------------------------------------
// to calculate d2x/dt2=acceleration=-10*t
void a(double acc[], int t){

acc[t]=-10*t;
}

//--------------------------------------------------------------------------
// main program
int main(){

//--------------------------------------------------------------------------
// create parameters
	double acc[];
	double t;

	for (t=0; t*dt<=time; t+=dt)                     /* time loop */
	{
	   cout<<"t="<<t<<"\t"<<"t+dt"<<t+dt<<"\t"<<"acc[t+dt]="<< acc[t]<<"\n";

	}


	return 0;

}
Does it need to be stored in acc array ?

And I am sorry to say but you should start moving to const instead of #define if you are coding in C++.

Edit:
wait,, you should never use a floating point as an index if you don't really know the consequences. You should probably use integer instead and converts them to double when you need to use the data.

I will guide you after all my understanding of what you want to do is confirmed.
Last edited on
Dear Friend,
Thanks a lot for your comments. I do not know if I should store the data in acc array or not. But I need those data to be used in other function and calculate the velocity but I postponed it to after this step as I want to learn this gradually.

Totally in this step I want to get acceleration data with its function between time 0 and 1 and time step 0.001 and show it in a table.

Bu later I want to use the acceleration data to calculate the velocity and position by using numerical method below:

f(x+1)=dt/2*(f'(x+1)-f'(X))-f(x)

I mean something like this:

acc[time_step]=-10*time_step;
}
//--------------------------------------------------------------------------
// to integrate equation du/dt=a

void solve_u(double *u, double *acc, double dt, int time_step){

u[time_step+1]=(dt/2)*(acc[time_step+1] + acc[time_step]) + u[time_step];
}
//--------------------------------------------------------------------------
// to integrate equation dx/dt=u
void solve_x(double *x, double *u, double dt, int time_step){
x[time_step+1]=(dt/2)*(u[time_step+1] + u[time_step]) + x[time_step];
change every time to millisecond and store it in an int,, I suggest if you want to array that way
but I think it's actually more reasonable to just use function and not store it in an array.
I don't know what you are using this for but suit your needs

In 2D Physics engine it's usually something like this
1
2
3
4
5
6
7
8
9
10
11
12
13

float a = -10.0;
float v = 10.0;
float y = 10.0;
const float timestep = 0.001;
const float start = 0;
const float end = 1;

for( float i = 0; i < end; i += timestep ){
     v += a*dt;
     y += v*dt;
}


I know that this is by no means a perfect calculation but it's usually negligible in computer graphics because there is no almost difference between 100px and 101px

Hope it answers though
Thanks for your comment. I edited this code but still I have problem with outpuy. I do not know why it starts from t=69.09 instead of t=0

#include <iostream>

using namespace std;


//--------------------------------------------------------------------------
//to calculate d2x/dt2=acceleration=-10*t
double acc(double a) {

return (-10*a);
}

//--------------------------------------------------------------------------
// main program
int main(){

//--------------------------------------------------------------------------
// create parameters
double t;
const double dt=0.01;
for (t=0; t*dt<=1; t+=dt) /* time loop */
{

cout<<"t="<<t<<"\t"<<"t+dt="<<t+dt<<"\t"<<"acc="<< acc(t+dt)<<"\n";

}


return 0;

}


Sorry for the late reply, I compiled it and it starts with 0 in my compiler
Topic archived. No new replies allowed.