Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Error using IF expression

Error using IF expression

Hi Everyone,

I want to implement an IF expression that enables two different sets of initial conditions. What I came up with is what follows:

Code:

parameter Real A;


if A ==1 then

Real y1(start=0.2), y2(start=3.5), y3(start=0.5);

else

Real y1(start=2.2), y2(start=1.5), y3(start=0.3);

end if;

Unfortunately, using the above code I got the following syntax error message:

No viable alternative near token: if

Could you help me figure this out?

Thank you so much!

Gabri

Edited by: Gabri - Jun-19-20 14:40:31

Re: Error using IF expression

You cannot have IF in the component section in a Modelica model (well, except for conditional components). You can only use if inside equations. However, what you want can be done via if expressions:

Code:


parameter Real A;
Real y1(start=if A ==1 then 0.2 else 2.2),
         y2(start=if A ==1 then 3.5 else 1.5),
         y3(start=if A ==1 then 0.5 else 0.3);

There are 0 guests and 0 other users also viewing this topic