APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

How to use myFunction.evaluate post and replies

Return to the How to use myFunction.evaluate thread
Login to post to this thread

How to use myFunction.evaluate
Mirka Kouka
5 Posts


I have in my simulation 3 fields. The first one has the function (for example 4*sin(pi/8*x)) but it can change from the user. The second one has the x-value of the function which also could be defined by the user. The third one is not editable and should show the value of the function.

By the initialization page when I write fx = _view.functionField.evaluate({x:1}); I get the correct result for x=1 but I want to get a result for any x given by the user in the second field.

I have tried  fx = _view.functionField.evaluate({x:_view.xField}); (xField is the name of my 2nd field) but it doesn't work.

Can anyone help me make this working? Thank you!!!!

Replies to How to use myFunction.evaluate

Re: How to use myFunction.evaluate -
146 Posts

Where are you evaluating the function?  You should evaluate the function in the Fixed Relation page and assign the value into a global variable that is bound to a numeric field.  Here is an example for how to evaluate the function field.


Re: Re: How to use myFunction.evaluate -
Mirka Kouka
5 Posts

The example "One Dimensional Wave Superposition JS Model" didn't help me a lot because I want to have a field with the calculated value of the function. If you could see the files I attached maybe you can spot my mistake.

The SinusFunction file is my first approach but I don't know what to do by ft = _view.functionField.evaluate({t:1}); so as to calculate the value from my tField. The plot works ok here.

The SinusFunction2 file is what I did based on your suggestion. With the changes I made now the plot doesn't work properly when I change the values of tmin or tmax. So I think I made it worse...

Thank you for your time!

Attached File: Sinus.rar

Re: Re: Re: How to use myFunction.evaluate -
146 Posts

Mirka-  You are making this much too hard.  Code in a Fixed Relation page is evaluated whenever anything in the model changes.  All you need to do is to evaluate the function string in a Fixed Relation. Your global variable "ft" will then show the correct value in the output field.  You do not need to re-plot the entire graph using _initialization to show only a single value.  See the attached example.

When sending source files, use the build in "Zip the simulation source files" option when right-clicking on the package icon to package all the source files.  Do not use an external program, such as rar, to package the files.

EJS manuals provide additional information:


Attached File: ejss_src_EvaluateFunction.zip

Re: Re: Re: Re: How to use myFunction.evaluate -
Mirka Kouka
5 Posts

Wolfang, thank you very much for your help and for your time!

I thought it was much more complicated than this and I got lost trying to do this by the initialization.

Thank you for the advice for the "Zip the simulation source files" as well.

OSP Projects:
Open Source Physics - EJS Modeling
Physlet Physics
Physlet Quantum Physics
STP Book