NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on June 10, 2008, 03:30:37 pm



Title: Newton cradle
Post by: lookang on June 10, 2008, 03:30:37 pm
any interest to create a newton's cradle for learning objective:

conservation of momentum

conservation of kinetic energy


The current well reference applet is this one by walter but it is still can be modified to a learner centered exploration simulation instead of a show and tell"see".

good idea?

reference:
http://www.walter-fendt.de/ph11e/ncradle.htm


found the source code in german from http://duepublico.uni-duisburg-essen.de/servlets/DerivateServlet/Derivate-81/Pendel5.java
Code:
// Applet zur Simulation von fünf Pendeln
// (Demonstration von Impuls- und Energieerhaltungssatz
// 03.11.1997 - 22.05.1998

import java.applet.*;
import java.awt.*;
import java.util.*;

public class Pendel5 extends Applet implements Runnable {

  int width, height;
  Graphics g1, g2;
  Image offscreen;
  Thread animatorThread;
  Choice ch;                         // Auswahlbox für Zahl der ausgelenkten Pendel
  final int n = 5;                   // Zahl der Pendel
  int k = 2;                         // Zahl der ausgelenkten Pendel
  final int r = 20;                  // Radius der Pendelkörper
  final int ax = 120, ay = 50;       // Aufhängung des linken Pendels
  final int l = 200;                 // Pendellänge
  final double A = 0.4;              // Amplitude (Bogenmaß)
  final double T = 2;                // Schwingungsdauer (s)
  final double omega = 2*Math.PI/T;  // Kreisfrequenz (1/s)
  double phi;                        // Phase (Bogenmaß)
  Date d;
  long t0 = 0, t1 = 0;               // Zeit (ms)
  double t = 0;                      // Zeit (s)
  int px, py;                        // Mittelpunkt der Pendelkörper

  public void init() {
    width = bounds().width; height = bounds().height;
    g1 = getGraphics();
    offscreen = createImage(width,height); g2 = offscreen.getGraphics();
    animatorThread = null;
    ch = new Choice();
    ch.addItem("  1  "); ch.addItem("  2  "); ch.addItem("  3  "); ch.addItem("  4  ");
    add(ch); ch.select(1);
    }

  public void drawPendulum (int i) {
    while (t >= T) t -= T;
    phi = A*Math.cos(omega*t);
    if (((t<0.25*T || t>=0.75*T) && i=0.25*T && t<0.75*T && i>=n-k)) {
      px = (int)(ax+i*2*r-l*Math.sin(phi)+0.5);
      py = (int)(ay+l*Math.cos(phi)+0.5);
      g2.setColor(Color.black); g2.drawLine(ax+i*2*r,ay,px,py);
      g2.setColor(Color.blue); g2.fillOval(px-r,py-r,2*r,2*r);
      }
    else {
      g2.setColor(Color.black); g2.drawLine(ax+i*2*r,ay,ax+i*2*r,ay+l);
      g2.setColor(Color.blue); g2.fillOval(ax+i*2*r-r,ay+l-r,2*r,2*r);
      }
    }

  public void paint (Graphics g) {
    int i;
    g.setColor(Color.yellow); g.fillRect(0,0,width,height);
    g.setColor(Color.black); g.fillRect(50,ay-2,300,4);
    for (i=0;i<5;i++) drawPendulum(i);
    }

  public boolean action (Event e, Object o) {
    if (e.target==ch) k = ch.getSelectedIndex()+1;
    paint(g2); g1.drawImage(offscreen,0,0,this);
    return true;
    }

  public void start () {
    if (animatorThread == null) {
      animatorThread = new Thread(this);
      animatorThread.start();
      }
    }

  public void stop () {
    if (animatorThread != null && animatorThread.isAlive())
      animatorThread.stop();
    animatorThread = null;
    }

  // Laufen des Programms (Endlosschleife):

  public void run () {
    d = new Date(); t0 = d.getTime();
    while (true) {
      paint(g2); g1.drawImage(offscreen,0,0,this);   
      d = new Date();
      t1 = d.getTime(); t += (t1-t0)/1000.0;
      t0 = t1;
      }
    }

  }


Title: Re: Newton cradle
Post by: lookang on June 10, 2008, 04:43:42 pm
is it possible to copy and paste the source code above into the EJS Model Custom and get the code to work?


It will be cool if it can be done !!??

anyway this is the code message

Generating simulation file
  D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java ...
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:93: illegal start of type
  import java.applet.*;  // > Custom.Lib Page:4
  ^
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:93: expected
  import java.applet.*;  // > Custom.Lib Page:4
                      ^
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:94: illegal start of type
  import java.awt.*;  // > Custom.Lib Page:5
  ^
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:94: expected
  import java.awt.*;  // > Custom.Lib Page:5
                   ^
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:95: illegal start of type
  import java.util.*;  // > Custom.Lib Page:6
  ^
D:\EasyJavaSimulation\Ejs3.47_080301\Ejs\Simulations\_apps\a.app\a.java:95: expected
  import java.util.*;  // > Custom.Lib Page:6
                    ^
6 errors
Compilation produced an error!


Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 10, 2008, 09:39:26 pm
The applet shown at http://www.walter-fendt.de/ph11e/ncradle.htm is not a real simulation.
It just use java to draw what you would expect.
You should be able to use EJS to do the same thing easily.
Try it. And I will help you.
And EJS can even create a real simulation, for example: what if the mass is different.

You can create a real simulation if you modify Simulations\BallsInBox.app\BallsInBox.xml


Title: Re: Newton cradle
Post by: lookang on June 10, 2008, 10:22:30 pm
ok i try :)



Title: Re: Newton cradle
Post by: lookang on June 11, 2008, 04:59:10 pm
File successfully read BallsInBox.xml
File saved successfully users/wee/BallsInBox.xml
Generating simulation file BallsInBox...
Compilation successful
Congratulations! The simulation was generated successfully.
Trying to run simulation BallsInBox...
Sorry! The simulation produced a run-time error.

strange after that i run again ok now............

Qn:  How to construct a string or rod constraint on the balls ?    

My thoughts:
Experiment 2 looks the best to modify from.



Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 11, 2008, 05:21:27 pm
If you use angle cta as independent variable, and write equation for cta, omega
For example:
 d(cta)/dt=omega;
 d(omega)/dt= -g*Math.sin(cta)/L;

x=xc+L*Math.sin(cta);
y=yc-L*Math.cos(cta);

Then you will have a pendulum.
This is one of the example I taught (if you have check out my flash tutorial about EJS).


Title: Re: Newton cradle
Post by: lookang on June 11, 2008, 10:45:24 pm
why is the
Code:
[flash=200,200][/flash]
not working ? :D

anyway i did a search but cannot find the swf tutorial on pendulum

example for search result using the word "flash"

http://www.phy.ntnu.edu.tw/demolab/swf/shm1.swf
http://www.phy.ntnu.edu.tw/ntnujava/swf/micrometer2.swf

i also can't do a listing of the directory so i can't find the tutorial you are refering too.

i think i can manage a simple task that you hinted.

i notice you like to use cta , omega..... in other xml.....i try

is the logic ( big picture like this)

1. make one pendulum think i can find a sample
2. make 5 pendulum by duplicating position side by side
3. add in event handlers for collisions found in BallsInBox.xml

4. how to add the control for number of balls to displace ?
5. how to add when to release the balls?

i doubt i can follow even if you try to explain.
i will consult you again when need . thanks!

One more qn:
x=xc+L*Math.sin(cta);
y=yc-L*Math.cos(cta);
This is one of the example I taught (if you have check out my flash tutorial about EJS).

is xc the change in x ? if it is, then i understand



Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 12, 2008, 04:52:23 pm
I just create an applet similar to the one at http://www.walter-fendt.de/ph11e/ncradle.htm





Title: Re: Newton cradle
Post by: lookang on June 12, 2008, 06:49:38 pm
you are amazing.

thanks for creating free interactive learning environments.

i see how you do it and learn, remix it to personalized learning for my students ;D


Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 12, 2008, 08:38:08 pm
But the above simulation did not really calculate the collision effect. It only use theoretical result to show expected result. I will try to create one which mass are not the same.
It is done. The following is the real simulation.
It is not quite the same as the above one, when all the mass are the same. (It is due to numerical error, but it is close to the real device).
When the simulation is paused: you can use slider to change the id (from 0-4) , then change the mass for the selected particle.
Press initialize to freeze all the particle, and move the first one with a small angle.


Title: Re: Newton cradle
Post by: lookang on June 12, 2008, 11:52:28 pm
yes i understand. it is a logic animation with simulation of the motion under gravity.

i was trying to find a way to show the energy

kineticenergy[1] = 0.5*mass[1]*L[1]*L[1]*omega[1]*omega[1]
and
potentialenergy[1] = mass[1]*g*L[1]*(1-Math.cos(cta[1])

and use the event handlers in BallsInBox.xml and the evolution for the balls.

it is a very interesting puzzle........ i need some time to think.

my thought strategy is use the BallsInBox.xml to build in the cta and omega because i can't use your due to my lack of code programming power.


anyway your newton cradle is now the most accurate java applet.

I thank you :)



Title: Re: Newton cradle
Post by: lookang on June 13, 2008, 12:01:30 am
your newtonscadlereal is now the most accurate simulation, just surpass your other simulation.

I will have to find time to read your code. :P again for personalizing it for my teaching objectives. 

Thanks!!


Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 13, 2008, 04:57:18 pm
You can use the code in "newtonscadlereal" to study how to process "event" in EJS.
you need to return negative for EJS know event occurs, and it can adjust the time step to find the exact time event really happened.  (Adjust time step so that the return value is minimum).



Title: Re: Newton cradle
Post by: lookang on June 16, 2008, 08:12:31 pm
i want to do this:

ketotal = kineticenergy[0]+kineticenergy[1]+kineticenergy[2]+kineticenergy[3]+kineticenergy[4];


but how to do it using the

Code:
for(int i=0;i
ketotal = ketotal+kineticenergy[i]++;

}

how to tell the program to reset the ketotal ?


Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 16, 2008, 08:45:02 pm
ketotal=0; // add this
for(int i=0;i<n;i++){
 ketotal=ketotal+kineticenergy;
}

Code:
If you want to show "<", you will have to type "& lt;". (without space in between)
If you want to show ">", you will have to type "& gt;".
Because you can use html code directly (while normal user can not), so the system will interprete "<" as part of html code.


Title: Re: Newton cradle
Post by: lookang on June 16, 2008, 10:02:29 pm
oic...........

Quote
ketotal=0; // add this

at the beginning of the loop statement....thanks!!

i managed to add the kinetic, potential n total energy


to do list:
1. i will try to add a simple way for the balls to be draggable, along the arc of the rotation.
2. i will then try to add a logic statement for dragging other balls.

this guy's flash is amazingly simple
http://www.flashkit.com/movies/Scripting/Physics/Newtons-Bryan_He-9109/index.php






Title: Re: Newton cradle
Post by: Fu-Kwun Hwang on June 17, 2008, 07:12:04 am
The flash version assume pendulum1._rotation = ball1*(Math.exp(-damping*to))*angle*Math.sin(fac*to);
That is why it is simpler.