Use of number variables in equation interpreter



###Is there an easier way of writing equations containing number variables to construct equation-based structures?

Some of the structures in FDTD Solutions and MODE Solutions are defined by equations describing the boundaries of the object:

The equation interpreter in these structures takes a limited number of operatiors, pre-defined constants and functions together with numbers. If your equation contains number variables as shown below, you need to use ‘num2str’ to convert the number to string.

For a simple equation like the above, the corresponding string is relatively simple to write and easy to understand. However, more complex equations like below require much longer expression and become less intuitive and more prone to syntax error.

If there is a script that converts the following equation string (1) into a format that is compatible with the equation interpreter (2), the job of creating an equation string with number variables would become much easier.

So, here’s a script [ converteqn.lsf (1.5 KB) ] that will do exactly what has been mentioned. The way this script works is as follows:

  1. Save the converteqn.lsf in a current working directory or in a location that is always in the path such as
    • C:\Program Files\Lumerical\FDTD\scripts (Windows)
    • /usr/local/lumerical/scripts (Linux)
  2. Define the necessary number variables
  3. Define the equation in a compact form as in (1). Make sure the name of the equation string is ‘eqn’
  4. Execute the ‘converteqn’ script command. This command will look for a string variable ‘eqn’ and will convert the string (compact equation) into a complete equation, which is compatible with the equation interpreter.

For example, if you run the following script, you get eqn = (0.03sin(2pi/0.1x)+0.1) exp( - 1.8*x), which is the same as the lengthy equation using ‘num2str’ script.


eqn = “(Asin(2pi/Bx)+C) exp(-D*x)”; # specify the equation in a simple format


The result of this script can now be entered into the equation interpreter of the structure you want. For example, the following script will create an extruded ‘custom’ object defined by the equation above.

setnamed(“custom”,“equation 1”,eqn);
set(“x”,0);set(“x span”,1e-6);
set(“y”,0);set(“y span”,1e-6);
set(“z”,0);set(“z span”,0.3e-6);
set(“create 3D object by”,“extrusion”);

The key idea behind this script is to recognize some of the strings in the compact form (1) as number variables and convert the numbers back into strings using combinations of ‘num2str’ and ‘eval’ script commands.

Generally speaking, mixing up the data types (number and string) of identical variable names is not a good practice as this will result in confusion. However, in this specific case, the number variables (A, B, C, D) are not actually changed into any string variables or vice versa. The ‘converteqn’ uses a separate cell array to identity the ‘A, B, C, D’ string elements in (1) as numbers and the data types outside the converteqn.lsf script are not changed.



First of all, thanks for your instruction.
I am curious how to make the structure that I attached here.ask_question


To create only half of the symmetric structure, you need to enable the “make nonsymmetric” and set the “equation 2” to “0”. Here’s a modified script:


eqn = "(A*sin(2*pi/B*x)+C)*exp(-D*x)"; # specify the equation in a simple format

setnamed("custom","equation 1",eqn);
set("x",0);set("x span",1e-6);
set("y",0);set("y span",1e-6);
set("z",0);set("z span",0.3e-6);
set("create 3D object by","extrusion");
set("make nonsymmetric",true);
set("equation 2","0");