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.

Comsol with Matlab? How to write functions from COMSOL model m files?(Diffusion)

Please login with a confirmed email address before reporting spam

I have a Comsol model m file and I want to convert it to a function m file. I am using diffusion application mode and my diffusion coefficient is a function of concentration.

D=D1*exp(-B*(1-c/10))

In my model m file ;

eq.D='D1*exp(-B*(1-c/10))'


In the function m file I would like to input 'D1 and B1' as inputs. However since it is a string defined in eq.D the function input parameters are not recognised.

My question is that :

How can I convert the model m file to a function m file such that my parameters in the diffusion coefficient will be recognised as inputs in the function?

Thanks a lot

Mecit

1 Reply Last Post Oct 20, 2009, 12:18 p.m. EDT

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Oct 20, 2009, 12:18 p.m. EDT
Mecit,
you should be able to do this by constructing the eq.D expression as a string. For example, your m file will look like this:
.
.
D1=0.01 % numerical value of D1 parameter
B=0.005 % numerical value of B parameter
.
.
equ.D = [num2str(D1) '*exp(-' num2str(B) '*(1-c/10))'];
.
.

As you see all the string "pieces" in "[ ]" are concatenated together. you convert numbers to strings using num2str function, and leave the remainder of the expressions as string pieces enclosed between ' ' marks.

Hope this helps,
Ozgur



I have a Comsol model m file and I want to convert it to a function m file. I am using diffusion application mode and my diffusion coefficient is a function of concentration.

D=D1*exp(-B*(1-c/10))

In my model m file ;

eq.D='D1*exp(-B*(1-c/10))'


In the function m file I would like to input 'D1 and B1' as inputs. However since it is a string defined in eq.D the function input parameters are not recognised.

My question is that :

How can I convert the model m file to a function m file such that my parameters in the diffusion coefficient will be recognised as inputs in the function?

Thanks a lot

Mecit


Mecit, you should be able to do this by constructing the eq.D expression as a string. For example, your m file will look like this: . . D1=0.01 % numerical value of D1 parameter B=0.005 % numerical value of B parameter . . equ.D = [num2str(D1) '*exp(-' num2str(B) '*(1-c/10))']; . . As you see all the string "pieces" in "[ ]" are concatenated together. you convert numbers to strings using num2str function, and leave the remainder of the expressions as string pieces enclosed between ' ' marks. Hope this helps, Ozgur [QUOTE] I have a Comsol model m file and I want to convert it to a function m file. I am using diffusion application mode and my diffusion coefficient is a function of concentration. D=D1*exp(-B*(1-c/10)) In my model m file ; eq.D='D1*exp(-B*(1-c/10))' In the function m file I would like to input 'D1 and B1' as inputs. However since it is a string defined in eq.D the function input parameters are not recognised. My question is that : How can I convert the model m file to a function m file such that my parameters in the diffusion coefficient will be recognised as inputs in the function? Thanks a lot Mecit [/QUOTE]

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.