Calculate the standard deviation of a vector

1

I can not solve the following equation:

Hereisthecode:

#include<stdio.h>#include<math.h>intmain(){floatm,media,sigma,p;intvetor[10];media=0;m=0;sigma=0;p=0;for(inti=0;i<10;i++){printf("Digite um número: ");
        scanf("%d", &vetor[i]);
    }
    for(int i = 0; i < 10; i++){
        m = m + vetor[i];
    }
    media = m / 10.0;
    for(int i = 0; i < 10; i++){
        p = p + (vetor[i] - media);
    }
    sigma = sqrt((p * 1)/10);
    printf("Resultado d = %.2f\n", sigma);
}
    
asked by anonymous 30.10.2017 / 03:51

3 answers

4

The formula of the standard deviation has to have the distance squared, which is not in yours. See this formula removed directly from wikipedia :

Where(xi-x)²israisedto²

Inthisformulatheistartsin1andgoestoNwhichcorrespondstoyourthatstartsin0andgoestoN-1,thusnotaffectingcalculations.

Applyingthisfixtoyourcode:

intmain(){...media=m/10.0;for(i=0;i<10;i++){p=p+pow(vetor[i]-media,2);//agoraquadradoaquiutilizandoafunçãopow}sigma=sqrt(p/(10-1));//dividirpor10-1quefaltava,ou9sequisersimplificarprintf("Resultado d = %.2f\n", sigma);

    return 0;
}

See the Ideone example

    
30.10.2017 / 04:21
3

The formula is wrong. You should get the sum of the squared mean variation.

Here's your revised and simplified program:

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

#define QTD_ELEMENTOS 5

int main() {
    int vetor[QTD_ELEMENTOS];

    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        //printf("Digite um número: ");
        scanf("%d", &vetor[i]);
    }

    int somatorio = 0;
    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        somatorio += vetor[i];
    }

    float media = somatorio / (float) QTD_ELEMENTOS;

    float variacoes = 0;
    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        float v = vetor[i] - media;
        variacoes += v * v;
    }

    float sigma = sqrt(variacoes / QTD_ELEMENTOS);
    printf("Resultado d = %.2f\n", sigma);
}

See here working on ideone.

    
30.10.2017 / 04:26
2

Consider the following set containing 10 amostras :

{ 2, 3, 3, 4, 5, 6, 7, 8, 9, 10 }

First of all, we calculate the simple arithmetic mean of the samples in the set:

/ p>

Next,wecalculatedthedeviationofallthesesamplesfromthemean:

Thus,wesquaredthedeviationofeachsamplefromthemean:

Withthis,weareabletocalculatethe Variance :

Calculatethe standard deviation by extracting the square root of the variance:

Hereisacodethatcancalculatethe"Mean", "Variance" and "Standard Deviation" of a set of values separately:

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

#define MAXSIZE 10

double media( double s[], int n )
{
    double sum = 0.0;
    int i = 0;

    for( i = 0; i < n; i++ )
        sum += s[i];

    return sum / n;
}

double variancia( double s[], int n )
{
    double sum = 0.0;
    double dev = 0.0;
    double med = media( s, n );
    int i = 0;

    for( i = 0; i < n; i++ )
    {
        dev = s[i] - med;
        sum += (dev * dev);
    }

    return sum / n;
}

double desvio_padrao( double s[], int n  )
{
    double v = variancia( s, n );
    return sqrt( v );
}

int main( void )
{
    double vetor[ MAXSIZE ];
    int  i;

    for( i = 0; i < MAXSIZE; i++ )
    {
        printf("Digite um numero: ");
        scanf( "%lf", &vetor[i] );
    }

    printf("Media = %g\n", media( vetor, MAXSIZE ) );
    printf("Variancia = %g\n", variancia( vetor, MAXSIZE ) );
    printf("Desvio Padrao = %g\n", desvio_padrao( vetor, MAXSIZE ) );

    return 0;
}

Compiling:

$ gcc -lm desvio.c -o desvio

Test:

Digite um numero: 2
Digite um numero: 3
Digite um numero: 3
Digite um numero: 4
Digite um numero: 5
Digite um numero: 6
Digite um numero: 7
Digite um numero: 8
Digite um numero: 9
Digite um numero: 10
Media = 5.7
Variancia = 6.81
Desvio Padrao = 2.6096
    
30.10.2017 / 05:04