Defining External MATLAB® Functions in a Model
Start by defining a MATLAB function in a model so that COMSOL Multiphysics can recognize it as an external function to be evaluated in the MATLAB engine.
1
2
On the Settings page, under the Functions section, enter conductivity in the Function name field, and enter x in the Arguments field.
By defining the function here, COMSOL knows that conductivity is a MATLAB function. COMSOL automatically starts a MATLAB engine to evaluate the function when necessary.
In case you did not save the function in a directory that is in the MATLAB path you need to save first the model MPH file.
3
You can display the value of the defined function by first defining the plot limit for the input arguments.
4
Expand the Plot parameters section. In the associated table enter 0 as the Lower limit and 1 as the Upper limit.
5
You can now define a second MATLAB function that returns the heat flux condition.
6
Repeating the above procedure, enter heatflux in the Function name field and enter x,y,x0,y0,Q0,scale in the Arguments field.
7
8
Before you continue with the model settings, you need to manually specify the function derivative with respect to all function arguments. If this is not done, the solver will return a warning message. For this model, the function arguments are not defined using the temperature (the dependent variable) and the numerical problem can be considered linear. For this reason, you can set the derivative to be 0 for all input arguments.
9
Note: For nonlinear problems, it is necessary to specify the partial derivatives. The Partial Derivative column can be set with an expression or another MATLAB function.