久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3627|回復: 0
收起左側

C++多點擬合線代碼

[復制鏈接]
ID:233602 發表于 2017-9-21 10:19 | 顯示全部樓層 |閱讀模式
//A program finds the best-fitted straight line to the x and y values, and then writes
//the original x, y values and the new y values on the line (for each x) to a data file.
#include<iostream>;                //include the first library
#include<fstream>;                //include the fstream library
using namespace std;        //tell complier that we will use entire namespace standard
void main(void)
{
double ax,ay;                        //declare two double variables
double x[20]={1.0000,2.0000,3.0000,4.0000,5.0000,6.0000,7.0000,8.0000,9.0000,10.0000,11.0000,12.0000,13.0000,14.0000,15.0000,16.0000,17.0000,18.0000,19.0000,20.0000};
                                                //declare a double array with length 20 to store original x values
double y[20]={13.3068,15.0928,16.2509,18.6416,23.6987,27.6380,26.1267,28.4638,33.9061,35.6735,36.0599,39.9298,40.6010,40.0202,43.9743,43.4650,45.7730,50.0811,51.1671,52.4696};
                                                //declare a double array with length 20 to store original y values
double sumx=0, sumy=0;  //declare two double variables
double sub1=0, sub2=0, sumsub1=0, sumsub2=0;
                                                //declare four double variables
double a,b;                                //declare two double variables               
for(int i=0;i<20;i++)        //set a for loop to calculate the sum of original x values
                                                //and the sum of original y values
{
sumx=sumx+x[i];               
sumy=sumy+y[i];               
}
ax=sumx/20;                                //calculate the average of x values
ay=sumy/20;                                //calculate the average of y values
cout<<"The average value of X is:"<<ax<<"\nThe average value of Y is:"<<ay;
                                                //Display the average of all x values and y values

for(int j=0;j<20;j++)        //set a for loop to calculate the component
                                                //of equation of linear regression         to get the value of slope
{
        sub1=(x[j]-ax)*(y[j]-ay);
        sub2=(x[j]-ax)*(x[j]-ax);
        sumsub1=sumsub1+sub1;
        sumsub2=sumsub2+sub2;
}
a=sumsub1/sumsub2;                //the  equation to calculate the slope
                                                //by using the value get from the last for loop
b=ay-a*ax;                                //calculate the value of constant b
cout<<"\nThe value of slope 'a' is:"<<a<<"\nThe value of 'b' is:"<<b;
                                                 //display the value of slope a and the value of constant b
ofstream out("data.dat");//opening a file and output the value of original
                                                 //x, y and new y to a data file named "data.dat"
out<<"x"<<"\t\t"<<"y"<<"\t\t"<<"new y"<<endl;
                                                 //set the format of values display in data file

for(int k=0;k<20;k++)         //set a for loop to display
                                                 //the value of x, y and calculated new y        
{
out<<x[k]<<"\t\t"<<y[k]<<"\t\t"<<a*x[k]+b<<endl;
}
cout<<"\nThe data is output to the file\n";
                                                 //indicate the data is stored in the data file
system("pause");                 //keep the screen otherwise it will appear fast
}


回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲高清视频在线观看 | 偷派自拍| 亚洲成人精品 | 久久九精品 | 国产农村妇女精品一区 | 日本午夜在线视频 | 欧美一区二区三区四区视频 | 国产成人精品一区二区三区四区 | 一区二区三区免费 | 中文字幕在线网 | 精品久久久久香蕉网 | 欧美精品一区二区三区在线 | 亚洲视频精品在线 | 国产美女福利在线观看 | 精品一级| 337p日本欧洲亚洲大胆鲁鲁 | 久久99精品国产99久久6男男 | av大片| 男人天堂免费在线 | 欧美中文字幕 | 日韩av一区二区在线观看 | 成人在线视频网 | 色网站入口 | 欧美另类视频 | 国产在线视频一区 | 国产一区二区在线视频 | 国产精品91久久久久久 | 91成人免费观看 | 一区二区三区精品视频 | 成人深夜福利 | 超碰在线播 | 一区二区三区av | 国产精品中文字幕在线 | 国产成人精品久久二区二区91 | 国产免费一区二区 | 水蜜桃亚洲一二三四在线 | 日本理论片好看理论片 | 亚洲黄色av | 精品粉嫩aⅴ一区二区三区四区 | 亚洲精品第一页 | 日韩一区二区三区在线观看视频 |