standard deviation

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

standard deviation

Post by wlederer »

We have automatical regulation equipment. Accordingly, two values for a parameter, expected and real one. How to calculate the standard deviation value? Do appropriate functions in MSDN library available?
I tried to program calculation direct:
Globals.Tags.UV_Dose_dev_std.Value=(Globals.Tags.UV_Dose_dev_sq_sum.Value/Globals.Tags.Measure_number)^(1/2);
but got error. Could not find any Math() after typing:using System...

Thanks in advance.
Waldemar

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: standard deviation

Post by mark.monroe »

That should not require you to add Math into your workspace. Those are all normal C# math operations.

//Your code has an error in it:
Globals.Tags.UV_Dose_dev_std.Value=(Globals.Tags.UV_Dose_dev_sq_sum.Value/Globals.Tags.Measure_number)^(1/2);

//This is correct:
Globals.Tags.UV_Dose_dev_std.Value=(Globals.Tags.UV_Dose_dev_sq_sum.Value/Globals.Tags.Measure_number.Value)^(1/2);
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

Re: standard deviation

Post by wlederer »

Thank You Mark for the answer and finding an error in my code. Now the compilation is OK, but the result is always zero. Checked components of the expression, every calculation is OK, except square root.
How to do it correctly?
Regards, Waldemar

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

Re: standard deviation

Post by wlederer »

Found solution:
Globals.Tags.UV_Dose_dev_std.Value=Math.Sqrt(Globals.Tags.UV_Dose_dev_sq_sum.Value/Globals.Tags.Measure_number.Value);
How can I insert a special symbol "±" to the screen?
I mean, how to change font to Symath for the string:
Globals.Tags.Plus_minus.Value=" E";

Regards, Waldemar

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: standard deviation

Post by mark.monroe »

You need to change the font on the component to the font you want. Select the component and then go to the font section of the Property Grid. If you do not see your font there, then you need to add it to your computers OS.
Snap 2012-08-28 at 08.15.46.jpg
Snap 2012-08-28 at 08.15.46.jpg (52.2 KiB) Viewed 15419 times
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

Patrick Hall
Posts: 22
Joined: Fri May 25, 2012 7:44 am
Location: Charlotte, NC. USA

Re: standard deviation

Post by Patrick Hall »

the ± symbol is present in many of the normal system fonts. Tahoma (the default font in iX Developer).

You could add the symbol to a standard text label, or you should be able to apply the symbol to the prefix (or suffix if you prefer) of an AnalogNumeric control so that the symbol is right next to the number. You do not necessarily have to change the font. The trick is getting iX developer to accept ALT code entry (which it appears unable to do presently) Trying to enter an ALT code just causes a lovely "Ding" sound.

You could open Word, Notepad or any other editor and press ALT+0177 (i.e. press and hold ALT while typing 0 1 7 7 on the number pad). Then select the ± symbol and copy, then return to iX and choose the destination text area and Paste (CTRL+V).

Or I prefer the Character Map Program (Start / Run... / "charmap"), you can browse the characters of all the installed fonts, select (copy to clipboard) or simply reference the ALT+ key code.

Also...
mark.monroe wrote:That should not require you to add Math into your workspace. Those are all normal C# math operations.
...
//This is correct:
Globals.Tags.UV_Dose_dev_std.Value=(Globals.Tags.UV_Dose_dev_sq_sum.Value/Globals.Tags.Measure_number.Value)^(1/2);
I am pretty sure the reason Waldemar is getting Zero's is the use of ^(1/2) while it is true that the Square root of a number is the same as raising the number to the power of 1/2 (or 0.5) the reason it is not working in this example comes down to how the C# compiler treats numerical constants. In this case the C# compiler is treating the (1/2) as an integer division which means it is actually ^0.
Since a tag's (GlobalDataItem) Value property is VariantValue the results of the (UV_Dose_dev_sq_sum.Value / Measure_number.Value) is being internally cast to the Variant Integer representation, *.Value.Int versus the *.Value.Double floating point version.

While Math.Sqrt whos parameter is expected to be double Math.Sqrt(double) is ensuring the cast or VariantValue Value.Double is used.

Somethings to keep in mind going forward, either cast explicitely or use the literal suffix to tell the compiler what type of number you are using.
1/2 is integer 1 / integer 2 (result int 0)
1.0f/2.0f is float 1.0 / float 2.0 (result float 0.5f)
1.0/2.0 is double 1.0 / double 2.0 (result double 0.5)

Another trick is to reference Value.Int or Value.Double explicitely but these values may not be what you expected. I tend to use explicite casts when dealing with any tag's Value property if I have any doubts about how the compiler will treat the code... at least then I know what I am asking the compiler to do specifically, and it will tell me if it has a problem meeting that request.
Attachments
8-28-2012 1-54-16 PM.png
8-28-2012 1-54-16 PM.png (43.22 KiB) Viewed 15417 times
8-28-2012 1-50-51 PM.png
8-28-2012 1-50-51 PM.png (8.64 KiB) Viewed 15417 times
Best Regards,
Patrick Hall

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

Re: standard deviation

Post by wlederer »

Thank You very much to Mark and Patrick for the usefull help.
What is confusing me, as far as I remember, any figure in the power of zero equal one. (Is this correct?) But result was zero. Why?
Another question:
I want to save the calculated values for the future reference. Is there any tools like indexing, which will allow me to find the wanted information later easy? For example, data of session #5 at 20 Aug. 2012 and so on?
Regards, Waldemar

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

Re: standard deviation

Post by Ron L. »

The recipe feature in iX Developer would be one good way to save values for future reference.

You can also mark internal variables as non-volatile to save them.
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

Patrick Hall
Posts: 22
Joined: Fri May 25, 2012 7:44 am
Location: Charlotte, NC. USA

Re: standard deviation

Post by Patrick Hall »

wlederer wrote:Thank You very much to Mark and Patrick for the usefull help.
What is confusing me, as far as I remember, any figure in the power of zero equal one. (Is this correct?) But result was zero. Why?
Another question:
I want to save the calculated values for the future reference. Is there any tools like indexing, which will allow me to find the wanted information later easy? For example, data of session #5 at 20 Aug. 2012 and so on?
Regards, Waldemar
Well, aside from my amnesia and the fact I have been programming in C#, IEC61131 (ST, LD, and SFC) and testing equations in Excel, I totally overlooked the fact that in C# the "^" is not the power symbol, It is actually the XOR (Exclusive OR) operator. It completely slipped my mind since I have been using the ^ all day in Excel where it is actually the power operator. IT wasn't until I started to recreate the

I have the same brain lapse sometimes after a long day of IEC (ST) programming…
myVar:=REAL#0.0;
When I get back into C# I keep wanting to use the same Pascal like assignment :=, as well as the tendency to bang out the IEC Type Literals REAL#, INT#, UDINT#, etc.
but I digress...

Math.Pow(double, double) is the correct way to raise a number to a given power in C#. Math.Sqrt(V1/V2) is the better way to go (As you have already found).


As to your second question logging the results for future reporting sounds like a DataLogger (as Mark mentions) and perhapse some report generator. I am new to Beijer myself and as such have not yet had to cross the data logging bridge. Though my current project will likely take me down that road in the coming days.
Best Regards,
Patrick Hall

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

Re: standard deviation

Post by wlederer »

Thank You very much. I will try the recommended options.
Another question, how can change the string colour depending on if the result is OK or not?
regards, Waldemar

Post Reply