Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Implementing functions into COMSOL

Please login with a confirmed email address before reporting spam

I've (very) recently started working with COMSOL with the aim of implementing "extern" formulas for certain equations into COMSOL using different models.
For that I need to know how to define any function in COMSOL, at all. I can't find any tutorials relating to that. Maybe someone could explain how to describe any function (for example a(x)=x^2+4). Eventually I'd like to have a formula looking somewhat like this:

Needed_value(x)=integrate(beta,x,x_a,x_b), where beta=f(E(x))

For now it would be enough for me being able to perform an equation like this:

needed_value(x)=integrate(E(x),x,x_a,x_b)).

As I use the AC/DC Module, E(x) actually should be given after calculation but I can't use ist (es.Ex?)
Does anyone know how to connect the AC/DC results with my equations in "Global ODEs and DAEs (ge)"

Thanks in advance

3 Replies Last Post 08.09.2015, 02:56 GMT-4
Andrea Ferrario COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 07.09.2015, 04:21 GMT-4
Hi Philipp,

You can define a function with an analytic expression (such as x^2+4) as an Analytic function under the Definitions node, and then use it anywhere in COMSOL as a function. For example, if you called your function f, you can use f(x) anywhere, where x is the argument to your function.

The integrate operator numerically integrates any function with respect to a variable:

integrate(E(x),x,x_a,x_b)

but this assumes that there is a function with name E defined e.g. as an analytic function. Note that x is a "dummy" integration variable, not the x-coordinate, so if you write

integrate(es.Ex,x,x_a,x_b)

you simply get es.Ex*(x_b-x_a) since es.Ex is a constant with respect to the dummy integration variable x. In this respect, it's better if you use another name for the dummy variable to avoid confusion.

If you want to integrate a space-dependent expression (such as es.Ex) on a line or a boundary, you need to use an Integration coupling operator (again under the Definitions node). For example, if you add an Integration operator called intop1 and select a line, the expression

intop1(es.Ex)

will integrate the x-component of the electric field on the line. You can also combine this with the Analytic function you defined earlier:

intop1(f(es.Ex))

--
Andrea Ferrario
COMSOL AB
Hi Philipp, You can define a function with an analytic expression (such as x^2+4) as an Analytic function under the Definitions node, and then use it anywhere in COMSOL as a function. For example, if you called your function f, you can use f(x) anywhere, where x is the argument to your function. The integrate operator numerically integrates any function with respect to a variable: integrate(E(x),x,x_a,x_b) but this assumes that there is a function with name E defined e.g. as an analytic function. Note that x is a "dummy" integration variable, not the x-coordinate, so if you write integrate(es.Ex,x,x_a,x_b) you simply get es.Ex*(x_b-x_a) since es.Ex is a constant with respect to the dummy integration variable x. In this respect, it's better if you use another name for the dummy variable to avoid confusion. If you want to integrate a space-dependent expression (such as es.Ex) on a line or a boundary, you need to use an Integration coupling operator (again under the Definitions node). For example, if you add an Integration operator called intop1 and select a line, the expression intop1(es.Ex) will integrate the x-component of the electric field on the line. You can also combine this with the Analytic function you defined earlier: intop1(f(es.Ex)) -- Andrea Ferrario COMSOL AB

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 07.09.2015, 05:54 GMT-4
Hi Andrea,

thanks for your helpful advice. I tried to do as you said, but it didn't work for some reason :(. Guess it's my bad.
I've attached a file with my problem. It's a simple capacitor and I wanted to (at first) plot the Integration of the electric potential). It just offers me one single point in the results. What did I do wrong? Maybe you could change it to a working solution and tell me about my mistakes.

Thanks a lot in advance,
Philipp.
Hi Andrea, thanks for your helpful advice. I tried to do as you said, but it didn't work for some reason :(. Guess it's my bad. I've attached a file with my problem. It's a simple capacitor and I wanted to (at first) plot the Integration of the electric potential). It just offers me one single point in the results. What did I do wrong? Maybe you could change it to a working solution and tell me about my mistakes. Thanks a lot in advance, Philipp.


Please login with a confirmed email address before reporting spam

Posted: 9 years ago 08.09.2015, 02:56 GMT-4
Try to define your functions in Global definition in analytical functions, above the component section.

Maybe it works, and then you can define your functions for different physics that you have.

Hope it helps.
Good luck
Try to define your functions in Global definition in analytical functions, above the component section. Maybe it works, and then you can define your functions for different physics that you have. Hope it helps. Good luck

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.