# Problem with scripting discrete estimator

**URL:** <https://forum.plexim.com/t/problem-with-scripting-discrete-estimator/775>\
**Category:** PLECS\
**Tags:** plecs, problem, discrete, c-script, derivative-infinite\
**Created:** [May 13, 2020, 6:13pm UTC](https://forum.plexim.com/t/problem-with-scripting-discrete-estimator/775 "2020-05-13T18:13:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nardi](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@Nardi](https://forum.plexim.com/u/Nardi)\
**Post date:** [May 13, 2020, 6:13pm UTC](https://forum.plexim.com/t/problem-with-scripting-discrete-estimator/775/1 "2020-05-13T18:13:05Z")

</div>

Greeting to all Plecs users,

I’m an electrical engineering student and I’m new to PLECS simulations.

I have some trouble with implementing some discrete equations in a C-script. I have a set of continuous equations:

 ![182](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/1X/37643813736eb029bfa148a688fd28b9b915921b.png)

and instead of modeling them with existing block (transfer functions, gains, summation points etc.) I would like to implement them into a C-script as a set of discrete equations with a non fixed sampling time (I would use the non fixed step from PLECS simulation):

 ![182](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/1X/7298969790d29d254578219057bb0dd9e4892436.jpeg)

Unfortunaly, when starting the simulation I always get an error that says a derivative is infinite or NaN and is pointing to the C-script in PLECS’ model of a synchronous machine that I’m using.

Could someone of You guys be so kind and check my C-script which is attached and see where is the problem, I suppose I wrote something wrong in C or defined some DiscState variables wrong.

Thank You very much for Your answers and precious time,

Nardi

[C-script estimator.plecs](https://forum.plexim.com/uploads/short-url/dVutQXe8aNCMEWH2AOGGuGDnk7m.plecs) (6.44 KB)

---

<div class="post-metadata">

**Author:** ![wolfgang](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/wolfgang/32/7_2.png) [@wolfgang](https://forum.plexim.com/u/wolfgang)\
**Post date:** [May 14, 2020, 10:20am UTC](https://forum.plexim.com/t/problem-with-scripting-discrete-estimator/775/2 "2020-05-14T10:20:49Z")

</div>

If you connect your C-Script with a Display block, you will find that the second output is indeed NaN. NaN and Inf values typically arise due to divisions by 0: 0/0 yields NaN, any non-zero number divided by 0 yields +/-Inf.

The culprit in your C code is this line:

```auto
current_value_delta = atan(current_value_psi_mq/current_value_psi_md);

```

You need to check whether “current\_value\_psi\_md” is zero and in this case calculate “current\_value\_delta” in a different way.

---

<div class="post-metadata">

**Author:** ![Nardi](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@Nardi](https://forum.plexim.com/u/Nardi)\
**Post date:** [May 14, 2020, 1:59pm UTC](https://forum.plexim.com/t/problem-with-scripting-discrete-estimator/775/3 "2020-05-14T13:59:54Z")

</div>

Dear Wolfgang,

Thank You very much for Your answer.

I’m not really sure if division by zero is the problem here. I tested the discrete esimator (in this C-script) outside my original control structure. In fact, I used my continuos estimator in the control structure and I just took the signals to the discrete estimator and looked a his outputs with displays. I didn’t give me any error, and I got the same values as I would get with the continuos estimator.

But when I delete the continuos one, a implement the discrete one in my control structure it gives me an error. 😕

Now I did all these calculations in major steps by adding if (IsMajorStep) before these calulations, and it work everything without an error but the signals are not very precise. 😕
