Generating two random time depedant veariables with different sample periods

前端 未结 1 497
予麋鹿
予麋鹿 2021-01-29 08:13

Following this question, I\'m trying to generate two time-dependent random functions omega1 and tau using this example. The difference is that I need t

相关标签:
1条回答
  • 2021-01-29 08:31

    You have omega1 := 0; in two when initial()statements. Replace it by tau := 0; in the second one and the example will work.

    I recommend to cleanup your code a bit. I found various smaller issues and needless code lines.

    • everything related to the impure random numbers can be removed
    • localSeed2 and globalSeed2 are useless when they are initialized like the other seed variables
    • state1024 is initialized at 3 different places (even though it works with OpenModelica): with start values and fixed=true and in two different when initial() statements
    • omega2 and tau2 don't need to be outputs. The Tool determines by itself what it has to compute.
    • And finally: Modelica models are a lot easier to debug and understand if existing blocks and physical components are used instead of writing lengthy code in a single class. Your model can also be built graphically with blocks from Modelica.Blocks.Noise and components from Modelica.Mechanics.Rotational.

    Below is an updated version of your code with units, only one section for initialization and removed algorithm section (not necessary anymore due to the additional variables rand_omega and rand_tau).

    model testData2
    
      extends Modelica.Icons.Example;
      import Modelica.Math.Random.Generators;
      import Modelica.Math.Random.Utilities;
      import SI = Modelica.SIunits;
    
      parameter SI.RotationalSpringConstant k = 50.0;
      parameter SI.Inertia J = 0.001;
    
      parameter SI.Period samplePeriod_tau = 0.17;
      parameter SI.Period samplePeriod_omega = 0.05;
    
      parameter Integer globalSeed = 30020;
      parameter Integer localSeed_tau = 614657;
      parameter Integer localSeed_omega = 45613;
    
      SI.Angle theta1, theta2;
      SI.AngularVelocity omega1, omega2, rand_omega;
      SI.Torque tau, rand_tau;
    
    protected 
      discrete Integer state1024_tau[33];
      discrete Integer state1024_omega[33];
    
    initial equation 
    
      state1024_omega = Generators.Xorshift1024star.initialState(localSeed_omega, globalSeed);
      state1024_tau = Generators.Xorshift1024star.initialState(localSeed_tau, globalSeed);
    
      theta1 = 0;
      theta2 = 0;
      der(theta2) = 0;
    
    equation 
    
      when sample(0, samplePeriod_omega) then
        (rand_omega, state1024_omega) = Generators.Xorshift1024star.random(pre(state1024_omega));
      end when;
    
      when sample(0, samplePeriod_tau) then
        (rand_tau, state1024_tau) = Generators.Xorshift1024star.random(pre(state1024_tau));
      end when;
    
      der(theta1) = omega1;
      der(theta2) = omega2;
    
      omega1 = (rand_omega - 0.5) * 13;
      tau = (rand_tau - 0.5) * 3;
    
      J * der(omega2) = 0 + k * (theta1 - theta2);
    
    annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.02));
    end testData2;
    
    0 讨论(0)
提交回复
热议问题