Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comsol with matlab function

  1. Nov 24, 2008 #1

    Now I am working on the convection and diffusion problem with chemical reactions. The question is simple (but the answer is hard to me...) : I want to put a matlab function in the generation term, ex: the original one is " -k*c" , and I write a function r_c(x) which returns -k*x. In this case c is the dependent variable, and k is the reaction constant.

    It seems to me that COMSOL can't do the Jacobian assembly for m-function with dependent variable (don't know how to compute the derivatives of m-function).

    Does anyone have the same experience with the idea? Thanks for sharing.

  2. jcsd
  3. Nov 24, 2008 #2


    User Avatar
    Science Advisor
    Gold Member

    Yeah, if understood about right that's a problem haven't gotten around to myself (when using m - file - specified functions within the equation systems) directly but rather by specifying the derivatives manually (in the function definition). Often by specifying other m - file functions, where in complex cases compute the derivatives for example using numerical differentiation (in case analytical doesn't exist).
  4. Nov 24, 2008 #3
    Wow, thanks for your QUICK reply. I am so impressed.

    Do you have any idea that other software can do this? I've tried specified other m-file for the derivatives, but not succeed yet even with the simplest problem. woe... There is still a long way to go if I want to incorporate more reaction rate function into the model.

    Thank you PerennialII. It's so nice to meet you and this forum. I'll come more often and update my progress.

  5. Nov 24, 2008 #4


    User Avatar
    Science Advisor
    Gold Member

    And forgot to welcome you to PF, belated one :biggrin: !! I'd say Comsol is build a bit different from other FE packages (pretty much all of them), and it doesn't "increment" or formulate the eq-systems like other software usually do (and I think that's one of the underlying issues affecting this as well, most software wouldn't complain about this, but then again they would lack many of the properties of Comsol). I've usually solved it simply by computing the derivatives like m-file(x+delta)-m-file(x-delta)/2*delta for models which are (for example) nonlinear and can't really get an analytical one (setting delta appropriately). And have gotten it to work with some pretty "weird" models.

    Then again there are other tricks, developing things to series, adding stabilizing functions to aid differentiability etc., but those aren't really at the core of this problem.
  6. Nov 26, 2008 #5
    Hello, it's me again.
    First, thanks for your hints. They are very helpful. So far I've made a big progress, and I want to make some remarks for this case. Actually, only one comment: the format of input and output. I was wrong that I thought COMSOL sends concentration of each points while calling m-function. In fact COMSOL sends c in the form of vector [c1 c2 c3 ... cn], that is I have to rewrite my m-function to deal with that.
    After all things done, when I look back, it's kind of stupid stuck there for so long....

    The next step will be using the better way to differentiate the m-function instead of using small delta.
  7. Dec 11, 2008 #6


    User Avatar
    Science Advisor
    Gold Member

    Yeah, the m-files should be made able to handle any form of input. Often get not-so-obvious error messages when fail to follow this "rule". Was doing some analyses of a highly nonlinear material last week and after a while of trying got it working best with a simple central - difference approach in an m-file combined with various smoothing functions to get it to accept a whole range of input. Although still favor developing pretty much everything complex in the m-files to a series (locally in a point of evaluation) and then working with them :biggrin: .
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Comsol with matlab function
  1. Working in comsol (Replies: 1)

  2. COMSOL question (Replies: 1)

  3. Comsol help (Replies: 4)