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.

Updating Parameters in MATLAB COMSOL Using Loops Giving Different Results than in COMSOL

Please login with a confirmed email address before reporting spam

I am trying to change the parameters for my COMSOL program using loops in MATLAB. The parameters I wanted to change were set to be taken as parameters in the model function. Once I change the parameters I have the program find results of the simulation. My problem is that in MATLAB, the graphs of the results for the different parameter combinations were looking incorrect (see attached picture, blue center should be together). When I plugged the same parameters directly into the COMSOL program and ran the simulation in COMSOL the graphs were just fine (see attached, blue is in center as needed). This is strange to me because I downloaded "Compact History" version of the original COMSOL simulation to MATLAB and then updated the parameters as follows:


%convert values to string so we can use them in parameter updates SIO2_str = num2str(SIO2_val); Electrode_gap_str = num2str(Electrode_gap_val); %this is what they call base gap Electrode_height_str = num2str(Electrode_height_val); %this is what they call electrode thickness

%adding units unit = '[um]'; SIO2_input = strcat(SIO2_str,unit); Electrode_gap_input = strcat(Electrode_gap_str, unit); Electrode_height_input = strcat(Electrode_height_str, unit);

model.param.set('Electrode_height', Electrode_height_input, 'height of electrode');%** model.param.set('Electrode_gap', Electrode_gap_input, 'Gap between electrodes'); %** model.param.set('Electrode_sidewall_height', SIO2_input);% ****


I even downloaded the "Compact History" for the simulation after directly entering the new parameters and it looked identical to the original "Compact History" code that I had updated with the exception of the parameter values. Any suggestions on how to fix this?



3 Replies Last Post Oct 19, 2021, 4:50 a.m. EDT
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 years ago Oct 18, 2021, 4:32 a.m. EDT

Hi Marissa

There could be a problem with the way mphplot works compared to plotting directly in Comsol. You can test this by running your code in Matlab and after having solved the problem open up Comsol to view the result using this command mphlaunch(model) This will open the Comsol GUI with the same model as in Matlab.

If the plot looks OK here and it looks different in Matlab as you describe then there is a bug in mphplot. Alternatively, there could be a problem with the parameter values and/or your solution process. You are welcome to send your model and script to Comsol support or post them here so we can help you further.

-------------------
Lars Gregersen
Comsol Denmark
Hi Marissa There could be a problem with the way mphplot works compared to plotting directly in Comsol. You can test this by running your code in Matlab and after having solved the problem open up Comsol to view the result using this command mphlaunch(model) This will open the Comsol GUI with the same model as in Matlab. If the plot looks OK here and it looks different in Matlab as you describe then there is a bug in mphplot. Alternatively, there could be a problem with the parameter values and/or your solution process. You are welcome to send your model and script to Comsol support or post them here so we can help you further.

Please login with a confirmed email address before reporting spam

Posted: 3 years ago Oct 18, 2021, 2:04 p.m. EDT

Hi Lars,

Thanks! As I looked more into it I realized that some of the values changed in the code every time I updated the parameters so rather than trying to update the parameters within the function I used the function to create the model, then updated the parameters, and then ran the simulation and used mphplot. This seemed to work a lot better. Thanks for the tip on mphlaunch(model). I'm checking my code again now and that was very helpful.

Marissa

Hi Lars, Thanks! As I looked more into it I realized that some of the values changed in the code every time I updated the parameters so rather than trying to update the parameters within the function I used the function to create the model, then updated the parameters, and then ran the simulation and used mphplot. This seemed to work a lot better. Thanks for the tip on mphlaunch(model). I'm checking my code again now and that was very helpful. Marissa

Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 years ago Oct 19, 2021, 4:50 a.m. EDT

Hi Marissa

You're welcome!

With regard to selections the recommended way of working with Comsol models it to load the MPH-file instead of running the saved M-file every time to set up the model from scratch. This make it easier to work with selections (since they are updated automatically exactly as they are in the Comsol GUI).

In case you wish to work out selections yourself I have made a blog post in three sections that describes how everything works: https://www.comsol.com/blogs/best-practice-working-model-m-files/

-------------------
Lars Gregersen
Comsol Denmark
Hi Marissa You're welcome! With regard to selections the recommended way of working with Comsol models it to load the MPH-file instead of running the saved M-file every time to set up the model from scratch. This make it easier to work with selections (since they are updated automatically exactly as they are in the Comsol GUI). In case you wish to work out selections yourself I have made a blog post in three sections that describes how everything works: https://www.comsol.com/blogs/best-practice-working-model-m-files/

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.