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

division by zero problem

division by zero problem

Hello,

i just wrote a simple program as follows:

Code:


model tmp
  parameter Real x(fixed = false);
  parameter Real y = 1 / x;
initial equation
  x = 2;
equation
end tmp;

when i run this program i get the following error:
division by zero at time 0, (a=1) / (b=0), where divisor b expression is: x

i slightly modified the above code as follows:

Code:


model tmp
  parameter Real x(fixed = false);
  parameter Real y(fixed=false);
initial equation
  x = 2;
  y = 1 / x;
equation
end tmp;

and the error did not appear anymore.

could you please explain me, why this happens?

Thanks,
Arvin

Re: division by zero problem

Hi,

It seems that in the first code, x is unevaluated when the code reaches y=1/x. And perhaps 0 is used as a default value for x.

In the second code, the value 2 is already assigned to x when the code reaches y=1/x.

Gregory

Re: division by zero problem

Thanks for your answer Gregory

So you mean that first equations in the parameter declaration are evaluated and then initial equations are solved?

,Arvin

Re: division by zero problem

I suppose it since I just begin with this language.

Also 'parameter' seems to be a keyword reserved for constants.

What do you want to do with this code ?

Re: division by zero problem

  suppr

Edited by: ggtlse - Nov-26-14 08:34:46

Re: division by zero problem

You could try and use the first version with the change that you set a start value for x like this:
Real x(start=2);

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