Experiment No.-Program:: Name-Akshay Bora ROLL NO.-7682
Experiment No.-Program:: Name-Akshay Bora ROLL NO.-7682
ROLL NO.-7682
EXPERIMENT NO.-
PROGRAM:
#include<iostream>
#include<math.h>
#include<iomanip>
main()
{ int n,i;
cout<<"This Program finds the best curve fit for a power function."<<endl;
double
x[n],y[n],sumlog10X=0.0,sumlog10Y=0.0,sumlog10XX=0.0,sumlog10Xlog10Y=0.0,sumlog10x1=0.0;
for ( i=1;i<=n;i++)
cout<<"\n\t"<<i<<": ";
cout<<"x["<<i<<"] = ";cin>>x[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];
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<<"\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