Help - Search - Members - Calendar
Full Version: Chattering
Modelica Forum > Modelica > Modelica
Djordjevic
I suppose I have chosen the wrong topic, because its not specific for Dymola but the programming language itself. HEre it is again.

Hello,

I have a problem with simulating a simple if-clause, so the system comes into chattering or sth. like an endless loop I think.

The model has an input 'u' and an output 'y'. The value comming into the element over the input 'u' shall be reached by the output using a linear function with a constant slope=150.

The algorithm part should determine if the slope is positive or negative and then the equation part shall reckon 'y'.

CODE
algorithm
   if y < u then
     sign :=1;
   elseif y > u then
     sign :=-1;
   else
     sign :=0;
   end if;
  
equation
  
der(y) = sign*150;


I suppose that my access to the Matter is lightly wrong, but I simply do not get what could be wrong...
Roland
Hi Djordjevic,

your topic is (apart from the typo) correct - your problem is shattering.

To explain the problem:
Let's assume y is < u so sign is 1. As soon as y > u, sign goes to -1. Due to the equation for y, it becomes smaller than u at the very next time step causing sign to change again and so on and so forth. Nothing wrong, but it does not really make sense either.

What you can do to avoid this problem is introducing an interval in which you switch sign:

CODE
model Test
  Real y;
  Real u;
  Real sign(start=1);
  
algorithm
  when y < u - 0.05 then
    sign :=1;
  elsewhen y > u + 0.05 then
    sign :=-1;
  end when;
  
equation
  u = sin(time);
  der(y) = sign*150;
  
end Test;


This model still changes the value of sign about every 0.0006 seconds, but it simulates really fast. If you fiddle a little bit with the size of the interval this further improves - and of course if you use the correct equation for u...

I hope this helped a little bit.
Roland
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.