Science SPORE Prize
November 2011
The Open Source Physics Project is supported by NSF DUE0442581.

Lookup Table
post and replies
Return to the Lookup Table thread
Login to post to this thread
Lookup Table
 Oct 4, 2013 at 3:59AM

Rosen Mitrev
3 Posts

Hello! I want to know: 1. Is it possible to define in EJS relation between two variables as Lookup Table (as in Matlab)? 2. How to change a value of the constant when certain condition is met? For example:
IF time>5 THEN spring_constant=10 ELSE spring_constant=5
Where to find illustrating examples?
Thanks in advance
Rosen
Post edited October 4, 2013 at 4:00 AM EST.

Re: Lookup Table  Oct 04 2013 4:25AM

Francisco Esquembre
222 Posts

 1. Is it possible to define in EJS relation between two variables as Lookup Table (as in Matlab)?
Any relation among variables, such as the one for kinetic energy: K = 0.5*mass*v*v; must be written in a page of Fixed Relations in EJS. These pages are evaluated after each evolution step and after each interaction of the user.
 2. How to change a value of the constant when certain condition is met? For example:
If the model is based on an ODE, add an Event with Zero value function: return 5  time;
and action: spring_constant = 10;
If it is not based on an ODE (which I doubt), your if is fine. But make sure the step for t makes it reach t=5 more or less exactly.
Paco

Re: Re: Lookup Table  Oct 04 2013 11:32AM

Rosen Mitrev
3 Posts

Dr Esquembre, thank you for your attention!
I'd like to be more specific in my first question. For example, i have a relation between height h and air density rho in the following table form:
h rho 0 2.377 1000 2.208 2000 2.241 4000 2.117 .... ....
and so on. I 'd like to use this relation with differential equation of rocket motion with drag force.
Is there any way to put this table in EJS and make interpolation between these discrete values and use them in the differential equation?
Thanks
Rosen
Post edited October 4, 2013 at 11:36 AM EST.

Re: Re: Re: Lookup Table  Nov 03 2013 3:03PM

Francisco Esquembre
222 Posts

Ok. Sorry for the delay. The answer is basically YES. But with caution.
EJS comes with the latest Apache Commons Math library in it. To use it, you just create an duse the objects you need.
I have attached an example where I use your data to create by interpolation a function using Neville's algorithm. This is what Apache says about it:
"Implements the Neville's Algorithm for interpolation of real univariate functions. For reference, see Introduction to Numerical Analysis, ISBN 038795452X, chapter 2."
Once created, you can, in principle, use this function to anything (I use it to plot the function in a smooth way), including the rate part of an ODE.
The caveat is that, depending on your solver, the algorithm to solve the ODE requires the rate function to be "of class C^k". That is the rate function must be differentiable a number of times (k times). For instance, you need C^4 (or was it C^5?) for a RungeKutta algorithm to work fine.
I honestly don't know what differentiability you get from Neville's algorithm. But it could every well be a polynomial interpolation, in which case it is just fine.
Hope this helps.
Again, sorry for the delay.
Paco
PS: This was a nice question...
Attached File: ejs_InterpolationExample.zip

Re: Re: Re: Re: Lookup Table  Nov 26 2013 1:31PM

Rosen Mitrev
3 Posts

Paco, thank you very much for the answer. That was what i need!

