Running Model in a Loop Using the MATLAB® Tools
Use MATLAB® tools such as for or while statements to run your model in a loop. The COMSOL API commands can be included in scripts using MATLAB commands. To evaluate such a script you need to have MATLAB connected to a COMSOL server.
To run a model in a loop you do not need to run the entire M-file’s commands from scratch. It is recommended to load a COMSOL model in MATLAB and run the loop only over the desired operations. The COMSOL model is automatically updated when running the study node.
You can run an M-file for a model from scratch if required, for example, to generate the geometry in loop.
When running loops in MATLAB, the iteration progress is taken care of by MATLAB; only the COMSOL commands are executed in the COMSOL server.
You can generate as many nested loops as needed and combine the loop with other MATLAB conditional statements such as if and switch or error handling statements such as try/catch. Or break the loop with break, or jump to the next loop iteration with continue.
See the MATLAB help for more information about the MATLAB commands for, while, if, switch, try/catch, break, and continue.
Geometry Parameterization
This example shows how to proceed to geometry parameterization using a MATLAB for loop. The model consists of the busbar example available in the COMSOL Multiphysics Applications Libraries; see the Introduction to COMSOL Multiphysics.
In this example the loop iterates over the busbar’s width, wbb. The solution for each parameter value is displayed using the second plot group defined in the COMSOL model. All the results are plotted in the same figure.
The results from the computation display in these plots:
Code for use with MATLAB®
model = mphopen('busbar');
w = [5e-2 10e-2 15e-2 20e-2];
for i = 1:4
model.param.set('wbb',w(i));
model.study('std1').run;
subplot(2,2,i);
mphplot(model,'pg4','rangenum',1);
end