NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
September 20, 2018, 04:07:34 am
 Welcome, Guest. Please login or register.Did you miss your activation email? 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length

 Home Help Search Login Register
"In theory, theory and practice are the same. In practice, they are not." ..."Albert Einstein (1879~1955, Mathematical physicist, Nobel Prize 1921-Physics)"

 Pages: [1]   Go Down
 Author Topic: Displacement and Distance travelled logic in Easy java simulation  (Read 14043 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1787

http://weelookang.blogspot.com

 « Embed this message on: September 26, 2008, 11:52:02 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

Displacement and Distance traveled logic in Easy java simulation.

Displacement = Displacement - Displacement_initial; // easy in EJS

I was wondering how to do this elegantly?

Distance_travel = Math.abs ( Displacement) ?? does not look correct

Q1 :is there a

Distance_travel = Math.XXX ( Displacement) ?

or must i think of a  If ... else statement

if (Displacement < initial_Displacement) {
statements  ;

it is a bit more complicated that i thought

}
else if (expr) {
statements;
}
else {
statements;
}

Reference:
http://en.wikipedia.org/wiki/Java_syntax#If_..._else
 « Last Edit: September 26, 2008, 01:35:02 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #1 on: September 26, 2008, 05:39:34 pm » posted from:Taipei,T'ai-pei,Taiwan

Displacement is a vector. Distance is line integral of absolute value of displacement.

It is possible to have zero displacement (back to original point) but the distance traveled is not zero.
For 2D case if you wan to calculate distance traveled=d.
define coordinate x,y and xs,ys (xs=x,ys=y, initially).
set d=0 at initialize page.

Assume evolution page is
dx/dt=vx;
dy/dt=vy;

At constraint page add the following lines:
d+= Math.sqrt((x-xs)*(x-xs)+(y-ys)*(y-ys));
xs=x;
ys=y;

Then, d is the distance traveled in 2D plane.
 Logged
lookang
Hero Member

Offline

Posts: 1787

http://weelookang.blogspot.com

 « Embed this message Reply #2 on: September 26, 2008, 10:28:54 pm »

thanks for the tip! i  will implement after 12 OCT 2008 as i am going to USA and Canada for holiday.

i  have not used "+=" before

At constraint page add the following lines:
d+= Math.sqrt((x-xs)*(x-xs)+(y-ys)*(y-ys));  // what is += ??  is it a syntax for line integral of absolute value of displacement.
xs=x;
ys=y;

that is so cool!
 Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #3 on: September 26, 2008, 11:15:33 pm »

d+=a; is a the same as d=d+a;
d-=a; is the same as d=d-a;
d*=a; is the same as d=d*a;
d/=a; is the same as d=d/a;

d++; is the same as d=d+1;
d--; is the same as d=d-1;

But d++ is not quiet the sam as ++d;

if  d=3;
x=d++; is the same as x=d; d=d+1;
so x=3; after x=d++; is execute

x=++d; is the same as d=d+1; x=d;
so x=4; after x=d++; is execute

For the above two case: d=4; after x=d++; is execute

I have chinese version web page about basic syntax for java. Please check out Java 程式基礎說明資料
There are similar document in english available on the web. Google will help you find it.

Enjoy you holiday! :-)
 Logged
lookang
Hero Member

Offline

Posts: 1787

http://weelookang.blogspot.com

 « Embed this message Reply #4 on: October 14, 2008, 04:12:58 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

Hi prof!

I adapted the logic into the codes,

where d1 = distance traveled

x1s = x1 slider position
x1area = dummy variable for x1s counting of line integral

Codes:
d1 = d1 + Math.sqrt((x1s-x1area)*(x1s-x1area)); // same as +=
// d1 += Math.sqrt((x1-x1area)*(x1-x1area));
x1area=x1s; // x1area is just a way to do line integral
Codes:

The problem:
as I slide the time slider bar that controls ts = time slider variable.
The value of d1 is still counting as I slide time variable ts, which makes the value run continually which is not was i wanted.

I want to make it give the value for d1 = distance travel for time ts = 0 to ts = current position.

I tested these codes and they work fine in normal situation/simulation without the slider "ts".

Any tips how to trick the program into replay the value of d1 and ts ?

or do i need a different approach or strategy?

in case u need, the source code is attached
 « Last Edit: October 14, 2008, 04:43:47 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #5 on: October 15, 2008, 10:02:53 am » posted from:Taipei,T'ai-pei,Taiwan

It is seems that what you need is not a real time simulation. You just need to show how displacement and velocity changed with time for constant acceleration.
The formula is all well know:
x(t)=x(0)+v(0)*t+0.5*t*t;
v(t)=v(0)+a*t;

Use the above equestion directly will be the easiest way to implement.
You can show the x-t or v-t diagram directly.
 Logged
lookang
Hero Member

Offline

Posts: 1787

http://weelookang.blogspot.com

 « Embed this message Reply #6 on: October 15, 2008, 11:54:58 am » posted from:Singapore,,Singapore

i see what you mean.......

x(t)=x(0)+v(0)*t+0.5*t*t;
v(t)=v(0)+a*t;

is good and already implemented it this way.

codes:
x1 = x1s - x10;

but the distance traveled is another concept in secondary school as the Math.abs (integral of the area under the v-t graph) as distance traveled.

i am having difficulty implementing it elegantly (because there is positive area and Math.abs|negative| area).

Thanks!

so far, i think the best approach for me is by brute force aka not so elegant way Smiley( if statements )
codes:
if(v10>=0 && v1s>=0){
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}
if(v10<=0 && v1s<=0){
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}
if(v10>=0 && v1s<0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}
if(v10<=0 && v1s>0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}

I have do it!!!
 « Last Edit: October 15, 2008, 02:12:39 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #7 on: October 15, 2008, 04:37:23 pm » posted from:Taipei,T'ai-pei,Taiwan

The above code can be simplified as
if(v10>=0 && v1s<0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}else if(v10<=0 && v1s>0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}else{
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}

 Logged
 Pages: [1]   Go Up
"In theory, theory and practice are the same. In practice, they are not." ..."Albert Einstein (1879~1955, Mathematical physicist, Nobel Prize 1921-Physics)"
Jump to:

 Related Topics Subject Started by Replies Views Last post Easy java simulation (Download and related informations) Information and Download Fu-Kwun Hwang 20 92377 January 04, 2017, 07:13:01 pm by smily Offcial web site for Easy Java Simulation Information and Download Fu-Kwun Hwang 4 12845 October 12, 2015, 12:41:26 pm by dissertationwriting Research Papers on Easy Java Simulation Information and Download lookang 12 30355 July 31, 2018, 03:22:55 pm by craigstewart HCI Ed Tech 2010 Introduction to the basic use of Easy Java Simulations. part 1 EJS tutorial (How to create simulation with EJS) lookang 2 6808 April 05, 2010, 09:41:12 am by Fu-Kwun Hwang Stopwatch using Easy Java Simulation Information and Download dhasthagheer 7 26033 August 17, 2012, 09:32:22 pm by meng1004
Page created in 0.059 seconds with 23 queries.since 2011/06/15