Common errors in Coursework 1

"Not too far off………"

"Pretty close to…….."

"Almost exactly……."

You must quote your numerical result with an estimate of the error. For example, if the time step in your numerical solution is D t then - at the very best - you can only quote values accurate to ± (D t/2).

float energy (float, float, float, float);

and then passed F0, m, x[t] and v[t] to the function. F0 and m were used in at least two functions and thus it is inefficient to keep passing these variables to the function. Set them as globals or, in this case, better still #define F0 and m as constants.

if (x[t]<0)

{

v[t+1]=v[t]+(F0/m)*time_step;

x[t+1]=x[t]+v[t]*time_step;

}

if (x[t]>0)

{

v[t+1]=v[t]-(F0/m)*time_step;

x[t+1]=x[t]+v[t]*time_step;

}

 

This is messy. You should rewrite the code as follows:

if (x[t]<0)

v[t+1]=v[t]-(F0/m)*time_step;

else

v[t+1]=v[t]+(F0/m)*time_step;

x[t+1]=x[t]+v[t]*time_step;

 

 

Back