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
Posted 7 set 2012, 04:33 GMT-4 Modeling Tools & Definitions, Parameters, Variables, & Functions Version 4.3 15 Replies
Please login with a confirmed email address before reporting spam
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
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
- Simppa -
Please login with a confirmed email address before reporting spam
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
But you got to have MATLAB Livelink for this...
Please login with a confirmed email address before reporting spam
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 -
Please login with a confirmed email address before reporting spam
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 -
Please login with a confirmed email address before reporting spam
wha not use COMSOL and a ODE/PDE ?
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
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 -
Please login with a confirmed email address before reporting spam
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.
Please login with a confirmed email address before reporting spam
OK if yo what is its name ? standard function nam2 "x,y,z,or r,t" as suffix ?
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
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.
Please login with a confirmed email address before reporting spam
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.
Please login with a confirmed email address before reporting spam
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
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
Please login with a confirmed email address before reporting spam
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
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.