Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Derivative of Interpolation Functions

Please login with a confirmed email address before reporting spam

Dear all,

I am trying to understand if, given an interpolation function (defined by a table of points in an external file like *.txt file) it is possible to define the derivative (spatial) of the interpolation function to insert in the weak formulation.

Let's suppose I have a convection term in my equation where the velocity of the convection is given by an interpolation function. When I compute the weak formulation, I'll have the spatial derivative of that function. Does Comsol recognize them if, when I define the weak formulation I have dx(interpolatedFunction) ?

Many thanks,

Paolo

15 Replies Last Post 27 ott 2014, 11:13 GMT-4
COMSOL Moderator

Hello Paolo Tricerri

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 mar 2013, 10:20 GMT-4
I would be interested in the answer as well...

- Simppa -
I would be interested in the answer as well... - Simppa -

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 mar 2013, 16:24 GMT-4
Hi

interesting question, I do not have any answer, support probably are the only ones to give any definitive answer here. By defining the interpolation function you can also define it's inverse, but no knobs for it's derivative.

If your function is imported and selected as a field of (x,y,z) you might get a results from d(func,x) but I haven't checked, if not you could add a specific math node to derive your function, it would not add too much complexity to your model ;)

--
Good luck
Ivar
Hi interesting question, I do not have any answer, support probably are the only ones to give any definitive answer here. By defining the interpolation function you can also define it's inverse, but no knobs for it's derivative. If your function is imported and selected as a field of (x,y,z) you might get a results from d(func,x) but I haven't checked, if not you could add a specific math node to derive your function, it would not add too much complexity to your model ;) -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 mar 2013, 17:56 GMT-4
I do it by using MATLAB function. Then COMSOL gives you the option to define the function as well as it's first derivatives. The interpolation itself is done in MATLAB.

But you got to have MATLAB Livelink for this...
I do it by using MATLAB function. Then COMSOL gives you the option to define the function as well as it's first derivatives. The interpolation itself is done in MATLAB. But you got to have MATLAB Livelink for this...

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 03:06 GMT-4
Hi!

I've opened a trouble ticket with the support.

www.comsol.fi/support/case/1307095/

I guess it will become public if it is solved.

- Simppa -
Hi! I've opened a trouble ticket with the support. http://www.comsol.fi/support/case/1307095/ I guess it will become public if it is solved. - Simppa -

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 03:09 GMT-4

I do it by using MATLAB function. Then COMSOL gives you the option to define the function as well as it's first derivatives. The interpolation itself is done in MATLAB.

But you got to have MATLAB Livelink for this...


How does that work out with cluster computing?

I've considered using a C/FORTRAN spline routine to do the interpolation. I guess the end result should be exactly same. Only starting a small library compared to whole MATLAB should be doable also for cluster computing...

- Simppa -
[QUOTE] I do it by using MATLAB function. Then COMSOL gives you the option to define the function as well as it's first derivatives. The interpolation itself is done in MATLAB. But you got to have MATLAB Livelink for this... [/QUOTE] How does that work out with cluster computing? I've considered using a C/FORTRAN spline routine to do the interpolation. I guess the end result should be exactly same. Only starting a small library compared to whole MATLAB should be doable also for cluster computing... - Simppa -

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 03:23 GMT-4
Hi

wha not use COMSOL and a ODE/PDE ?

--
Good luck
Ivar
Hi wha not use COMSOL and a ODE/PDE ? -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 03:31 GMT-4

Hi

wha not use COMSOL and a ODE/PDE ?

--
Good luck
Ivar


I appreciate your input, but could you be slightly more verbose, please?

Thanks.

- Simppa -
[QUOTE] Hi wha not use COMSOL and a ODE/PDE ? -- Good luck Ivar [/QUOTE] I appreciate your input, but could you be slightly more verbose, please? Thanks. - Simppa -

Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 05:13 GMT-4

I am trying to understand if, given an interpolation function (defined by a table of points in an external file like *.txt file) it is possible to define the derivative (spatial) of the interpolation function to insert in the weak formulation.


The derivate of an interpolation function is defined automatically. The second derivative is all-zero, though.

[QUOTE] I am trying to understand if, given an interpolation function (defined by a table of points in an external file like *.txt file) it is possible to define the derivative (spatial) of the interpolation function to insert in the weak formulation. [/QUOTE] The derivate of an interpolation function is defined automatically. The second derivative is all-zero, though.

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mar 2013, 13:05 GMT-4
Hi Gunnar

OK if yo what is its name ? standard function nam2 "x,y,z,or r,t" as suffix ?

--
Good luck
Ivar
Hi Gunnar OK if yo what is its name ? standard function nam2 "x,y,z,or r,t" as suffix ? -- Good luck Ivar

Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2 apr 2013, 02:29 GMT-4

OK if yo what is its name ? standard function nam2 "x,y,z,or r,t" as suffix ?


There is no variable defined for the derivative of a function, but you can use the d() operator: E.g., d(f(x,y), x). This works for all expressions; e.g., d(E, var) where E is an expression and var is a variable.



[QUOTE] OK if yo what is its name ? standard function nam2 "x,y,z,or r,t" as suffix ? [/QUOTE] There is no variable defined for the derivative of a function, but you can use the d() operator: E.g., d(f(x,y), x). This works for all expressions; e.g., d(E, var) where E is an expression and var is a variable.

Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2 apr 2013, 02:32 GMT-4

The derivate of an interpolation function is defined automatically. The second derivative is all-zero, though.


Correction: For an interpolation function of one variable with Interpolation set to "Piecewise cubic" or "Cubic spline", the second derivative is typically not zero.

[QUOTE] The derivate of an interpolation function is defined automatically. The second derivative is all-zero, though. [/QUOTE] Correction: For an interpolation function of one variable with Interpolation set to "Piecewise cubic" or "Cubic spline", the second derivative is typically not zero.

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2 apr 2013, 14:25 GMT-4
Hi

In fact just as for the degree of discretization of any dependent variable ...

--
Good luck
Ivar
Hi In fact just as for the degree of discretization of any dependent variable ... -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 24 ott 2014, 15:50 GMT-4
Hi,
I came up with a similar problem related to this topic. I use a linear interpolation function, from a txt file, to define a heat source P(x) in a equation. When I run the model in Comsol there is no problem, and Comsol automatically sets the derivative to zero, I see that when i plot d(P(x),x).
But if I save the same model as M-file and run it inside Matlab, it complains the following message "Failed to evaluate variable jacobian".
Is this a correct behavior because Comsol cannot set the derivative automatically, when run through Matlab?
If it's so, is there a way to set the variable jacobian to zero with API syntax?

Thank you very much

Franco
Hi, I came up with a similar problem related to this topic. I use a linear interpolation function, from a txt file, to define a heat source P(x) in a equation. When I run the model in Comsol there is no problem, and Comsol automatically sets the derivative to zero, I see that when i plot d(P(x),x). But if I save the same model as M-file and run it inside Matlab, it complains the following message "Failed to evaluate variable jacobian". Is this a correct behavior because Comsol cannot set the derivative automatically, when run through Matlab? If it's so, is there a way to set the variable jacobian to zero with API syntax? Thank you very much Franco

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 25 ott 2014, 04:19 GMT-4
Hi

When you define your function in COMSOL, the pre-processor can estimate the derivative and use it for its Jacobian calculations, but when you define it externally (without giving a function for it's derivative, COMSOL has no way before to estimate the range and the derivative of your function, for the solving process.

You could try the nojac() operator around your call to the Matlab function, but I'm not sure it's enough.

What's wrong with COMSOLs internal function, I must say since v4 has arrived I hardly use Matlab anymore, and certainly not when I have an internal GUI way to do the job

--
Good luck
Ivar
Hi When you define your function in COMSOL, the pre-processor can estimate the derivative and use it for its Jacobian calculations, but when you define it externally (without giving a function for it's derivative, COMSOL has no way before to estimate the range and the derivative of your function, for the solving process. You could try the nojac() operator around your call to the Matlab function, but I'm not sure it's enough. What's wrong with COMSOLs internal function, I must say since v4 has arrived I hardly use Matlab anymore, and certainly not when I have an internal GUI way to do the job -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 ott 2014, 11:13 GMT-4
Hi Ivar,

Thanks for the quick reply; the nojac() did work.

I do think COMSOL internal function is complete and exhaustive, and usually I prefer to stay in Comsol Desktop environment; it is just in this particular case that I'm forced to use an external library.

I load an initial space dependent heat term P(x), then i run the model to steady state and extract final density and electron temperature; I send these quantities to an external library (called through Matlab) that recalculates P(x), and so on till convergence of the iterative loop.

I manage also to solve my problem defining, within the Comsol M_script, external Matlab functions (that perform the interpolation) .

A Matlab addpath('\path') command has to be added to the M_script for this to work, otherwise Matlab will prompt that cannot find the functions, even if they are in the same folder.
Also a Comsol mph model that calls a matlab function, (establishing a livelink connection with matlab) has to be run and kept open in the same folder, otherwise Matlab will still prompt the same error message. I guess in this case there should be some Matlab-related PATH to be set with API syntax too in the Comsol M_script file.
I'm writing this in case someone else is caught in the same situation.

Thanks again

Franco




Hi Ivar, Thanks for the quick reply; the nojac() did work. I do think COMSOL internal function is complete and exhaustive, and usually I prefer to stay in Comsol Desktop environment; it is just in this particular case that I'm forced to use an external library. I load an initial space dependent heat term P(x), then i run the model to steady state and extract final density and electron temperature; I send these quantities to an external library (called through Matlab) that recalculates P(x), and so on till convergence of the iterative loop. I manage also to solve my problem defining, within the Comsol M_script, external Matlab functions (that perform the interpolation) . A Matlab addpath('\path') command has to be added to the M_script for this to work, otherwise Matlab will prompt that cannot find the functions, even if they are in the same folder. Also a Comsol mph model that calls a matlab function, (establishing a livelink connection with matlab) has to be run and kept open in the same folder, otherwise Matlab will still prompt the same error message. I guess in this case there should be some Matlab-related PATH to be set with API syntax too in the Comsol M_script file. I'm writing this in case someone else is caught in the same situation. Thanks again Franco

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.