Exercise 2.19 d9 on page 40 of Introd Comp Sim Meth

Martin Jola
7 Posts

I'm not sure wether I understand this part of the exercise. In BouncingBall class I added a new methode for kinetic energy (I initialized the variables) public void calc(){ energy = ((vx*vx)+(vy*vy)/2);

and in the App Class in doStep for (int i=0;iball[i].calc(); totalenergy +=ball[i].energy ; } control.println("totalenergy = " +totalenergy);

It works well, however the results seem to be strange When I plot the result I get an up and down curve for each ball. With large number of balls the plot is almost completely full. When I add the potential energy as well (y*g) I get a streigth line upwartds. Can you comment my code and the result? Did I missunderstand the exercies? So far all exercises in Chap 2 have not been a problem. regards

Re: Exercise 2.19 d9 on page 40 of Introd Comp Sim Meth -

Wolfgang
191 Posts

Martin: I am not sure what you are doing but it appears to me that your kinetic energy calculation is missing a factor of 0.5. Also, are you setting the total energy variable equal to zero before you enter the loop?

Re: Re: Exercise 2.19 d9 on page 40 of Introd Comp Sim Meth -

Martin Jola
7 Posts

Hello Wolfgang

many thanks for your comments. You are right, I forgot to divide by 2 in my question, but I had included it in the source code, sorry for this.

I attached the two source codes and I'm just wondering whether my solution for the exercise is correct or not. If not what is the correct solution?

For me, the result does not make any physical sense. I'm not sure about the follwing: a) should I use the absolute values of vx/vy? I think yes b) are the velocities vx/vy set to random values at each step or are they constant over all steps? I believe they are constant over all time steps, they are set randomly only the first time. c) the kinetic energy does not make sense because collision with other balls are not taken into account in the simulation.

Re: Re: Re: Exercise 2.19 d9 on page 40 of Introd Comp Sim Meth -

Wolfgang
191 Posts

The formula for kinetic energy is incorrect. Your code has: ((vx*vx)+(vy*vy)/2);

The correct expression for the kinetic energy is: (vx*vx+vy*vy)/2;

Also note that the Euler method has a large numerical error and your simulation will show an increase in energy unless the time step is very very small.