How to avoid "zero division" in Modelica

Minoru Yubuchi_21420
Minoru Yubuchi_21420 New Altair Community Member
edited October 2020 in Community Q&A

Dear Experts,

 

My Modelica sript contains the following equation:

 

  tan(gamma) = (y - y_b)/(x - x_b);

 

where y, x, x_b, y_b are variables. 

 

Please let me have your advice how to avoid zero-division (to avoid to become x=x_b) in Modelica script.

 

When I tried to insert the following statements in Modelica argorithm section:

 

  if abs(x-x_b) <= 0.01 then
    x := x_b + 0.01;
  end if; 

 

of cource, as you expected, Activate issued the error: system is overdetermined.

 

Regards,

Minoru Yubuchi

Answers

  • RoKet
    RoKet
    Altair Employee
    edited June 2019

    Hi Minoru,

     

    To solve the division by zero you can use an if-clause. I have used an additional variable auxval, alternatively you can use the tan(gamma)-expression directly inside the if-clause.

     

         Real auxval(start=0.01);  equation      if (abs(x - x_b) <= 0.01) then         auxval = 0.01;     else         auxval = x - x_b;     end if;          tan(gamma) = (y - y_b) / auxval;

    Best regards,
    Ronald

     

  • Minoru Yubuchi_21420
    Minoru Yubuchi_21420 New Altair Community Member
    edited June 2019

    Ronald,

     

    Thank you very much!

     

    Minoru Yubuchi