In this post the matlab code to build a model using COMSOL with matlab is analyzed from Koji Arai's codes on the calculation of TR noise in the 1D axisymmetric case. The post primarily describes the various commands used to interface with COMSOL.
It should be noted that the matlab script analyzed here is not the master script that will perform the simulation. The "main" file is called the "Main_thermo_refracive.m" found in the SVN repository TR noise_comsol
>The main script defines a structure called 'param' which stores all the parameters including the material properties and those related to running the simulation in comsol. This structure 'param' is hence passed on as arguments to all the other functions (in the SVN rep)
that compute the analytical solution or the COMSOL simulations.
>The part of the code ( the second if...end block) in "Main_thermo_refracive.m" that calls the COMSOL simulations, calls the function "thermo_refractive_COMSOL_1D_axisymmetric".
>In this function, the parameters are extracted from the structure 'param' and stored in separate variables. The time step in the simulation, the end time, mirror radius are defined and the function
that generates the model "thermo_refractive_COMSOL_1D_axisymmetric_model" is called which prepares the model in COMSOL.
> it should be noted that it is much of much ease to the user to define a simple model in COMSOL and export it in Matlab and then analyzed the same.
I had created a simple such file which a simple 3D metal bar is created and added some physics and study to see how the matlab file.
I have attached the same which also describes the relevant packages and subroutines in the comments. However, I had not erased the COMSOL history before the export hence it has significant amount of superfluous code. One may however have a look at the same

>The first thing done by the model builiding function is that it extracts a few parameters and stores them in separate variables in the first lines
f_mod = param.COMSOL.simulation_modulation_freq;
dt = param.COMSOL.simulation_time_step;
t_end = param.COMSOL.simulation_end_time;
>Since COMSOL accepts string arguments, the time steps are defined as a string
trange = ['range(0,' num2str(dt) ',' num2str(t_end) ')'];
**There is however a query regarding the keyword range which in Matlab returns the difference between the maximum and minimum of the list passed to it. range() passed to COMSOL would probably create an array fro 0  t_end in steps of dt.
The above line converts the quantity 'dt' and 't_end' to a string and concatenates it along with the rest of the string to make a valid string that would be understood by COMSOL.
>The COMSOL class is called next by the following two import statements (after a series of displays on the screen showing the status of the simulation).
>Managing models( like creation and destruction) is handled by the modelUtil class in COMSOL with Matlab and hence to create a model modelUtil.create() is called. "model.remove()" is used to destroy one such object. Line 29 says 
model = ModelUtil.create('Model');
>The above statement created an object called 'model' in Matlab and and a model in COMSOL by the name of 'Model'. The various attributes of this object is defined by the next few statements. Like,
[line 31]  model.name('thermo_refractive_COMSOL_1D_axisymmetric_model.mph');
The above statement assigns the filename of this COMSOL model created. Note that, since in Matlab, the Matlab object 'model' is used to invoke the functions i.e. 'model.name()' and not 'Model.name()'.
> The 'model.param' contains all functions related to setting and describing the parameters in the model.
*Note that the 'param' following keyword 'model' connected by the '.' has nothing to do with the 'param' structure used in the script "Main_thermo_refracive.m" which have been named anything else
> Now the model.param.set(<P>,<expr>) is used to give the parameter P an expression expr both being string.
The model.param.descr(<P>,<des>) has the same format but gives the description of the parameter P as des (something that is understandable in common terms).
This can be understood in the statements in [line 46  51]
model.param.set('beam_shape', '2/(pi*beam_size^2)*exp(2*r^2/beam_size^2)');
model.param.descr('beam_shape', '');
model.param.set('beam_intensity', 'beam_power*beam_shape');
model.param.descr('beam_intensity', '');
model.param.set('dT', 'mod1.TT_amb');
model.param.descr('dT', '');
An empty quote in the description implies that no description is given. The above statements define the parameters mentioned alongside them according to Heinerts paper.
> [line 5361]. the string 'var1' is used to tag all the global variables that are created. variables are expressions created out of the parameters.
Just like the above case, 'set()' is used to create variables and give them an expression, here as one can see 4 variables are created T_amb, beam_size, beam_power. and f_mod.
model.variable.create('var1');
model.variable('var1').set('T_amb', [num2str(param.material.temperature) '[K]']);
model.variable('var1').descr('T_amb', '');
model.variable('var1').set('beam_size', [num2str(param.beam.radius) '[m]']);
model.variable('var1').descr('beam_size', '');
model.variable('var1').set('beam_power', [num2str(param.COMSOL.beam_power) '[W]']);
model.variable('var1').descr('beam_power', '');
model.variable('var1').set('fmod', num2str(f_mod));
model.variable('var1').descr('fmod', '');
> The next section deals with the geometry. In this case the model is a 1D axisymmetric model. Hence one has the the digit 1 which specifies the dimension and axisymmetric is set to 'true'
model.geom.create('geom1', 1);
model.geom('geom1').axisymmetric(true);
> The run() function is to build the geometry
model.geom('geom1').run;
> The 'Interval' feature is present in the case of 1D models. The following line creates an interval feature called 'i1'
model.geom('geom1').feature.create('i1', 'Interval');
model.geom('geom1').feature('i1').set('p2', num2str(param.mirror.radius));
The above line is used to set the value of the right endpoint as the radius of the mirror. The string 'p2' is used to denote the right end point..
So this step creates a cylinder of radius equal to the radius of the mirror. In case of an infinite mirror an extra interval is added from the the radius to twice the radius.
It would help if more description on "interval" is left as comments and on why the extra interval was added and how does it make the calculation different.
> The interval is run using the 'run()' command.
> The material definition is added in the following section. Probably the data seems to have been taken from an external file rather than the COMSOL material library. Details of the file would be of help from the author.
The following lines cannot be understood. Comsol help on"propertyGroup" gives no results found
model.material('mat1').propertyGroup('def').set('heatcapacity', [num2str(param.material.specific_heat_per_volume) '[J/(kg*K)]']);
model.material('mat1').propertyGroup('def').set('density', [num2str(param.material.density) '[kg/m^3]']);
model.material('mat1').propertyGroup('def').set('thermalconductivity', [num2str(param.material.thermal_conductivity) '[W/(m*K)]']);
> The meshing is done on auto.
> As far as the Physics is concerned i.e. what kind of results we expect out of this geometry, the 'HeatTransfer' is selected.
A 1D heat source is created called 'hs1'
model.physics('ht').feature.create('hs1', 'HeatSource', 1);
and the Heat Source is time varying and given by beam_intensity*sin(2*fmod*pi*t). This can be seen in lines 121122
model.physics('ht').feature('hs1').selection.set([1]);
model.physics('ht').feature('hs1').set('Q', 1, 'beam_intensity*sin(2*fmod*pi*t)');
> If the mirror is infinite, something called 'InfiniteElements' is applied to the outer interval. More details on "infiniteElements" and why was it used would be helpful.
> A study involving the feature "Transient" was used. However, it is required to know what study was implemented from the GUI since the term "Transient" was not found under Study Steps in the GUI
model.study('std1').feature.create('time', 'Transient'); [line 134]
> The time step tweaking of COMSOL is stopped by the following line 166
model.sol('sol1').feature('t1').set('tstepsbdf', 'strict');
