C++ Program For Numerical Methods
By hotshotracer, 27th Jun 2012 | Follow this author
| RSS Feed | Short URL http://nut.bz/h2cd3sjm/
Posted in WikinutBusinessIT & Ecommerce
This article contains C++ program for the numerical methods and also I provide free download link of Microsoft Visual C++ for run this program.
- C++ Program for Curve Fitting
- C++ program for Straight Line Fitting
- C++ Program to Find Correlation Coefficient of a Set of Numbers
C++ Program for Curve Fitting
This program for Curve fitting ( y=a+bx+cx^2 ). If you don't have any C++ programming software, Download now for free. This link leads to download page of Microsoft Visual C++.
Program
#include<iostream.h>
#include<conio.h>
void curvefit(double,double,double,double,double,double,double,int);
int main()
{
double x,y;
int i,n;
double p=0,q=0,r=0,s=0,t=0,u=0,v=0;
cout<<"Enter the number of sets:";
cin>>n;
cout<<"Enter the values ofx:";
for(i=0;i<n;i++)
cin>>x;
cout<<"Enter the values of y:";
for(i=0;i<n;i++)
cin>>y;
for(i=0;i<n;i++)
{
p+=x;
q+=(x*x);
r+=(x*y);
s+=(x*x*x);
t+=(x*x*y);
u+=(x*x*x*x);
v+=y;
}
curvefit(p,q,r,s,t,u,v,n);
getch();
return(0);
}
void curvefit(double p,double q,double r,double s,double t,double u,double v,int n)
{
double a,b,c,D,D1,D2,D3;
D=n*(q*u-s*s)-p*(p*u-q*s)+q*(p*s-q*q);
D1=v*(q*u-s*s)-p*(r*u-t*s)+q*(r*s-t*q);
D2=n*(r*u-t*s)-v*(p*u-q*s)+q*(p*t-q*r);
D3=n*(q*t-s*r)-p*(p*t-q*r)+v*(p*s-q*q);
a=D1/D;
b=D2/D;
c=D3/D;
cout<<"\nThe required curve of best fit is y="<<a<<"+"<<b<<"x+"<<c<<"x^2";
}
Run it in Microsoft Visual C++ and you get output as shown below.
Output
Enter the number of sets :6
Enter the values of x:1 1.5 2 2.5 3 3.5
Enter the values of y:1.1 1.3 1.6 2 2.7 3.4
The required curve of best fit is y=1.0323 - 0.192834 x + 0.242456 x^2
C++ program for Straight Line Fitting
This contains program and output for straight ling fitting.
Program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,n;
double t=0,u=0,r=0,s=0;
cout<<"\n\nEnter the number of observations : ";
cin>>n;
cout<<"\n\nEnter the values of x : "<<endl;
for(int z=0;z<n;++z)
cin>>x;
cout<<"\n\nEnter the corresponding values of y : "<<endl;
for(int j=0;j<n;++j)
cin>>y;
for(int i=0;i<n;++i)
{
r=r+y;
s=s+x;
t=t+(x*y);
u=u+(x*x);
}
cout<<"\n\nThe normal equations are "<<endl<<r<<" = "<<s<<"a+ "<<n<<"b"<<endl<<t<<" = "<<u<<"a+ "<<s<<"b";
double b=(r*u-s*t)/(n*u-s*s);
double a=(r*s-n*t)/(s*s-n*u);
cout<<"\n\nThe values of a & b are "<<a<<" & "<<b;
cout<<"\n\nThe equation for the straight line is "<<"y="<<a<<"x+"<<b;
getch();
}
Output
Enter the number of observations : 4
Enter the values of x : 50, 70, 100, 120
Enter the corresponding values of y :12, 15, 21, 25
The normal equations are
73 = 340a+ 4b
6750 = 31800a+ 340b
The values of a & b are 0.187931 & 2.275862
The equation for the straight line is y=0.187931x+2.275862
C++ Program to Find Correlation Coefficient of a Set of Numbers
It is program to correlation coefficient 'r' between x and y and it also predict whether x and y have high correlation, moderate or low correlation.
Program
#include<iostream.h>
#include<conio.h>
#include<math.h>
void corrcoeff(float a,float b,float c,float d,float e,int N);
int main()
{
int N,i;
float X,Y;
float s1,s2,s3,s4,s5;
s1=s2=0;
s3=s4=s5=0;
cout<<"enter the number of sets of values:";
cin>>N;
cout<<"enter the elements of first array:";
for(i=0;i<N;i++)
{
cin>>X;
}
cout<<"enter the elements of secnd array:";
for(i=0;i<N;i++)
{cin>>Y;}
for(i=0;i<N;i++)
{
s1+=X;
s2+=Y;
s3+=(X*Y);
s4+=(X*X);
s5+=(Y*Y);
}
corrcoeff(s1,s2,s3,s4,s5,N);
getch();
}
void corrcoeff(float a,float b,float c,float d,float e,int n)
{
float R;
float U,V;
U=sqrt((n*d)-(a*a));
V=sqrt((n*e)-(b*b));
R=((n*c)-(a*b))/(U*V);
cout<<"the correlation coefficient,r="<<R;
if(R>0.75&&R<1)
cout<<"\nHIGH CORRELATION";
if(R>0.4&&R<0.7)
cout<<"\nmoderate correlation";
if(R>0&&R<0.4)
cout<<"\nLow correlation";
if(R==0)
cout<<"\nZERO CORRELATION";
}
Output
(example)
Enter the number of sets of values:5
Enter the elements of first array:40 41 42 43 44
Enter the elements of the second array: 2 2 2 3 3
The correlation coefficient,r=0.866025
High correlation

Comments