NAME-AKSHAY BORA
ROLL NO.-7682
EXPERIMENT NO.-
PROGRAM:
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
main()
{ int n,i;
cout<<"This Program finds the best curve fit for a power function."<<endl;
cout<<"How many data points ? "<<"\n\tn = ";cin>>n;
double
x[n],y[n],sumlog10X=0.0,sumlog10Y=0.0,sumlog10XX=0.0,sumlog10Xlog10Y=0.0,sumlog10x1=0.0;
cout<<"Give the value of x and corresponding y:"<<endl;
for ( i=1;i<=n;i++)
cout<<"\n\t"<<i<<": ";
cout<<"x["<<i<<"] = ";cin>>x[i];
cout<<"\t y["<<i<<"] = ";cin>>y[i];
sumlog10X+=log10(x[i]);
sumlog10Y+=log10(y[i]);
sumlog10XX+=log10(x[i])*log10(x[i]);
sumlog10Xlog10Y+=log10(x[i])*log10(y[i]);
double a,b;
b=((n*sumlog10Xlog10Y)-(sumlog10X*sumlog10Y))/((n*sumlog10XX)-
(sumlog10X*sumlog10X));
a=(sumlog10Y-b*sumlog10X)/n;
double y1[n];
cout<<"\n\n\tThe corresponding table is:"<<endl;
cout<<"Sl.no."<<setw(5)<<"x"<<setw(10)<<"y"<<setw(10)<<"New Y"<<endl;
for (i=1;i<=n;i++){
y1[i]=(pow(10,a))*pow(x[i],b);
cout<<i<<"."<<setw(10)<<x[i]<<setw(10)<<y[i]<<setw(10)<<y1[i]<<endl;
cout<<"\n\tThe Slope of the given data points is : "<<pow(10,a)<<"\n\n\tThe intercept of
the best fit is : "<<b;
cout<<"\n\n\tThus Equation of the best curve fit is :\n\t";
cout<<"\ty = "<<pow(10,a)<<"*x^("<<b<<")";
return 0;
OUTPUT:
This Program finds the best curve fit for a power function.
How many data points ?
n=6
Give the value of x and corresponding y:
1: x[1] = 1
y[1] = 1200
2: x[2] = 2
y[2] = 900
3: x[3] = 3
y[3] = 600
4: x[4] = 4
y[4] = 200
5: x[5] = 5
y[5] = 110
6: x[6] = 6
y[6] = 50
The corresponding table is:
Sl.no. x y New Y
1. 1 1200 2033.97
2. 2 900 605.177
3. 3 600 297.798
4. 4 200 180.061
5. 5 110 121.882
6. 6 50 88.6055
The Slope of the given data points is : 2033.97
The intercept of the best fit is : -1.74887
Thus Equation of the best curve fit is :
y = 2033.97*x^(-1.74887)
--------------------------------
Process exited after 19.53 seconds with return value 0
Press any key to continue . . .