C++最小二乘计算回归方程

介绍

用来理解一下数学思路和练习一下代码。即兴而写的,所以没写注释。
最小二乘法

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
// #include "Windows.h"

double sum_square_x = 0;
double sum_x = 0;
double sum_y = 0;
double sum_xy = 0;
int n = 0;
double ave_x = 0;
double ave_y = 0;
double b_ba = 0;
double a_ba = 0;

using namespace std;

void InputAndSum()
{
cout << "请输入n的值" << endl;
cin >> n;
cout << " " << endl;
double data_x = 0;
double data_y = 0;
int i = 1;
while (i <= n)
{
cout << "请输入第" << i << "个数据的x值" << endl;
cin >> data_x;
cout << " " << endl;
cout << "请输入第" << i << "个数据的y值" << endl;
cin >> data_y;
cout << " " << endl;

sum_x += data_x;
sum_y += data_y;
sum_xy += (data_x * data_y);
sum_square_x += (data_x * data_x);

i++;
}

}

double Average(double a)
{
double ave = a / n;
return ave;
}

double B_ba(double calc1,double calc2)
{
double square_ave_x = calc1 * calc1;
double calc3;
calc3 = (sum_xy - n * calc1 * calc2) / (sum_square_x - n * square_ave_x);
return calc3;
}

void A_ba()
{
a_ba = ave_y - b_ba * ave_x;
}

int main()
{
InputAndSum();
ave_x = Average(sum_x);
ave_y = Average(sum_y);
b_ba = B_ba(ave_x,ave_y);
A_ba();
cout << "---------------------------------" << endl;
cout << "---------------------------------" << endl;
cout << "n的值为: " << n << endl;
cout << "各x的和为: " << sum_x << endl;
cout << "各y的和为: " << sum_y << endl;
cout << "各个xy乘积的求和为: " << sum_xy << endl;
cout << "各个x平方的求和为: " << sum_xy << endl;
cout << "x的均值为: " << ave_x << endl;
cout << "y的均值为: " << ave_y << endl;
cout << "a(ba)为: " << a_ba << endl;
cout << "b(ba)为: " << b_ba << endl;
cout << "样本点中心为:(" << ave_x << "," << ave_y << ")" << endl;
cout << "回归直线方程为 y = " << b_ba << "x + " << "(" << a_ba << ")" << endl;
cout << "---------------------------------" << endl;
cout << "---------------------------------" << endl;
//system("pause");

}

C Sharp 模拟高尔顿钉板正态分布

起因

数学学习了正态分布和高尔顿钉板模型,打算用C#模拟一下

展示

高尔顿钉板模拟

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;

namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
gal oa = new gal();
oa.infor();
Console.WriteLine("请输入格子数(正整数):");
string brid_input = Console.ReadLine();
Console.WriteLine("请输入小球数(正整数):");
string ball_input = Console.ReadLine();
uint brid_input2 = Convert.ToUInt32(brid_input);
uint ball_input2 = Convert.ToUInt32(ball_input);
oa.galton(brid_input2,ball_input2);
Console.ReadLine();
}
}

class gal
{
public void infor()
{
Console.WriteLine("***********");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine("高尔顿钉板模型");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine("***********");
Console.WriteLine(" ");
}
public void galton(uint Grid, uint Ball)
{
int[] grid = new int[Grid];
int number = 0;

for (int counter_ball = 1; counter_ball <= Ball; counter_ball++)
{
for(int times = 1; times <= (Grid - 1); times++)
{

Random rd = new Random();
double l = rd.NextDouble()*2;
int rand = (int)l;
number += rand;
}
grid[number]++;
number = 0;
}

Console.WriteLine("小球总数为{0},格数为{1}", Ball, Grid);

for (int index = 0;index < grid.Length; index++)
{
int k = index + 1;
Console.WriteLine("{0}号格子小球数为{1}", k, grid[index]);
}
}
}
}

Git

GitHub:
https://github.com/CairBin/GaltonKnockedBoards

推荐一个数学绘图软件GeoGebra

内容

作为一名数学爱好者,强大的的绘图软件是必不可少的。
这里我推荐一款我特别喜欢的软件GeoGebra。
具体功能我就不细说了,只有用起来才能体会它的强大。
** 最重要的是它支持Win Mac Linux三大平台!!!! **

然后附上链接地址:

官网:https://www.geogebra.org/

C++ 输出斐波那契数列

代码

先贴代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;
int main(){
int n = 1;
double n_m = 93;
cout << endl;
while(1){
int n = 1;
long long int a=1;
long long int b=1;
cout << "Please input the max value of n." << endl;
cin >> n_m;
int n_max = static_cast<int> (n_m);
if (n_max < 1){
cout << "The max value of n is error." << endl;
}
if (n_max > 92){ //因为斐波那契数列发散很快,所以给n一个上限,防止数列数值溢出
cout << "The value is too large." << endl;
}
else{
while(n <= n_max){
if (n == 1){
cout << a << endl;
n++;
}
if ( n== 2){
cout << b << endl;
n++;
}
else{
a = a+b;
cout << a << endl;
n++;
b = a+b;
cout << b << endl;
n++;
}
}
}
}
}
  • 复制

相关知识

斐波那契数列递推关系F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)
通项公式

为便于理解,本文章方法是根据递推关系来写的程序
相关数学知识请参考:百度百科