lookang
«

Embed this message

on: November 26, 2008, 09:42:01 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

Can explain what this do? I can't make sense of it

Zero condition

if(vy>0) return 1; // particle is not falling

{what does return 1 mean?} which i found out later means do nothing return y; // displacement of particle above floor

{why return y?} which i found out later means check for y = 0 to action Action Check box End step at event

// action if particle is falling and below the floor

vy = Math.abs(vy)*coef_of_restitution;

{to return the value of Math.abs(vy)*coef_of_restitution to variable vy to simulate hitting n bouncing up like collision} ODE

{Ordinary Differential Equations} page is

dy/dt = vy

and

dvy/dt = - g

thx for reply!

« Last Edit: November 26, 2008, 10:32:45 am by lookang »
Logged

lookang
«

Embed this message

Reply #1 on: November 26, 2008, 10:29:49 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

I found this

http://www.um.es/fem/Download/Ejs/EjsManual_en_3.4_050914.pdf 2.5.4 Events of a differential equation Sometimes, when we are implementing the evolution of our simulation through the solution of a system of differential equations, we want the computer to detect that a given condition which depends on the variables of the system has taken place, allowing us then to make some corrections to adjust the simulation. For instance, suppose that the falling body that we are simulating using equations (2.4) is an elastic ball that we have thrown against the floor. The numerical solution of these equations doesn’t take into account, by itself, the fact that the computed solution will eventually take the ball to a position below ground, that is, y(t) < 0. As the method for numerical solution advances at constants step of time, it is very likely that the exact moment of the collision of the ball with the floor doesn’t coincide with any of the solution steps of the algorithm, taking the ball to a, let’s put it this way, ‘illegal state’. Instead of this, we would have preferred that the computer had detected the problem and had momentaneously stopped at the precise instant of the collision, applying then the code necessary to simulate the rebounding of the ball against the floor, and continuing the simulation from these new initial conditions. This is the archetypical example of what we call an event. More precisely, we define an event as the change of sign of a real-valued function of the state variables (the variables that we differentiate) and of the independent variable of an ODE. (Events caused only by the independent variable are traditionally called time events, while those caused by the other variables are called state events). To simplify the discussion that follows and, since actually all variables depend on the independent variable, we will denote by h(t) the function that changes sign in the event. an event is specified by providing: (a) the function h which depends on the state, (b) the desired tolerance, and (c) the action to invoke at the event instant. Creating events for an ODE Notice that the editor for differential equations includes a button labeled “Events” and a field that indicates that, by default, there are no events defined for this equation. Clicking the button “Events” will bring in an independent window with an editor with a behavior similar to that found in other parts of the model, and which we can use to create as many pages of events for our differential equation as we want. There exist, however, some differences. Observe in Figure 2.16 that a page for the edition of events appears divided into two sections (besides the text field for comments) In the upper section of this page we need to indicate how to detect the event. For this, we need to write the code that computes the function h(t) from the values of the state and the independent variable of our ODE. This code must end, necessarily, returning a value of type double. To help us remember this, the editor writes by default the next code (which, by the way, causes no event at all): return 1.0; In the “Tolerance” field on the upper right corner of this section we need to indicate the value for the tolerance that we want for this event (this is the in the discussion above). The lower section is used to tell the computer what to do when it detects (and then precisely locates) an event. Recall that this action must solve, or at least simplify, the situation that triggered the event. In the upper-right corner of this second section we find a checkbox labeled “Stop at event”, currently activated, that tells the computer whether it should return from the solution of the ODE at the instant of the event or not. Notice that, if checked, this causes the real increment of the independent variable to be smaller that the one originally desired. But still, checking this option may be useful if you want to appreciate the exact moment of the event. We can use our example of the falling ball to construct a sample event. For this, edit the code of the upper section of the page so that it reads: return y; This indicates that the event will take place when the ball reaches the level of the ground, y = 0. The default value for the tolerance is adequate. As action for the event, write the code: vy = -vy; which simulates a totally elastic rebounding at the instant the event takes place. Leave the box “Stop at event” checked, so that the system visualizes the instant of the rebounding.
« Last Edit: November 26, 2008, 10:35:33 am by lookang »
Logged

lookang
«

Embed this message

Reply #2 on: November 26, 2008, 11:01:35 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

i messed around the code to come up with this easy to understand codes

if (y<-0.5) return y; // change the value to check where to rebounce example y<1.0 else return 1; i finally understand my own codes

no worries! the original codes was confusing/misleading i think deeper meaning than i thought

« Last Edit: November 26, 2008, 02:40:05 pm by lookang »
Logged

Fu-Kwun Hwang
«

Embed this message

Reply #3 on: November 26, 2008, 01:46:41 pm » posted from:Taipei,T'ai-pei,Taiwan

The original code

if(vy>0) return 1; return y;

The above code is the same as

if(vy>0) return 1;

else return y;

if(vy>0) it means that particle is moving upward, so there is no bouncing need to be take care of.

otherwise, it will check if y is less than bouncing point y=0;

You change the code to

if(y<-0.5)return y; it mean that particle will bounced at y=-0.5 (not y=0).

Logged

lookang
«

Embed this message

Reply #4 on: November 26, 2008, 02:27:52 pm » posted from:Singapore,,Singapore

The original code The above code is the same as if(vy>0) return 1; else return y; if(vy>0) it means that particle is moving upward, so there is no bouncing need to be take care of. otherwise, it will check if y is less than bouncing point y=0;

Hi prof,

In the examination of the original code,

1. what must be done to include a position of y to rebounce that is to be determine by user instead of the default of y = zero?

Thx!

my codes doesn't work when y = 0.5 for example

if (y<0.5) return y; // change the value to check where to rebounce example y<1.0 else return 1; // do nothing

it still bounce at y = 0

strange

« Last Edit: November 26, 2008, 02:34:14 pm by lookang »
Logged

Fu-Kwun Hwang
«

Embed this message

Reply #5 on: November 26, 2008, 05:27:49 pm » posted from:Taipei,T'ai-pei,Taiwan

If you want to have particle bounced at y=y0; (You can add a slider for user to change y0,too. )

you should change the code to

if(vy>0)return 1;

return y-y0;

How it works in ejs:

If the return value is less than zero, EJS will know it is an event need to be processed.

And EJS will reduce time step to find the most precise point so that return value is almost equal to zero.

process the event, and move forward the time to it should be.

In your previous case,

if(y<0.5)return y;

you did not notice the error. Because the relative error is too small for your eye to notice it.

However, the error will accumulate.

If you change the range to y(maximum)=1 you will notice the error easily.

Logged

lookang
«

Embed this message

Reply #6 on: November 27, 2008, 07:42:23 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

if(vy>0)return 1; return y-y0;

Thanks! i go try it! so the syntax is "return y-y0".

cool!

Logged