- Index
- » Programming
- » Modelica Language
- » issue generating triangular wave...
issue generating triangular wave function in Modelica
issue generating triangular wave function in Modelica
same question in stackoverflow
I'm trying to create a model where one Modelica variable is a triangular wave of another variable. First I tried the floor() function as below:
Code:
model test1
final constant Real pi=2*Modelica.Math.asin(1.0);
parameter Real b = 1;
parameter Real a = 1;
Real x,p,u;
equation
if sign(sin(x*pi/b))>=0 then
p=a*(x-b*floor(x/b));
else
p=a*(b-(x-b*floor(x/b)));
end if;
x=time;
u = floor(x/b);
end test1
(x=time; is arbitrary so the model compiles)
but the result is weird, as you can see below
zoom in:
somehow 0.005 seconds before the next step floor function behaves unexpectedly and becomes a linear function ending by the next value.
then I tried the ceil() function. everything seemed right till I realised the same problem happens with ceil() function at other values (e.g. x=13)
I would appreciate if you could:
1. help me understand why this "glitch" happens and if it is intentional by design or a bug?
2. how I can fix this?
3. are there any alternatives to create a triangular wave function?
P.S. I am using this "wave function" to model the interaction between two jagged bodies
- Index
- » Programming
- » Modelica Language
- » issue generating triangular wave...