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.

If else condition syntax in comsol multiphysics 4.2

Please login with a confirmed email address before reporting spam

Hi
well i would like to impliment a function which is having different values at different ranges. Can any one provide me syntax of if else condition in comsol. Even any example of Implimentation can serve my purpose :)....

Krishna Chaitanya S

14 Replies Last Post Jun 1, 2017, 4:58 a.m. EDT

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 3:10 a.m. EST
Hi,

For a simple rule of 2 variables x and y: if x<a then y=c1 else y=c2
In Comsol: y= if(x<a,c1,c2)

Furthermore, i don't know how to link 2 rules with 3 variables x,y,z:

if x<a then y=c1
else if z<b then y=c2
else y=c3

I will appreciate if anyone know the solution.

Best regards,

Dinh An
Hi, For a simple rule of 2 variables x and y: if x

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 3:48 a.m. EST
Hi

you can also try something like for a < b
c1*(x<a)+c2*(x>=a)*(x<b)+c3*(x>=b)

but you should perhaps consider step functions to smoothen the jumps, as your solver might get caught in such Dirac step functions

--
Good luck
Ivar
Hi you can also try something like for a < b c1*(x=a)*(x=b) but you should perhaps consider step functions to smoothen the jumps, as your solver might get caught in such Dirac step functions -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 3:59 a.m. EST
Hi Ivar,

Thanks for your response. I have one more question if you can help me.

c1*(x<a)+c2*(x>=a)*(x<b)+c3*(x>=b)

If we use a step function in this case, and if i change a little bit this expression such as:

c1*(x<a)+c2*(x>=a)*(y<b)+c3*(y>=b) so y is the second variable vs time.

I know the step function can separately solve : c1*(x<a)+c2*(x>=a) and c2*(y<b)+c3*(y>=b)

If i would like to link these 2 steps in one expression at the same time, do you think this is possible ?


Thanks once more time

Dinh An
Hi Ivar, Thanks for your response. I have one more question if you can help me. c1*(x=a)*(x=b) If we use a step function in this case, and if i change a little bit this expression such as: c1*(x=a)*(y=b) so y is the second variable vs time. I know the step function can separately solve : c1*(x=a) and c2*(y=b) If i would like to link these 2 steps in one expression at the same time, do you think this is possible ? Thanks once more time Dinh An

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 5:40 a.m. EST
Thanks this is usefull imformation for me
Thanks this is usefull imformation for me

Lechoslaw Krolikowski

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 6:38 a.m. EST

Furthermore, i don't know how to link 2 rules with 3 variables x,y,z:

if x<a then y=c1
else if z<b then y=c2
else y=c3




Hi,

The if operator can be nested, hence y=if(x<a,c1,if(z<b,c2,c3)) is a valid expression.

Regards,
Andrzej
[QUOTE] Furthermore, i don't know how to link 2 rules with 3 variables x,y,z: if x

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 6:49 a.m. EST
Thanks Andrzej,

Do you have any idea if we use step1() function to represent this conditions ?

Because as said in the user's guide :"Use smoothing to improve the behavior of the model by avoiding discontinuities that are difficult to handle numerically. "

Regards,

Dinh An
Thanks Andrzej, Do you have any idea if we use step1() function to represent this conditions ? Because as said in the user's guide :"Use smoothing to improve the behavior of the model by avoiding discontinuities that are difficult to handle numerically. " Regards, Dinh An

Lechoslaw Krolikowski

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 7:49 a.m. EST
Hi,

y=((c3-c2)*step1(z-b)+c2-c1)*step2(x-a)+c1.

Parameters of both step functions: Location 0, From 0 To 1. Size of transition zone can be different.

Regards,
Andrzej
Hi, y=((c3-c2)*step1(z-b)+c2-c1)*step2(x-a)+c1. Parameters of both step functions: Location 0, From 0 To 1. Size of transition zone can be different. Regards, Andrzej

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 9, 2011, 7:59 a.m. EST
Andrzej,

I really appreciate your response. It works well. I can now add more conditions in this serie.

Many thanks to you and thanks to the Comsol forum.

Best regards,

Dinh An
Andrzej, I really appreciate your response. It works well. I can now add more conditions in this serie. Many thanks to you and thanks to the Comsol forum. Best regards, Dinh An

Please login with a confirmed email address before reporting spam

Posted: 9 years ago Aug 18, 2015, 2:50 p.m. EDT
Hi Ivar I´ve applied your usefull tip for a variable spring constant having dependence to a spring extension, but seems that only gets the first k value, I´ve use comsol 4.4 ¿Can you, or someone else, figure out why doesnt work? This is my global variable probe: if(ppb1<0.01,27500000,if(ppb1<0.05,0.001,20340000))

Hi Ivar I´ve applied your usefull tip for a variable spring constant having dependence to a spring extension, but seems that only gets the first k value, I´ve use comsol 4.4 ¿Can you, or someone else, figure out why doesnt work? This is my global variable probe: if(ppb1

Please login with a confirmed email address before reporting spam

Posted: 8 years ago May 28, 2017, 9:15 a.m. EDT
Hi,

How if I want to make these logic with if function?:

Q1: if t<4 [d] then Q=0.5, else Q1=0
Q2: if 4[d]<= t <8[d] then Q2=0.5, else Q2=0
Q3: if 8[d]<= t <12[d] then Q3=0.5, else Q3 = 0
Q4: if t>= 12[d] then Q4 = 0.5, else Q4 = 0

Thank for your help
Hi, How if I want to make these logic with if function?: Q1: if t

Please login with a confirmed email address before reporting spam

Posted: 8 years ago May 30, 2017, 6:44 p.m. EDT
Updated: 8 years ago May 30, 2017, 6:45 p.m. EDT

Hi,

How if I want to make these logic with if function?:

Q1: if t<4 [d] then Q=0.5, else Q1=0
Q2: if 4[d]<= t <8[d] then Q2=0.5, else Q2=0
Q3: if 8[d]<= t <12[d] then Q3=0.5, else Q3 = 0
Q4: if t>= 12[d] then Q4 = 0.5, else Q4 = 0

Thank for your help


Just define them separately in the variable section.
Q1 : if( t < 4 [d], 0.5 , 0)
and so on.
[QUOTE] Hi, How if I want to make these logic with if function?: Q1: if t

Please login with a confirmed email address before reporting spam

Posted: 8 years ago May 31, 2017, 3:12 a.m. EDT

Just define them separately in the variable section.
Q1 : if( t < 4 [d], 0.5 , 0)
and so on.


Thank you for your response.
I tried that way, but the singularity error appeared.
Then I used piecewise function and it worked.

[QUOTE] Just define them separately in the variable section. Q1 : if( t < 4 [d], 0.5 , 0) and so on. [/QUOTE] Thank you for your response. I tried that way, but the singularity error appeared. Then I used piecewise function and it worked.

Walter Frei COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago May 31, 2017, 9:02 a.m. EDT
Hello,
If this question is about applying a loading that changes instantaneously in time, then you may also be interested in reviewing these articles:
www.comsol.com/blogs/modeling-a-periodic-heat-load/
www.comsol.com/blogs/implementing-a-thermostat-with-the-events-interface/
Hello, If this question is about applying a loading that changes instantaneously in time, then you may also be interested in reviewing these articles: https://www.comsol.com/blogs/modeling-a-periodic-heat-load/ https://www.comsol.com/blogs/implementing-a-thermostat-with-the-events-interface/

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 1, 2017, 4:58 a.m. EDT
Updated: 8 years ago Jun 1, 2017, 5:00 a.m. EDT

Hello,
If this question is about applying a loading that changes instantaneously in time, then you may also be interested in reviewing these articles:
www.comsol.com/blogs/modeling-a-periodic-heat-load/
www.comsol.com/blogs/implementing-a-thermostat-with-the-events-interface/


Hello Walter, thank you for your information.
I found it useful to enhance my current solution.

I have another question.
I'm modeling a 2D Heat Transfer in Porous Media module (time-dependent).
There's a boundary (outflow boundary) which activated / deactivated by time function.

For instance:
Outflow boundary active at time 0-4 day
Outflow boundary not active at time 4-8 day.

However, for this outflow boundary, there is no text-field where I can state a function.
So this outflow boundary is like a "non modifiable" boundary.

Could you please kindly suggest me how to deal with this problem?

Thank you



[QUOTE] Hello, If this question is about applying a loading that changes instantaneously in time, then you may also be interested in reviewing these articles: https://www.comsol.com/blogs/modeling-a-periodic-heat-load/ https://www.comsol.com/blogs/implementing-a-thermostat-with-the-events-interface/ [/QUOTE] Hello Walter, thank you for your information. I found it useful to enhance my current solution. I have another question. I'm modeling a 2D Heat Transfer in Porous Media module (time-dependent). There's a boundary (outflow boundary) which activated / deactivated by time function. For instance: Outflow boundary active at time 0-4 day Outflow boundary not active at time 4-8 day. However, for this outflow boundary, there is no text-field where I can state a function. So this outflow boundary is like a "non modifiable" boundary. Could you please kindly suggest me how to deal with this problem? Thank you

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.