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.

Parameters in COMSOL with MATLAB

Please login with a confirmed email address before reporting spam

Dear All, I used several parameters in COMSOL while creating a geometry. I imported this geometry into COMSOL with MATLAB. I need to use these parameters (initially created in COMSOL) in a loop at various places AS VARIABLES in COMSOL with MATLAB. Every time I need to use either function str2num before each parameter or need to remove inverted commas ( ' ' )for each and every parameter.

Example:

model.geom('geom1').feature('blk1').set('size', ['SampleLength' 'SampleWidth' 'SampleThickness']) will not work in a loop when using COMSOL server MATLAB WHILE IT IS THE DEFAULT ONE.

SampleLength, SampleWidth, SampleThickness are parameters and numerical value of these parameters is getting varied while in a loop.

Either model.geom('geom1').feature('blk1').set('size', [SampleLength 'SampleWidth SampleThickness]) OR

model.geom('geom1').feature('blk1').set('size', [str2num(SampleLength) str2num(SampleWidth) str2num(SampleThickness)]) will work in a loop when using COMSOL server MATLAB.

This exercise becomes tedious when there are lots of parameters.

Is there any other way out. Please kindly help me out.


Asha Sharma INDIA


1 Reply Last Post 7 nov 2018, 08:25 GMT-5
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 6 years ago 7 nov 2018, 08:25 GMT-5

Hi Sharma

I suggets that you define SampleLength, SampleWidth, SampleThickness as parameters in your model. E.g. the parameters should show up if you do this: mphgetexpressions(model.param)

Use these parameters throughout the model when setting up the model in the Comsol GUI. You can afterwards vary the parameter values both when using the Comsol GUI and when using Matlab.

You can use Matlab in this way to vary the parameters: sl = [1 2 4 8]; for i=1:length(sl) model.param.set('SampleLength', sl(i)); % solve model using % extract data end

The "magic" happens in the "model.param.set" line. It sends the numerical value of the sample length from Matlab to Comsol. Comsol will then use the value of the parameter SampleLength for its definitions and settings.

-------------------
Lars Gregersen
Comsol Denmark
Hi Sharma I suggets that you define SampleLength, SampleWidth, SampleThickness as parameters in your model. E.g. the parameters should show up if you do this: mphgetexpressions(model.param) Use these parameters throughout the model when setting up the model in the Comsol GUI. You can afterwards vary the parameter values both when using the Comsol GUI and when using Matlab. You can use Matlab in this way to vary the parameters: sl = [1 2 4 8]; for i=1:length(sl) model.param.set('SampleLength', sl(i)); % solve model using % extract data end The "magic" happens in the "model.param.set" line. It sends the numerical value of the sample length from Matlab to Comsol. Comsol will then use the value of the parameter SampleLength for its definitions and settings.

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.