我的数学出了什么问题

What is wrong with my math?

本文关键字:什么 问题 我的      更新时间:2023-10-16

好的,所以我的老师让我们制作一个使用一组数字的程序,并找到它的标准偏差。我的程序找到了平均值。然而,我的数学有问题。它出了什么问题。它给了我59的平均值和8.4的偏差。平均值是正确的,但偏差应为96.4。我的数学出了什么问题。

编辑:我的程序现在可以工作了
P。S.我已将以下代码更改为当前版本的代码。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Used To Round The Decimal Points
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(1);
//Declaring
double Numbers[] = {65, 49, 74, 59, 48}; //Work On Making This A User Input----------Deivation = 96.4
double Mean = 0, Items = 0, Sum = 0, Deviation = 0;
int Counter;
//Finds The Mean Of The Set Of Numbers
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
    for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
    {
        Sum += Numbers[Counter]; //Adds All Numbers In Array Together
    }
    Items = sizeof(Numbers) / sizeof(double); //Gets The Number Of Items In The Array
    Mean = Sum / Items; //Finds The Mean
}
//Finds The Standard Deviation
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
    Deviation += pow((Numbers[Counter] - Mean), 2) / Items; //Does Math Things...
}
Deviation = sqrt(Deviation);
cout << "Deviation = " << Deviation << endl; //Print Out The Standard Deviation
system("pause");
return 0;
}

[…]然而,偏差应为96.4

方差应该是96.4。它是根据平均值的平方差的平均值计算的,所以你根本不需要平方根:

for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
    Variance += pow((Numbers[Counter] - Mean), 2) / Items;
}
Deviation = sqrt(Variance);

取方差的平方根得到9.81835。

从循环中提取sqrt,并在求和后应用它。

偏差的数学表达式存在错误,该表达式应为集合的方差平方根:

方差=总和(pow(集[i]-平均值,2))/n

偏差=sqrt(方差)

顺便说一句,我认为这里的9.82比96.4 更正确

正如人们指出的那样,错误在于无法直接添加标准偏差。计算标准偏差的更好方法是取(元素从其平均值中减去的)均方根。