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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3616|回復: 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
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 午夜网| 国产精品成av人在线视午夜片 | 欧美黄色绿像 | 欧美成人一区二区三区 | 最新日韩精品 | 五月婷婷视频 | 国产精品视频一 | 日韩在线 | 亚洲视频二区 | 日韩在线免费看 | 精品视频在线一区 | 艹逼网| 91porn在线观看| 日韩在线综合 | 日韩乱码一二三 | 亚洲国产精品一区二区www | 亚洲国产精品va在线看黑人 | 在线免费观看成人 | 婷婷在线视频 | 亚洲欧美中文日韩在线v日本 | 国产精品久久精品 | 亚洲欧美日韩国产 | 国产精品精品视频一区二区三区 | 亚洲欧美激情精品一区二区 | 国产激情一区二区三区 | 精品成人一区二区 | 欧美国产视频 | 激情一区二区三区 | 欧美精品欧美精品系列 | 国产毛片久久久久久久久春天 | 久久久久久免费毛片精品 | 久久中文字幕一区 | 一区二区三区视频免费看 | 久久精品亚洲 | 精品国产欧美 | 欧美成人精品一区二区三区 | 精品久久久久久久人人人人传媒 | 黄色日本视频 | 日韩手机视频 | 中文字幕韩在线第一页 | 日本黄色影片在线观看 |