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.

Debugging MATLAB Function used by COMSOL

Please login with a confirmed email address before reporting spam

Hi, I have a MATLAB function that uses "&" operators and, although it works fine when I test it in my MATLAB window, COMSOL cannot seem to handle it. I get this error, "Operands to the || and && operators must be convertible to logical scalar values."

I have already tried changing the "&" symbols in my function to "&&" symbols, but I still get the same error.

I use .mat files in my MATLAB programs which don't seem to want to be uploaded here, so I guess I can't post all of my files.

Thanks in advance. -Tim



4 Replies Last Post 8 janv. 2019, 04:43 UTC−5
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 6 years ago 4 janv. 2019, 04:21 UTC−5

When Comsol calls your Matlab function it uses (long) vectors as arguments.

You should test that your functions work with vector arguments (of the same size) and that your function returns a vector of the same size as the input.

-------------------
Lars Gregersen
Comsol Denmark
When Comsol calls your Matlab function it uses (long) vectors as arguments. You should test that your functions work with vector arguments (of the same size) and that your function returns a vector of the same size as the input.

Please login with a confirmed email address before reporting spam

Posted: 6 years ago 4 janv. 2019, 14:59 UTC−5

Thanks for the quick reply!

I understand what you're saying. I checked and made sure my function works given long vectors. In fact everything was working when I was using a 2D COMSOL model (attached), but since moving to a 3D model, I haven't gotten it to work.

I may be able to circumvent the problem by rewriting my code to avoid the use of logical operators, but I'd love to hear if you have any other, better ideas.

Thanks! Tim

Thanks for the quick reply! I understand what you're saying. I checked and made sure my function works given long vectors. In fact everything was working when I was using a 2D COMSOL model (attached), but since moving to a 3D model, I haven't gotten it to work. I may be able to circumvent the problem by rewriting my code to avoid the use of logical operators, but I'd love to hear if you have any other, better ideas. Thanks! Tim


Please login with a confirmed email address before reporting spam

Posted: 6 years ago 4 janv. 2019, 16:27 UTC−5

It's working now!

I closed out of everything and opened it up to find things working. I don't know what all happened...I may have tried a bad boundary condition intially and COMSOL didn't register my correction until after I had saved and closed everything and opened it back up again.

Thanks for the help.

Tim

It's working now! I closed out of everything and opened it up to find things working. I don't know what all happened...I may have tried a bad boundary condition intially and COMSOL didn't register my correction until after I had saved and closed everything and opened it back up again. Thanks for the help. Tim

Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 6 years ago 8 janv. 2019, 04:43 UTC−5

Note the "Clear Functions" button on the MATLAB node in the model. When clicking this button Matlab will reload the functions before using them. This picks up changed you have made to the files. Yes, Matlab should do this automatically, but sometimes it doesn't.

When working on your functions it may be beneficial to put a checkmark in "Clear functions automatically before solving" - unless your functions rely on not being reloaded too often; e.g. if they load a lot of data or perform very expensive calculations when initialising.

-------------------
Lars Gregersen
Comsol Denmark
Note the "Clear Functions" button on the MATLAB node in the model. When clicking this button Matlab will reload the functions before using them. This picks up changed you have made to the files. Yes, Matlab should do this automatically, but sometimes it doesn't. When working on your functions it may be beneficial to put a checkmark in "Clear functions automatically before solving" - unless your functions rely on not being reloaded too often; e.g. if they load a lot of data or perform very expensive calculations when initialising.

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.