Hi. I am also having a standard deviation problem. When I try to calculate the sandard deviation of the numbers below with the suggested algorithm, I run into square root of a negative number. The algorithm does work for some data sets, but neither this one or 8 others. Does anybody have any suggestions?
Thanks.

ROI_z_cur-100.72-100.72-100.75-100.73-100.73-100.7-100.69-100.72-100.71-100.73-100.71-100.72-100.72-100.72-100.73-100.7-100.73-100.72-100.7-100.73-100.74-100.74-100.72-100.71-100.73-100.74-100.75-100.74-100.77-100.72-100.74-100.72-100.74-100.72-100.73-100.69-100.73-100.73-100.75-100.72-100.71-100.73-100.71-100.73-100.71-100.72-100.7-100.72-100.71-100.73-100.71-100.73-100.71-100.72-100.72-100.74-100.73-100.75-100.73-100.72-100.69-100.73-100.7-100.72-100.74-100.73-100.72-100.73-100.71-100.72-100.72-100.72-100.72-100.73-100.73-100.73-100.75-100.73-100.74-100.73-100.72-100.74-100.73-100.72-100.72-100.74-100.74-100.73-100.73-100.74-100.71-100.72-100.71-100.72-100.72-100.74-100.72-100.74-100.71-100.74-100.74-100.73-100.73-100.73-100.73-100.72-100.74-100.73-100.71-100.72-100.72-100.72-100.73-100.72-100.71-100.71-100.7-100.73-100.73-100.73-100.72-100.72-100.74-100.74-100.73-100.74-100.73-100.71-100.71-100.73-100.72-100.73-100.74-100.72-100.73-100.72-100.71-100.73-100.73-100.74-100.74-100.73-100.73-100.73-100.73-100.73-100.74-100.73-100.71-100.73-100.74-100.73-100.74-100.72-100.71-100.73-100.7-100.73-100.75-100.72-100.74-100.75-100.74-100.69-100.72-100.7-100.72-100.7-100.72-100.73-100.71-100.73-100.71-100.72-100.72-100.73-100.73-100.72-100.74-100.73-100.72-100.72-100.73-100.72-100.74-100.73-100.71-100.74-100.76-100.74-100.74-100.72-100.73-100.74-100.7-100.71-100.75-100.73-100.74-100.73-100.73-100.73-100.72-100.72-100.72-100.72-100.71-100.73-100.72-100.73-100.72-100.73-100.72-100.73-100.73-100.74-100.72-100.72-100.72-100.73-100.72-100.71-100.73-100.73-100.72-100.72-100.72-100.72-100.72-100.74-100.7-100.73-100.71-100.74-100.73-100.74-100.73-100.72-100.72-100.71-100.71-100.73-100.73-100.74-100.74-100.72-100.74-100.7-100.73-100.73-100.71-100.71-100.74-100.73-100.74-100.73-100.74-100.74-100.73-100.72-100.73-100.74-100.73-100.73-100.73-100.73-100.73-100.72-100.73-100.72-100.72-100.72-100.72-100.7-100.75-100.74-100.71-100.74-100.73-100.72-100.73-100.74-100.72-100.74-100.73-100.73-100.71-100.73-100.74-100.71-100.73-100.71-100.72-100.73-100.73-100.7-100.72-100.7-100.74-100.74-100.73-100.74-100.74-100.73-100.71-100.72-100.71-100.73-100.73-100.71-100.7-100.72-100.72-100.75-100.74-100.74-100.72-100.73-100.73-100.73-100.71-100.73-100.74-100.74-100.73-100.71-100.73-100.72-100.71-100.72-100.73-100.74-100.7-100.7-100.72-100.73-100.72-100.74-100.69-100.73-100.73-100.72-100.72-100.7-100.73-100.72-100.74-100.71-100.74-100.71-100.72-100.74-100.73-100.71-100.73-100.73-100.71-100.72-100.73-100.73-100.74-100.72-100.75-100.73-100.74-100.73-100.73-100.71-100.72-100.73-100.73-100.73-100.74-100.74-100.74-100.72-100.72-100.71-100.73-100.72-100.74-100.72-100.72-100.74-100.73-100.72-100.74-100.73-100.71-100.73-100.73-100.73-100.73-100.72-100.73-100.73-100.71-100.72-100.73-100.74-100.74-100.71-100.73-100.74-100.74-100.74-100.73-100.74-100.75-100.74-100.74-100.72-100.73-100.72-100.71-100.71-100.71-100.7-100.72-100.69-100.74-100.72-100.73-100.72-100.71-100.74-100.71-100.72-100.7-100.72-100.72-100.72-100.71-100.72-100.72-100.72-100.71-100.73-100.73-100.71-100.73-100.7-100.74-100.73-100.74-100.75-100.75-100.71-100.72-100.7-100.71-100.71-100.7-100.74-100.74-100.73-100.74-100.73-100.72-100.73-100.72-100.74-100.74-100.71-100.73-100.72-100.72-100.73-100.72-100.74-100.74-100.72-100.72-100.73-100.72-100.73-100.73-100.72-100.74-100.73-100.71-100.73-100.73-100.72-100.73-100.72-100.73-100.73-100.73-100.72-100.72-100.72-100.74-100.74-100.74-100.71-100.73-100.73-100.7-100.72-100.72-100.73-100.72-100.72-100.73-100.7-100.72-100.71-100.71-100.73-100.73-100.73-100.73-100.73-100.74-100.72-100.74-100.72-100.73-100.74-100.73-100.72-100.71-100.75-100.73-100.72-100.73-100.72-100.74-100.73-100.74-100.73-100.73-100.73-100.73-100.73-100.73-100.74-100.73-100.71-100.75-100.74-100.74-100.74-100.72-100.73-100.72-100.73-100.71-100.72-100.73-100.74-100.74-100.75-100.72-100.73-100.73-100.74-100.71-100.74-100.72-100.71-100.72-100.74-100.73-100.73-100.74-100.75-100.73-100.72-100.72-100.71-100.72-100.74-100.71-100.72-100.73-100.72-100.71-100.72-100.73-100.74-100.72-100.74-100.73-100.72-100.74-100.72-100.74-100.72-100.72-100.68-100.71-100.71-100.72-100.74-100.7-100.73-100.73-100.73-100.7-100.75-100.74-100.74-100.72-100.75-100.72-100.72-100.73-100.72-100.73-100.75-100.71-100.71-100.73-100.71-100.71-100.73-100.74-100.74-100.73-100.72-100.7-100.73-100.69-100.73-100.69-100.74-100.74-100.71-100.72-100.72-100.73-100.74-100.72-100.72-100.71-100.72-100.72-100.7-100.72-100.73-100.73-100.73-100.73-100.73-100.71-100.74-100.71-100.72-100.74-100.72-100.73-100.74-100.69-100.73-100.74-100.72-100.72-100.73-100.73-100.72-100.73-100.72-100.72-100.73-100.72-100.74-100.73-100.75-100.72-100.74-100.72-100.71-100.72-100.73-100.72-100.74-100.74-100.7-100.73-100.73-100.7-100.73-100.72-100.73-100.72-100.71-100.73-100.71-100.69-100.72-100.72-100.73-100.73-100.73-100.72-100.74-100.74-100.72-100.71-100.72-100.72-100.72-100.73-100.72-100.73-100.72-100.72-100.72-100.73-100.72-100.73-100.72-100.72-100.7-100.73-100.73-100.72-100.7-100.74-100.73-100.74-100.74-100.73-100.72-100.7-100.72-100.71-100.73-100.71-100.75-100.74-100.74-100.72-100.73-100.72-100.73-100.71-100.72-100.74-100.73-100.72-100.72-100.72-100.75-100.73-100.7-100.73-100.72-100.71-100.74-100.74-100.75-100.72-100.74-100.74-100.74-100.75-100.73-100.73-100.73-100.72-100.7-100.72-100.74-100.71-100.73-100.71-100.74-100.73-100.71-100.72-100.74-100.74-100.73-100.72-100.74-100.7-100.74-100.74-100.74-100.72-100.72-100.72-100.73-100.74-100.74-100.71-100.73-100.72-100.73-100.7-100.72-100.73-100.7-100.74-100.74-100.74-100.73-100.73-100.72-100.72-100.7-100.74-100.73-100.72-100.73-100.73-100.74-100.74-100.7-100.73-100.72-100.74-100.73-100.74-100.73-100.73-100.73-100.72-100.7-100.73-100.73-100.72-100.72-100.75-100.72-100.71-100.74-100.73-100.73-100.73-100.72-100.74-100.72-100.73-100.72-100.74-100.74-100.74-100.72-100.73-100.7-100.74-100.71-100.74-100.72-100.73-100.73-100.73-100.72-100.72-100.73-100.69-100.72-100.73-100.7-100.74-100.71-100.73-100.71-100.7-100.73-100.76-100.72-100.73-100.72-100.73-100.72-100.74-100.72-100.73-100.72-100.68-100.72-100.71-100.73-100.75-100.72-100.74-100.72-100.73-100.7-100.73-100.71-100.7-100.74-100.73-100.72-100.72-100.71-100.73-100.72-100.73-100.73-100.72-100.71-100.73-100.72-100.71-100.73-100.72-100.73-100.73-100.7-100.74-100.72-100.73-100.74-100.73-100.73-100.7-100.72-100.73-100.74-100.7-100.72-100.72-100.71-100.72-100.72-100.74-100.73-100.72-100.74-100.73-100.71-100.71-100.73-100.73-100.72-100.7-100.72-100.72

Well perhaps if you posted YOUR implementation, we might be able to tell you what's going wrong.

Or start single stepping the code through the debugger, rather than relying on other people to debug your programs for you.

Sorry... my thread got moved. There was code in the post I was referring too. Here is what my code looks like now.

Thank you for your help!

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
FILE *PrData;
char input_file[80];
char one_line[100];
char *line_ptr;

int ii;
int counter = 0;
int a;

float data[4] = {0}; //time, x, y, z
float sum[4] = {0};

double sumSq[4] = {0};
double stDev[4] = {0};
double average[4] = {0};

/*gets name of input file*/
printf("Please enter the name of the file to be read: ");
scanf("%s", &input_file);

/*opens the file to read*/
PrData = fopen(input_file, "r");
if (PrData ==NULL) printf("Could not open input file.\n");
else printf("Input file opened successfully.\n");
while(1)
{
line_ptr = fgets(one_line, sizeof(one_line), PrData);
/*Quit if at end of file. */
if (line_ptr == NULL) break;

/*Replace "new line" will null character (optional)*/
one_line[strlen(one_line)-1]='\0';
/*Print the string just as a test (optional) */
// printf("%s\n", one_line);
(void) sscanf(one_line, "%f %f %f %f", &data[0], &data[1], &data[2], &data[3]);
for( ii = 1; ii<=3; ii++)
{
sum[ii] = sum[ii] + data[ii];
sumSq[ii] = sumSq[ii] + data[ii] * data[ii];
a = 1;
}
if (a ==1) counter++;
a = 0;
}//while(1)

for( ii = 1; ii<=3; ii++)
{
printf("SumSq = %10.2f\t", sumSq[ii]);
average[ii] = sum[ii]/counter;
stDev[ii] = sqrt((sumSq[ii] - counter * average[ii] * average[ii])/(counter - 1));
printf("ave = %10.3f\t stDev = %10.3f\n", average[ii], stDev[ii]);
}
fclose(PrData);
}

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
   FILE *PrData;
   char input_file[80];
   char one_line[100];
   char *line_ptr;

   int ii;
   int counter = 0;
   int a;

   float data[4] = {0}; //time, x, y, z
   float sum[4] = {0};

   double sumSq[4] = {0};
   double stDev[4] = {0};
   double average[4] = {0};

   /*gets name of input file*/
   printf ( "Please enter the name of the file to be read: " );
   scanf ( "%s", &input_file );

   /*opens the file to read*/
   PrData = fopen ( input_file, "r" );
   if ( PrData == NULL ) printf ( "Could not open input file.\n" );
   else printf ( "Input file opened successfully.\n" );
   while ( 1 )
   {
      line_ptr = fgets ( one_line, sizeof ( one_line ), PrData );
      /*Quit if at end of file. */
      if ( line_ptr == NULL ) break;

      /*Replace "new line" will null character (optional)*/
      one_line[strlen ( one_line )-1] = '\0';
      /*Print the string just as a test (optional) */
     // printf("%s\n", one_line);
      ( void ) sscanf ( one_line, "%f %f %f %f", &data[0], &data[1], &data[2], &data[3] );
      for ( ii = 1; ii <= 3; ii++ )
      {
         sum[ii] = sum[ii] + data[ii];
         sumSq[ii] = sumSq[ii] + data[ii] * data[ii];
         a = 1;
      }
      if ( a == 1 ) counter++;
      a = 0;
   }//while(1)

   for ( ii = 1; ii <= 3; ii++ )
   {
      printf ( "SumSq = %10.2f\t", sumSq[ii] );
      average[ii] = sum[ii] / counter;
      stDev[ii] = sqrt ( ( sumSq[ii] - counter * average[ii] * average[ii] ) / ( counter - 1 ) );
      printf ( "ave = %10.3f\t stDev = %10.3f\n", average[ii], stDev[ii] );
   }
   fclose ( PrData );
}

http://en.wikipedia.org/wiki/Standard_deviation

Break the formula down into pieces. At each piece wack a printf in there to check if it's correct etc

Thanks for the suggestion. I tried that & the part that comes out wrong is counter * average[ii] * average[ii].
It's only off when my input values (data[ii]) are around 100. It's ok when they are around 0. Any further ideas out there?

Ignoring for the time being your compiler warnings which you should address...

pedantic.c

#include <stdio.h>
#include <string.h>
#include <math.h>

int main ( void )
{
   FILE *PrData;
   char input_file[80];
   char one_line[100];
   char *line_ptr;


   int j;

   float data[4] = {0}; //time, x, y, z

   /*gets name of input file*/
   printf ( "Please enter the name of the file to be read: " );
   scanf ( "%s", &input_file );

   /*opens the file to read*/
   PrData = fopen ( input_file, "r" );
   if ( PrData == NULL ) printf ( "Could not open input file.\n" );
   else printf ( "Input file opened successfully.\n" );
   while ( 1 )
   {
      line_ptr = fgets ( one_line, sizeof ( one_line ), PrData );
      /*Quit if at end of file. */
      if ( line_ptr == NULL ) break;

      /*Replace "new line" will null character (optional)*/
      one_line[strlen ( one_line )-1] = '\0';
      /*Print the string just as a test (optional) */
      // printf("%s\n", one_line);
      sscanf ( one_line, "%f %f %f %f", &data[0], &data[1], &data[2], &data[3] );

      //calculate the mean
      float total = 0; //initialise

      for ( j = 0; j < 4; j++ )
      {
         total = total + data[j];
      }
      float mean = 0;
      mean = total / 4.0;

      printf ( "The mean is %f", mean );

      //now calculate the standard deviation

      float anotherTotal = 0; //initialise
      for ( j = 0; j < 4; j++ )
      {
         anotherTotal = anotherTotal + ( data[j] - mean ) * ( data[j] - mean );
      }
      float result;
      float tmp = anotherTotal / 4.0;

      result = sqrt ( tmp );

      printf ( "The s.d is %f", result );
      printf ( "\n" );

   }//while(1)


   fclose ( PrData );
   return 0;
}

numbers.txt
5 6 8 9

My output

thwee@ubuntu:~$ gcc -Wall pedantic.c -lm
pedantic.c: In function ‘main’:
pedantic.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[80]’
thwee@ubuntu:~$ ./a.out
Please enter the name of the file to be read: numbers
Input file opened successfully.
The mean is 7.000000 The s.d is 1.581139

do you really think you have so novel an approach to STD DEV that needs to dig up a thread that is 3 years old?

because i assure you the OP hasn't been wainting for 3 years for your answer.


.

Edited 6 Years Ago by jephthah: n/a

This article has been dead for over six months. Start a new discussion instead.