Help me progress in this code

Dear Friends,
I tried hard to complete this code but as this is just 1 week I have started programming I can not complete the code below. Would you please help me?
I have the equation acceleration (dx^2/dt^2=-10t) and I want to calculate the acceleration, velocity and position in a table by numerical method mentioned below from t=0 till t=1.
f(x+1)=dt/2*(f'(x+1)-f'(X))-f(x)



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

#define dt 0.1
#define time 10



//--------------------------------------------------------------------------
// to calculate d2u/dt2
void solve_a(double *acc, int time_step){

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];
}

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

//--------------------------------------------------------------------------
// create parameters
	double *acc, *u, *x;

for (time_step=0; time_step*dt<=time ;time_step++)    /* time loop */
	time=time_step*dt; cout<<"time_step="<<time_step<<"\t"<<"t="<<time<<"\t"<<"y[0]="<< y[0]<<"\n";
	   
		cout<<solve_a[];
		cout<< solve_u[];
		cout<<solve_x[];

	}


	return 0;

}
Last edited on
I think your for loop is off on line 37, and you're also using some variable incorrectly. It should be something like this:

1
2
3
4
5
6
for( int i = 0; i *dt <= 10; i++ )
   {
      time = i*dt;   //The actual time is integer multiples of your time-step

      //the rest of your code
    }


Your variable notation is confusing to me, and some things are mislabeled. I suggest:

1
2
double dt;        //This is the step-size in your time increment
double time;    //This is the actual time 


Along with these changes, you will have to slightly change up your functions:

 
u[time + dt] = (dt/2)*(acc[time + dt] + acc[time]) + u[time]

You are also missing an opening bracket on your for loop in int main().
Last edited on
I decreased the size of my code. 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

#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;

}
Topic archived. No new replies allowed.